Json type
This commit is contained in:
parent
4e711ee898
commit
ec1406a057
18
src/lib.rs
18
src/lib.rs
@ -215,6 +215,7 @@ where
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::assert_matches::assert_matches;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[test]
|
||||
fn test_parsing() {
|
||||
@ -273,15 +274,24 @@ mod tests {
|
||||
<object> ::= "{" [<property>] {"," <property>}* "}"
|
||||
<property> ::= <string> ":" <value>
|
||||
*/
|
||||
#[derive(Debug, Clone)]
|
||||
enum JsonValue {
|
||||
Null,
|
||||
Bool(bool),
|
||||
Str(String),
|
||||
Num(f64),
|
||||
Array(Vec<JsonValue>),
|
||||
Object(HashMap<String, JsonValue>),
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_json() {
|
||||
let json_null = literal("null");
|
||||
let json_true = literal("true");
|
||||
let json_false = literal("false");
|
||||
let json_null = literal("null").to(JsonValue::Null);
|
||||
let json_true = literal("true").to(JsonValue::Bool(true));
|
||||
let json_false = literal("false").to(JsonValue::Bool(false));
|
||||
|
||||
let json_value = choice(json_null, choice(json_true, json_false));
|
||||
|
||||
assert_matches!(json_value.parse("true"), Ok(("true", "")));
|
||||
assert_matches!(json_value.parse("true"), Ok((JsonValue::Bool(true), "")));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user