Json type

This commit is contained in:
Greg Shuflin 2022-10-16 01:42:03 -07:00
parent 4e711ee898
commit ec1406a057
1 changed files with 14 additions and 4 deletions

View File

@ -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), "")));
}
}