diff --git a/src/lib.rs b/src/lib.rs index 879ccc7..c38a6d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,11 @@ pub type JSONArray = Vec; pub type JSONMap = HashMap; /// The type of JSON value +/// +/// The `From` trait is implemented for all the +/// types of values wrapped in the `JSONValue` enum +/// +/// Additionally, `()` will convert to `JSONValue::Null` #[derive(Clone, Debug, PartialEq)] pub enum JSONValue { /// Object Literal @@ -38,6 +43,9 @@ pub enum JSONValue { impl JSONValue { /// Convert the wrapped JSONValue to its simpler rust value /// + /// This is a convenience method that calls the `from` method + /// for the appropriate type. + /// /// Example: /// ``` /// use naive_json_parser::JSONValue; @@ -558,6 +566,15 @@ impl JSON { } /// Convert a `&str` containing JSON into a `Result` + /// + /// Example: + /// ```rust + /// use naive_json_parser::JSON; + /// + /// // This should now be a set of nested `JSONValue` enums containing the parsed values + /// let parse_result = JSON::parse(r#"[1, 2.0, 3e4, "foo", {}, [], true, false, null]"#); + /// # assert!(parse_result.is_ok(), "Parse method example failed"); + /// ``` pub fn parse(json: &str) -> JSONResult { JSON::new(json).parse_value() } @@ -568,49 +585,6 @@ mod tests { use super::JSONValue::{Array, Number, True}; use super::*; - #[test] - fn value_conversion() { - let map: JSONMap = HashMap::new(); - let num = 9.380831539; - let str = "applesauce"; - let arr: JSONArray = vec![ - JSONValue::from(map.clone()), - JSONValue::from(num), - JSONValue::from(str), - ]; - - assert_eq!(map.clone(), JSONMap::from(JSONValue::from(map.clone()))); - assert_eq!(num, f64::from(JSONValue::from(num))); - assert_eq!(String::from(str), String::from(JSONValue::from(str))); - assert_eq!(arr.clone(), JSONArray::from(JSONValue::from(arr.clone()))); - assert_eq!(true, bool::from(JSONValue::from(true))); - assert_eq!(false, bool::from(JSONValue::from(false))); - assert_eq!((), <()>::from(JSONValue::from(()))); - } - - #[test] - fn wrap_and_unwrap() { - let map: JSONMap = HashMap::new(); - let num = 9.380831539; - let str = "applesauce"; - let arr: JSONArray = vec![ - JSONValue::from(map.clone()), - JSONValue::from(num), - JSONValue::from(str), - ]; - - let s: String = JSONValue::from(str).unwrap(); - let a: JSONArray = JSONValue::from(arr.clone()).unwrap(); - - assert_eq!(map.clone(), JSONValue::from(map.clone()).unwrap()); - assert_eq!(num, JSONValue::from(num).unwrap()); - assert_eq!(str, &s); - assert_eq!(arr.clone(), a); - assert_eq!(true, JSONValue::from(true).unwrap()); - assert_eq!(false, JSONValue::from(false).unwrap()); - assert_eq!((), JSONValue::from(()).unwrap()); - } - #[test] fn parse_keyword() { let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True); diff --git a/tests/happy_paths.rs b/tests/happy_paths.rs index 51c8a74..d72df22 100644 --- a/tests/happy_paths.rs +++ b/tests/happy_paths.rs @@ -3,6 +3,49 @@ use naive_json_parser::*; use std::collections::HashMap; use std::f64::consts::PI; +#[test] +fn value_conversion() { + let map: JSONMap = HashMap::new(); + let num = 9.380831539; + let str = "applesauce"; + let arr: JSONArray = vec![ + JSONValue::from(map.clone()), + JSONValue::from(num), + JSONValue::from(str), + ]; + + assert_eq!(map.clone(), JSONMap::from(JSONValue::from(map.clone()))); + assert_eq!(num, f64::from(JSONValue::from(num))); + assert_eq!(String::from(str), String::from(JSONValue::from(str))); + assert_eq!(arr.clone(), JSONArray::from(JSONValue::from(arr.clone()))); + assert_eq!(true, bool::from(JSONValue::from(true))); + assert_eq!(false, bool::from(JSONValue::from(false))); + assert_eq!((), <()>::from(JSONValue::from(()))); +} + +#[test] +fn wrap_and_unwrap() { + let map: JSONMap = HashMap::new(); + let num = 9.380831539; + let str = "applesauce"; + let arr: JSONArray = vec![ + JSONValue::from(map.clone()), + JSONValue::from(num), + JSONValue::from(str), + ]; + + let s: String = JSONValue::from(str).unwrap(); + let a: JSONArray = JSONValue::from(arr.clone()).unwrap(); + + assert_eq!(map.clone(), JSONValue::from(map.clone()).unwrap()); + assert_eq!(num, JSONValue::from(num).unwrap()); + assert_eq!(str, &s); + assert_eq!(arr.clone(), a); + assert_eq!(true, JSONValue::from(true).unwrap()); + assert_eq!(false, JSONValue::from(false).unwrap()); + assert_eq!((), JSONValue::from(()).unwrap()); +} + #[test] fn sanity_check() { let res = JSON::parse(r#""foo""#);