Skip to content

Commit 7536a9c

Browse files
committed
update EDN FFI to 0.0.5
1 parent 0ecf3be commit 7536a9c

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

Cargo.lock

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ crate-type = ["dylib"] # Creates dynamic lib
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[dependencies]
16-
cirru_edn = "0.1.12"
17-
cirru_parser = "0.1.8"
16+
cirru_edn = "0.2.8"
17+
cirru_parser = "0.1.12"
1818
tiny_http = "0.8"

src/lib.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ use tiny_http::{Method, Response, Server};
55

66
struct HttpServerOptions {
77
port: u16,
8-
host: String,
8+
host: Box<str>,
99
}
1010

1111
struct ResponseSkeleton {
1212
code: u8,
13-
headers: HashMap<String, String>,
14-
body: String,
13+
headers: HashMap<Box<str>, Box<str>>,
14+
body: Box<str>,
1515
}
1616

1717
#[no_mangle]
1818
pub fn abi_version() -> String {
19-
String::from("0.0.1")
19+
String::from("0.0.5")
2020
}
2121

2222
#[no_mangle]
@@ -41,31 +41,25 @@ pub fn serve_http(
4141
// );
4242

4343
let mut m: HashMap<Edn, Edn> = HashMap::new();
44-
m.insert(
45-
Edn::Keyword(String::from("method")),
46-
Edn::Keyword(request.method().to_string()),
47-
);
48-
m.insert(
49-
Edn::Keyword(String::from("url")),
50-
Edn::Str(request.url().to_string()),
51-
);
44+
m.insert(Edn::kwd("method"), Edn::kwd(&request.method().to_string()));
45+
m.insert(Edn::kwd("url"), Edn::str(request.url().to_string()));
5246

5347
let mut headers: HashMap<Edn, Edn> = HashMap::new();
5448

5549
for pair in request.headers() {
5650
headers.insert(
57-
Edn::Keyword(pair.field.to_string()),
58-
Edn::Str(pair.value.to_string()),
51+
Edn::kwd(&pair.field.to_string()),
52+
Edn::str(pair.value.to_string()),
5953
);
6054
}
61-
m.insert(Edn::Keyword(String::from("headers")), Edn::Map(headers));
55+
m.insert(Edn::kwd("headers"), Edn::Map(headers));
6256

6357
if request.method() != &Method::Get {
6458
let mut content = String::new();
6559
request.as_reader().read_to_string(&mut content).unwrap();
6660
m.insert(
67-
Edn::Keyword(String::from("body")),
68-
Edn::Str(content.to_string()),
61+
Edn::kwd("body"),
62+
Edn::Str(content.to_string().into_boxed_str()),
6963
);
7064
}
7165

@@ -92,21 +86,21 @@ fn parse_options(d: &Edn) -> Result<HttpServerOptions, String> {
9286
match d {
9387
Edn::Nil => Ok(HttpServerOptions {
9488
port: 4000,
95-
host: String::from("0.0.0.0"),
89+
host: String::from("0.0.0.0").into_boxed_str(),
9690
}),
9791
Edn::Map(m) => {
9892
let mut options = HttpServerOptions {
9993
port: 4000,
100-
host: String::from("0.0.0.0"),
94+
host: String::from("0.0.0.0").into_boxed_str(),
10195
};
102-
options.port = match m.get(&Edn::Keyword(String::from("port"))) {
96+
options.port = match m.get(&Edn::kwd("port")) {
10397
Some(Edn::Number(port)) => *port as u16,
10498
None => 4000,
10599
a => return Err(format!("invalid config for port: {:?}", a)),
106100
};
107-
options.host = match m.get(&Edn::Keyword(String::from("host"))) {
101+
options.host = match m.get(&Edn::kwd("host")) {
108102
Some(Edn::Str(host)) => host.to_owned(),
109-
None => String::from("0.0.0.0"),
103+
None => String::from("0.0.0.0").into_boxed_str(),
110104
a => return Err(format!("invalid config for host: {:?}", a)),
111105
};
112106
Ok(options)
@@ -121,27 +115,27 @@ fn parse_response(info: &Edn) -> Result<ResponseSkeleton, String> {
121115
let mut res = ResponseSkeleton {
122116
code: 200,
123117
headers: HashMap::new(),
124-
body: String::from(""),
118+
body: String::from("").into_boxed_str(),
125119
};
126-
res.code = match m.get(&Edn::Keyword(String::from("code"))) {
120+
res.code = match m.get(&Edn::kwd("code")) {
127121
Some(Edn::Number(n)) => *n as u8,
128122
None => 200,
129123
a => return Err(format!("invalid code: {:?}", a)),
130124
};
131-
res.body = match m.get(&Edn::Keyword(String::from("body"))) {
125+
res.body = match m.get(&Edn::kwd("body")) {
132126
Some(Edn::Str(s)) => s.to_owned(),
133-
Some(a) => a.to_string(),
134-
None => String::from(""),
127+
Some(a) => a.to_string().into_boxed_str(),
128+
None => String::from("").into_boxed_str(),
135129
};
136-
res.headers = match m.get(&Edn::Keyword(String::from("headers"))) {
130+
res.headers = match m.get(&Edn::kwd("headers")) {
137131
Some(Edn::Map(m)) => {
138-
let mut hs: HashMap<String, String> = HashMap::new();
132+
let mut hs: HashMap<Box<str>, Box<str>> = HashMap::new();
139133
for (k, v) in m {
140134
if let Edn::Keyword(s) = k {
141135
if let Edn::Str(s2) = v {
142-
hs.insert(s.to_owned(), s2.to_owned());
136+
hs.insert(s.to_str(), s2.to_owned());
143137
} else {
144-
hs.insert(s.to_owned(), v.to_string());
138+
hs.insert(s.to_str(), v.to_string().into_boxed_str());
145139
}
146140
} else {
147141
return Err(format!("invalid head entry: {}", k));

0 commit comments

Comments
 (0)