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> {
let val = json_value().delimited(whitespace(), whitespace());
move |input| {
let val = json_value().delimited(whitespace(), whitespace());
repeated(val)
.separated_by(literal(","), false)
.delimited(literal_char('['), literal_char(']'))
.map(JsonValue::Array)
repeated(val)
.separated_by(literal(","), false)
.delimited(literal_char('['), literal_char(']'))
.map(JsonValue::Array)
.parse(input)
}
}
fn json_value() -> impl JsonParser<'static, JsonValue> {
@ -147,20 +150,33 @@ mod tests {
#[test]
fn parse_json_array() {
assert!(json_array().parse(r#"[ 4, 9, "ara",]"#).is_err());
// assert_eq!(
// json_array().parse(r#"[ 4, 9, "foo" ]"#).unwrap(),
// (
// 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("[[],[]]").unwrap().0,
JsonValue::Array(vec![JsonValue::Array(vec![]), JsonValue::Array(vec![])])
);
assert_eq!(
json_array().parse(r#"[ 4, 9, "foo" ]"#).unwrap(),
(
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), "")));
}
}