Add some more documentation
All checks were successful
timw4mail/json-parser/master This commit looks good

This commit is contained in:
Timothy Warren 2020-02-12 11:38:37 -05:00
parent 05e6e53cfb
commit d93fa28630
2 changed files with 60 additions and 43 deletions

View File

@ -11,6 +11,11 @@ pub type JSONArray = Vec<JSONValue>;
pub type JSONMap = HashMap<String, JSONValue>; pub type JSONMap = HashMap<String, JSONValue>;
/// The type of JSON value /// 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)] #[derive(Clone, Debug, PartialEq)]
pub enum JSONValue { pub enum JSONValue {
/// Object Literal /// Object Literal
@ -38,6 +43,9 @@ pub enum JSONValue {
impl JSONValue { impl JSONValue {
/// Convert the wrapped JSONValue to its simpler rust value /// Convert the wrapped JSONValue to its simpler rust value
/// ///
/// This is a convenience method that calls the `from` method
/// for the appropriate type.
///
/// Example: /// Example:
/// ``` /// ```
/// use naive_json_parser::JSONValue; /// use naive_json_parser::JSONValue;
@ -558,6 +566,15 @@ impl JSON {
} }
/// Convert a `&str` containing JSON into a `Result<JSONValue, ParseError>` /// Convert a `&str` containing JSON into a `Result<JSONValue, ParseError>`
///
/// 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 { pub fn parse(json: &str) -> JSONResult {
JSON::new(json).parse_value() JSON::new(json).parse_value()
} }
@ -568,49 +585,6 @@ mod tests {
use super::JSONValue::{Array, Number, True}; use super::JSONValue::{Array, Number, True};
use super::*; 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] #[test]
fn parse_keyword() { fn parse_keyword() {
let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True); let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True);

View File

@ -3,6 +3,49 @@ use naive_json_parser::*;
use std::collections::HashMap; use std::collections::HashMap;
use std::f64::consts::PI; 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] #[test]
fn sanity_check() { fn sanity_check() {
let res = JSON::parse(r#""foo""#); let res = JSON::parse(r#""foo""#);