Json type

This commit is contained in:
Greg Shuflin 2022-10-16 01:42:03 -07:00
parent 4e711ee898
commit ec1406a057

View File

@ -215,6 +215,7 @@ where
mod tests { mod tests {
use super::*; use super::*;
use std::assert_matches::assert_matches; use std::assert_matches::assert_matches;
use std::collections::HashMap;
#[test] #[test]
fn test_parsing() { fn test_parsing() {
@ -273,15 +274,24 @@ mod tests {
<object> ::= "{" [<property>] {"," <property>}* "}" <object> ::= "{" [<property>] {"," <property>}* "}"
<property> ::= <string> ":" <value> <property> ::= <string> ":" <value>
*/ */
#[derive(Debug, Clone)]
enum JsonValue {
Null,
Bool(bool),
Str(String),
Num(f64),
Array(Vec<JsonValue>),
Object(HashMap<String, JsonValue>),
}
#[test] #[test]
fn parse_json() { fn parse_json() {
let json_null = literal("null"); let json_null = literal("null").to(JsonValue::Null);
let json_true = literal("true"); let json_true = literal("true").to(JsonValue::Bool(true));
let json_false = literal("false"); let json_false = literal("false").to(JsonValue::Bool(false));
let json_value = choice(json_null, choice(json_true, json_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), "")));
} }
} }