Work around infinite recursion with closure

This commit is contained in:
Greg Shuflin 2022-10-21 17:04:16 -07:00
parent 9198fdd407
commit 92155a8f36

View File

@ -101,12 +101,15 @@ mod tests {
} }
fn json_array() -> impl JsonParser<'static, JsonValue> { fn json_array() -> impl JsonParser<'static, JsonValue> {
let val = json_value().delimited(whitespace(), whitespace()); move |input| {
let val = json_value().delimited(whitespace(), whitespace());
repeated(val) repeated(val)
.separated_by(literal(","), false) .separated_by(literal(","), false)
.delimited(literal_char('['), literal_char(']')) .delimited(literal_char('['), literal_char(']'))
.map(JsonValue::Array) .map(JsonValue::Array)
.parse(input)
}
} }
fn json_value() -> impl JsonParser<'static, JsonValue> { fn json_value() -> impl JsonParser<'static, JsonValue> {
@ -147,20 +150,33 @@ mod tests {
#[test] #[test]
fn parse_json_array() { fn parse_json_array() {
assert!(json_array().parse(r#"[ 4, 9, "ara",]"#).is_err()); assert!(json_array().parse(r#"[ 4, 9, "ara",]"#).is_err());
// assert_eq!( assert_eq!(
// json_array().parse(r#"[ 4, 9, "foo" ]"#).unwrap(), json_array().parse("[[],[]]").unwrap().0,
// ( JsonValue::Array(vec![JsonValue::Array(vec![]), JsonValue::Array(vec![])])
// JsonValue::Array(vec![ );
// JsonValue::Num(4.), assert_eq!(
// JsonValue::Num(9.0), json_array().parse(r#"[ 4, 9, "foo" ]"#).unwrap(),
// JsonValue::Str("foo".to_string()) (
// ]), JsonValue::Array(vec![
// "" JsonValue::Num(4.),
// ) JsonValue::Num(9.0),
// ); JsonValue::Str("foo".to_string())
// assert_eq!(json_array().parse(r#"[8,null,[],5]"#).unwrap(), (JsonValue::Array(vec![ ]),
// JsonValue::Num(8.), JsonValue::Null, JsonValue::Array(vec![]), JsonValue::Num(5.) ""
// ]), "")); )
// assert_eq!(json_value().parse("true"), Ok((JsonValue::Bool(true), ""))); );
assert_eq!(
json_array().parse(r#"[8,null,[],5]"#).unwrap(),
(
JsonValue::Array(vec![
JsonValue::Num(8.),
JsonValue::Null,
JsonValue::Array(vec![]),
JsonValue::Num(5.)
]),
""
)
);
assert_eq!(json_value().parse("true"), Ok((JsonValue::Bool(true), "")));
} }
} }