From ec1406a057628f674e97e706af634d8022cc9e4e Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 16 Oct 2022 01:42:03 -0700 Subject: [PATCH] Json type --- src/lib.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0d43c51..760bda8 100644 --- a/src/lib.rs +++ b/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 { ::= "{" [] {"," }* "}" ::= ":" */ + #[derive(Debug, Clone)] + enum JsonValue { + Null, + Bool(bool), + Str(String), + Num(f64), + Array(Vec), + Object(HashMap), + } #[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), ""))); } }