diff options
author | Yorhel <git@yorhel.nl> | 2017-03-05 09:06:48 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2017-03-05 09:06:48 +0100 |
commit | 9d7038274905a6932630cf9a56c0ca19d63f8408 (patch) | |
tree | e60c11ac15dd6923156be96198cca809b74e2cc6 | |
parent | 634d0ea29755da9047014f94a04e445646e49bfc (diff) |
Refactor message handling and IO into separate crates
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | vndbapi-msg/Cargo.toml | 7 | ||||
-rw-r--r-- | vndbapi-msg/src/lib.rs | 6 | ||||
-rw-r--r-- | vndbapi-msg/src/msg.rs (renamed from src/msg.rs) | 0 | ||||
-rw-r--r-- | vndbapi-msg/src/parser.rs (renamed from src/parser.rs) | 9 |
7 files changed, 25 insertions, 10 deletions
@@ -1,2 +1,4 @@ target Cargo.lock +vndbapi-msg/target +vndbapi-msg/Cargo.lock @@ -4,8 +4,8 @@ version = "0.1.0" authors = ["Yorhel <projects@yorhel.nl>"] [dependencies] +vndbapi-msg = { path="vndbapi-msg" } rustls = "0.5.3" lazy_static = "0.2.2" -webpki = "0.8.0" # Must be the same as used by rustls +webpki = "0.10.0" # Must be the same as used by rustls netbuf = "0.3.8" -serde_json = "0.8.4" @@ -2,14 +2,11 @@ extern crate rustls; extern crate webpki; extern crate netbuf; -extern crate serde_json; +extern crate vndbapi_msg as msg; mod conn; -mod parser; - -pub mod msg; -pub use conn::Connection; +pub use conn::*; static LE_ROOT: [webpki::TrustAnchor<'static>; 2] = [ // ISRG Root X1 - The Let's Encrypt root cert diff --git a/vndbapi-msg/Cargo.toml b/vndbapi-msg/Cargo.toml new file mode 100644 index 0000000..05655f1 --- /dev/null +++ b/vndbapi-msg/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "vndbapi-msg" +version = "0.1.0" +authors = ["Yorhel <projects@yorhel.nl>"] + +[dependencies] +serde_json = "0.9.8" diff --git a/vndbapi-msg/src/lib.rs b/vndbapi-msg/src/lib.rs new file mode 100644 index 0000000..d36199c --- /dev/null +++ b/vndbapi-msg/src/lib.rs @@ -0,0 +1,6 @@ +extern crate serde_json; + +mod parser; +mod msg; + +pub use msg::*; diff --git a/src/msg.rs b/vndbapi-msg/src/msg.rs index ead8ac0..ead8ac0 100644 --- a/src/msg.rs +++ b/vndbapi-msg/src/msg.rs diff --git a/src/parser.rs b/vndbapi-msg/src/parser.rs index 0644e45..135c71d 100644 --- a/src/parser.rs +++ b/vndbapi-msg/src/parser.rs @@ -9,12 +9,15 @@ type Result<T> = ::std::result::Result<T, &'static str>; /* Parse a JSON value with trailing data. This is a workaround until a proper solution has * been implemented: https://github.com/serde-rs/json/issues/183 */ fn parse_json(s: &str) -> Result<(Value, &str)> { + println!("Parsing JSON: {}", s); let mut bytes = 0; - let val: Value = ::serde_json::de::StreamDeserializer::new(s.bytes().map(|b| { bytes += 1; Ok(b)} )) - .next().ok_or("Expected JSON value")?.map_err(|_| "Invalid JSON value")?; + let val: Value = ::serde_json::Deserializer::from_iter(s.bytes().map(|b| { bytes += 1; Ok(b)} )) + .into_iter().next() + .ok_or("Expected JSON value")? + .map_err(|_| "Invalid JSON value")?; // The JSON deserializer consumes one extra byte for numeric types, subtract that. - match val { Value::I64(_) | Value::U64(_) | Value::F64(_) => bytes -= 1, _ => () }; + match val { Value::Number(_) => bytes -= 1, _ => () }; Ok((val, &s[bytes..])) } |