From 71fb77af9bccb49b0bbd9eef62da4b9401ddc3c5 Mon Sep 17 00:00:00 2001 From: trentspi Date: Sat, 25 Feb 2017 03:11:20 -0500 Subject: [PATCH 1/5] JSON rust implementation recipe --- Cargo.lock | 7 +++ Cargo.toml | 1 + README.md | 129 ++++++++++++++--------------------------------------- 3 files changed, 41 insertions(+), 96 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37ceaf03..92617553 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "json 0.11.5 (registry+https://github.com/rust-lang/crates.io-index)", "skeptic 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -67,6 +68,11 @@ name = "getopts" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "json" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -138,6 +144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum error-chain 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6930e04918388a9a2e41d518c25cf679ccafe26733fb4127dbf21993f2575d46" "checksum gcc 0.3.43 (registry+https://github.com/rust-lang/crates.io-index)" = "c07c758b972368e703a562686adb39125707cc1ef3399da8c019fc6c2498a75d" "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" +"checksum json 0.11.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5e7eb285e773498f9473a6e2255feffe95db9c55579c7931a6db83c9e02a4673" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)" = "684f330624d8c3784fb9558ca46c4ce488073a8d22450415c5eb4f4cfb0d11b5" "checksum pulldown-cmark 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8361e81576d2e02643b04950e487ec172b687180da65c731c03cf336784e6c07" diff --git a/Cargo.toml b/Cargo.toml index b62af0db..34b2a9aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ build = "build.rs" [dependencies] byteorder = "1.0.0" error-chain = "0.8.0" +json = "0.11.5" [build-dependencies] skeptic = "0.6" diff --git a/README.md b/README.md index 001ac022..57a090ab 100644 --- a/README.md +++ b/README.md @@ -1,117 +1,54 @@ -# Cookin' with Rust +#JSON -A practical guide to the Rust crate ecosystem. +##JSON implementation in Rust: -- [Read and write integers in little-endian byte order](#byteorder) - [![byteorder][byteorder-badge]][byteorder] +[![json][json-badge]][json] -# A note about error handling - -Error handling in Rust is robust when done correctly, but in today's -Rust it requires a fair bit of boilerplate. Because of this one often -seees Rust examples filled with `unwrap` calls instead of proper error -handling. - -Since these recipes are intended to be reused as-is and encourage best -practices, they set up error handling correctly, and when necessary to -reduce boilerplate, they use the [error-chain] crate. - -The code for this setup generally looks like: ```rust -#[macro_use] -extern crate error_chain; - -mod errors { - error_chain! { - foreign_links { - Io(::std::io::Error); - } - } -} - -use errors::*; - -fn main() { run().unwrap() } - -fn run() -> Result<()> { - use std::io::Write; - let ref mut stdout = ::std::io::stdout(); - writeln!(stdout, "hello, world")?; - - Ok(()) -} -``` - -This is using the `error_chain!` macro to define a custom `Error` -and `Result` type, along with an automatic conversion from -the common `::std::io::Error` type. The automatic conversion -makes the `?` operator work - -For more background on error handling in Rust, read TODO and TODO. - - -## Read and write integers in little-endian byte order - -[![byteorder][byteorder-badge]][byteorder] +#[macro_use] +extern crate json; -```rust -extern crate byteorder; +fn main() { -use std::io::Cursor; -use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +let parsed = json::parse(r#" -#[derive(Default, Eq, PartialEq, Debug)] -struct Payload { - kind: u8, - value: u16, +{ + "code": 200, + "success": true, + "payload": { + "features": [ + "awesome", + "easyAPI", + "lowLearningCurve" + ] + } } -fn run() -> Result<()> { - let original_payload = Payload::default(); - let encoded_buf = encode(&original_payload)?; - let decoded_payload = decode(&encoded_buf)?; - assert_eq!(original_payload, decoded_payload); - Ok(()) -} +"#).unwrap(); + +let instantiated = object!{ + "code" => 200, + "success" => true, + "payload" => object!{ + "features" => array![ + "awesome", + "easyAPI", + "lowLearningCurve" + ] + } +}; -fn encode(payload: &Payload) -> Result> { - let mut wtr = vec![]; - wtr.write_u8(payload.kind)?; - wtr.write_u16::(payload.value)?; - Ok(wtr) -} +assert_eq!(parsed, instantiated); -fn decode(buf: &[u8]) -> Result { - let mut rdr = Cursor::new(buf); - Ok(Payload { - kind: rdr.read_u8()?, - value: rdr.read_u16::()?, - }) } - -#[macro_use] -extern crate error_chain; -mod errors { - error_chain! { - foreign_links { - Io(::std::io::Error); - } - } -} -use errors::*; -fn main() { run().unwrap() } ``` - - # License MIT/Apache-2.0 - - -[byteorder-badge]: https://img.shields.io/crates/v/rustc-serialize.svg -[byteorder]: https://docs.rs/byteorder +[json-badge]: https://img.shields.io/crates/v/rustc-serialize.svg +[json]: http://json.rs/doc/json/ From e8e406f99827ebc55d2ffce549e8db57838936f7 Mon Sep 17 00:00:00 2001 From: trentspi Date: Sat, 25 Feb 2017 03:21:26 -0500 Subject: [PATCH 2/5] Added pages directory to match new pages structure and renamed README to json to match crate name --- README.md => pages/json.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.md => pages/json.md (100%) diff --git a/README.md b/pages/json.md similarity index 100% rename from README.md rename to pages/json.md From ace914c9eba3116fcb6ee2613a3a22f80713e3d1 Mon Sep 17 00:00:00 2001 From: trentspi Date: Sat, 25 Feb 2017 03:34:17 -0500 Subject: [PATCH 3/5] Added build implementation for automating md testing --- build.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index a780b3d3..f7056cb3 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,15 @@ extern crate skeptic; +use std::fs; + fn main() { - skeptic::generate_doc_tests(&["README.md"]); + let paths = fs::read_dir("./pages/").unwrap(); + + for path in paths { + let p = path.unwrap().path(); + let s = p.to_str().unwrap(); + if s.contains(".md") { + skeptic::generate_doc_tests(&[s]) + } + } } From 3edbf70a4070bd069425a70b020b49f605859be6 Mon Sep 17 00:00:00 2001 From: trentspi Date: Sat, 25 Feb 2017 03:51:51 -0500 Subject: [PATCH 4/5] Modified label to reference json crate --- pages/json.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pages/json.md b/pages/json.md index 57a090ab..c5278fcf 100644 --- a/pages/json.md +++ b/pages/json.md @@ -5,9 +5,7 @@ [![json][json-badge]][json] - ```rust - #[macro_use] extern crate json; @@ -51,4 +49,4 @@ MIT/Apache-2.0 [json-badge]: https://img.shields.io/crates/v/rustc-serialize.svg -[json]: http://json.rs/doc/json/ +[json]: http://json.rs/doc/json/?label=json From 3abeb1a0f3e8d77aa129170432476d14c0824d9c Mon Sep 17 00:00:00 2001 From: trentspi Date: Sat, 25 Feb 2017 03:54:20 -0500 Subject: [PATCH 5/5] Fixed label reference to json modification --- pages/json.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/json.md b/pages/json.md index c5278fcf..3c614d44 100644 --- a/pages/json.md +++ b/pages/json.md @@ -48,5 +48,5 @@ assert_eq!(parsed, instantiated); MIT/Apache-2.0 -[json-badge]: https://img.shields.io/crates/v/rustc-serialize.svg -[json]: http://json.rs/doc/json/?label=json +[json-badge]: https://img.shields.io/crates/v/rustc-serialize.svg?label=json +[json]: http://json.rs/doc/json/