parse_that 0.3.2

Zero-copy parser combinator library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#[cfg(test)]
mod tests {
    use parse_that::parsers::json::JsonValue;
    use parse_that::parsers::json::json_parser;
    use std::borrow::Cow;
    use std::fs;

    /// Helper: lookup a key in a Vec-of-pairs object.
    fn obj_get<'a, 'b>(
        pairs: &'b [(Cow<'a, str>, JsonValue<'a>)],
        key: &str,
    ) -> Option<&'b JsonValue<'a>> {
        pairs
            .iter()
            .find(|(k, _)| k.as_ref() == key)
            .map(|(_, v)| v)
    }

    // ── Combinator path tests ───────────────────────────────────────

    #[test]
    fn test_json() {
        let json = r#"
            {
                "a": 1,
                "b": 2,
                "c": 3
            }
        "#;

        let result = json_parser().parse(json).unwrap();

        match result {
            JsonValue::Object(pairs) => {
                assert_eq!(pairs.len(), 3);
                assert_eq!(obj_get(&pairs, "a").unwrap(), &JsonValue::Number(1.0));
                assert_eq!(obj_get(&pairs, "b").unwrap(), &JsonValue::Number(2.0));
                assert_eq!(obj_get(&pairs, "c").unwrap(), &JsonValue::Number(3.0));
            }
            _ => panic!("Expected JsonValue::Object"),
        }
    }

    #[test]
    fn test_json_file() {
        let json_file_path = "../../data/json/data-l.json";
        let json_string = fs::read_to_string(json_file_path).unwrap();

        let arr = json_parser().parse(&json_string).unwrap();

        match arr {
            JsonValue::Array(arr) => {
                assert_eq!(arr.len(), 4784);
            }
            _ => panic!("Expected JsonValue::Array"),
        }
    }

    #[test]
    fn test_json_file_print() {
        use pprint::{Printer, pprint};

        let json_file_path = "../../data/json/data.json";
        let json_string = fs::read_to_string(json_file_path).unwrap();

        let arr = json_parser().parse(&json_string).unwrap();

        let pretty = pprint(arr, Printer::default());

        println!("{}", pretty);
    }

    // ── String edge case tests (combinator path — raw spans) ────────

    #[test]
    fn test_string_basic_escapes() {
        let cases = vec![
            r#""\"""#, r#""\\""#, r#""\/""#, r#""\b""#, r#""\n""#, r#""\r""#, r#""\t""#,
        ];

        let parser = json_parser();
        for input in &cases {
            let result = parser.parse(input);
            assert!(result.is_some(), "Failed to parse string: {}", input);
            match result.unwrap() {
                JsonValue::String(s) => {
                    // Combinator path returns raw span — just verify it parsed
                    assert!(!s.is_empty());
                }
                other => panic!("Expected String, got {:?} for input {}", other, input),
            }
        }
    }

    #[test]
    fn test_string_unicode_escape() {
        let input = r#""\u0041""#; // \u0041 = 'A'
        let result = json_parser().parse(input);
        assert!(result.is_some(), "Failed to parse unicode escape");
        match result.unwrap() {
            JsonValue::String(s) => {
                // Combinator path returns raw span
                assert_eq!(s.as_ref(), r"\u0041");
            }
            other => panic!("Expected String, got {:?}", other),
        }
    }

    #[test]
    fn test_string_empty() {
        let input = r#""""#;
        let result = json_parser().parse(input);
        assert!(result.is_some(), "Failed to parse empty string");
        match result.unwrap() {
            JsonValue::String(s) => assert_eq!(s.as_ref(), ""),
            other => panic!("Expected String, got {:?}", other),
        }
    }

    #[test]
    fn test_string_nested_escapes() {
        let input = r#""\\\"""#;
        let result = json_parser().parse(input);
        assert!(result.is_some(), "Failed to parse nested escapes");
    }

    #[test]
    fn test_string_only_escapes() {
        let input = r#""\n\t\r""#;
        let result = json_parser().parse(input);
        assert!(result.is_some(), "Failed to parse string with only escapes");
    }

    #[test]
    fn test_string_with_forward_slash_escape() {
        let input = r#""http:\/\/example.com""#;
        let result = json_parser().parse(input);
        assert!(result.is_some(), "Failed to parse string with \\/ escapes");
    }

    #[test]
    fn test_string_surrogate_pair_edges() {
        let valid = r#""\uD834\uDD1E""#;
        assert!(
            json_parser().parse(valid).is_some(),
            "Valid surrogate pair should parse"
        );

        let lone_high = r#""\uD834x""#;
        assert!(
            json_parser().parse(lone_high).is_none(),
            "Lone high surrogate should fail"
        );

        let lone_low = r#""\uDD1E""#;
        assert!(
            json_parser().parse(lone_low).is_none(),
            "Lone low surrogate should fail"
        );
    }

    #[test]
    fn test_malformed_unterminated_string() {
        let input = r#""hello"#;
        let result = json_parser().parse(input);
        assert!(result.is_none(), "Should reject unterminated string");
    }

    #[test]
    fn test_malformed_truncated_unicode() {
        let input = r#""\u00""#;
        let result = json_parser().parse(input);
        assert!(result.is_none(), "Should reject truncated \\uXX");
    }

    #[test]
    fn test_malformed_unicode_at_end() {
        let input = r#""\u""#;
        let result = json_parser().parse(input);
        assert!(result.is_none(), "Should reject \\u with no hex digits");
    }

    #[test]
    fn test_malformed_backslash_at_end() {
        let input = "\"\\";
        let result = json_parser().parse(input);
        assert!(result.is_none(), "Should reject backslash at end of input");
    }

    #[test]
    fn test_malformed_not_json() {
        let result = json_parser().parse("hello");
        assert!(result.is_none(), "Should reject bare identifier");
    }

    #[test]
    fn test_null_value() {
        let result = json_parser().parse("null").unwrap();
        assert_eq!(result, JsonValue::Null);
    }

    #[test]
    fn test_boolean_values() {
        assert_eq!(json_parser().parse("true").unwrap(), JsonValue::Bool(true));
        assert_eq!(
            json_parser().parse("false").unwrap(),
            JsonValue::Bool(false)
        );
    }

    #[test]
    fn test_empty_array() {
        let result = json_parser().parse("[]").unwrap();
        assert_eq!(result, JsonValue::Array(Box::new(vec![])));
    }

    #[test]
    fn test_empty_object() {
        let result = json_parser().parse("{}").unwrap();
        match result {
            JsonValue::Object(pairs) => assert_eq!(pairs.len(), 0),
            other => panic!("Expected empty object, got {:?}", other),
        }
    }

    #[test]
    fn test_nested_arrays() {
        let input = "[[1, 2], [3, 4]]";
        let result = json_parser().parse(input).unwrap();
        match result {
            JsonValue::Array(arr) => {
                assert_eq!(arr.len(), 2);
                match &arr[0] {
                    JsonValue::Array(inner) => assert_eq!(inner.len(), 2),
                    other => panic!("Expected inner array, got {:?}", other),
                }
            }
            other => panic!("Expected Array, got {:?}", other),
        }
    }

    #[test]
    fn test_negative_number() {
        let result = json_parser().parse("-42.5").unwrap();
        assert_eq!(result, JsonValue::Number(-42.5));
    }

    #[test]
    fn test_scientific_notation() {
        let result = json_parser().parse("1.5e10").unwrap();
        assert_eq!(result, JsonValue::Number(1.5e10));
    }

    #[test]
    fn test_long_string() {
        let long = "a".repeat(10_000);
        let input = format!("\"{}\"", long);
        let result = json_parser().parse(&input).unwrap();
        match result {
            JsonValue::String(s) => assert_eq!(s.len(), 10_000),
            other => panic!("Expected String, got {:?}", other),
        }
    }

    #[test]
    fn test_single_element_containers() {
        match json_parser().parse("[42]").unwrap() {
            JsonValue::Array(arr) => {
                assert_eq!(arr.len(), 1);
                assert_eq!(arr[0], JsonValue::Number(42.0));
            }
            other => panic!("Expected Array, got {:?}", other),
        }
        match json_parser().parse(r#"{"x": true}"#).unwrap() {
            JsonValue::Object(pairs) => {
                assert_eq!(pairs.len(), 1);
                assert_eq!(pairs[0].0.as_ref(), "x");
                assert_eq!(pairs[0].1, JsonValue::Bool(true));
            }
            other => panic!("Expected Object, got {:?}", other),
        }
    }

    #[test]
    fn test_whitespace_variants() {
        assert!(json_parser().parse("{\t\"a\"\t:\t1\t}").is_some());
        assert!(json_parser().parse("{\r\n\"a\"\r\n:\r\n1\r\n}").is_some());
        assert!(json_parser().parse(" \t\r\n null \t\r\n ").is_some());
    }

    #[test]
    fn test_number_edge_cases() {
        let parser = json_parser();
        assert_eq!(parser.parse("1.5e-10").unwrap(), JsonValue::Number(1.5e-10));
        assert_eq!(parser.parse("1.5e+10").unwrap(), JsonValue::Number(1.5e10));
        assert_eq!(parser.parse("1.0").unwrap(), JsonValue::Number(1.0));
    }

    #[test]
    fn test_integer_values() {
        let parser = json_parser();
        assert_eq!(parser.parse("42").unwrap(), JsonValue::Number(42.0));
        assert_eq!(parser.parse("-100").unwrap(), JsonValue::Number(-100.0));
        assert_eq!(parser.parse("0").unwrap(), JsonValue::Number(0.0));
    }

    #[test]
    fn test_large_numeric_literals_match_serde_f64() {
        let parser = json_parser();
        let cases = [
            "9007199254740991",
            "9007199254740992",
            "12345678901234567890",
            "-9223372036854775808",
            "18446744073709551615",
        ];

        for input in cases {
            let ours = parser.parse(input).unwrap();
            let theirs: serde_json::Value = serde_json::from_str(input).unwrap();
            let serde_num = theirs.as_f64().expect("number should convert to f64");
            match ours {
                JsonValue::Number(n) => assert_eq!(n, serde_num, "Mismatch for {input}"),
                other => panic!("Expected number for {input}, got {:?}", other),
            }
        }
    }

    // ── Structural comparison helpers ───────────────────────────────

    fn count_values(v: &JsonValue) -> usize {
        match v {
            JsonValue::Null | JsonValue::Bool(_) | JsonValue::Number(_) | JsonValue::String(_) => 1,
            JsonValue::Array(arr) => 1 + arr.iter().map(count_values).sum::<usize>(),
            JsonValue::Object(pairs) => {
                1 + pairs.iter().map(|(_, v)| count_values(v)).sum::<usize>()
            }
        }
    }

    fn count_serde_values(v: &serde_json::Value) -> usize {
        match v {
            serde_json::Value::Null
            | serde_json::Value::Bool(_)
            | serde_json::Value::Number(_)
            | serde_json::Value::String(_) => 1,
            serde_json::Value::Array(arr) => 1 + arr.iter().map(count_serde_values).sum::<usize>(),
            serde_json::Value::Object(map) => {
                1 + map.values().map(count_serde_values).sum::<usize>()
            }
        }
    }

    fn compare_structure(ours: &JsonValue, theirs: &serde_json::Value) {
        match (ours, theirs) {
            (JsonValue::Null, serde_json::Value::Null) => {}
            (JsonValue::Bool(a), serde_json::Value::Bool(b)) => assert_eq!(a, b),
            (JsonValue::Number(a), serde_json::Value::Number(b)) => {
                let b_f64 = b.as_f64().unwrap();
                assert!(
                    (a - b_f64).abs() < 1e-10 || (a.is_nan() && b_f64.is_nan()),
                    "Number mismatch: {} vs {}",
                    a,
                    b_f64
                );
            }
            (JsonValue::String(_), serde_json::Value::String(_)) => {
                // Combinator path returns raw spans — skip string content comparison
            }
            (JsonValue::Array(a), serde_json::Value::Array(b)) => {
                assert_eq!(a.len(), b.len(), "Array length mismatch");
                for (ai, bi) in a.iter().zip(b.iter()) {
                    compare_structure(ai, bi);
                }
            }
            (JsonValue::Object(a), serde_json::Value::Object(b)) => {
                assert_eq!(a.len(), b.len(), "Object key count mismatch");
                for (key, val) in a.iter() {
                    let serde_val = b.get(key.as_ref()).unwrap_or_else(|| {
                        panic!("Key '{}' in parse_that but not in serde_json", key)
                    });
                    compare_structure(val, serde_val);
                }
            }
            _ => panic!(
                "Type mismatch: parse_that={:?}, serde={:?}",
                std::mem::discriminant(ours),
                theirs
            ),
        }
    }

    // ── Combinator equivalence tests with serde_json ──────────────

    fn test_equivalence_for_file(filename: &str) {
        let path = format!("../../data/json/{}", filename);
        let data =
            fs::read_to_string(&path).unwrap_or_else(|e| panic!("Failed to read {}: {}", path, e));

        let ours = json_parser()
            .parse(&data)
            .unwrap_or_else(|| panic!("parse_that failed on {}", filename));
        let theirs: serde_json::Value = serde_json::from_str(&data)
            .unwrap_or_else(|e| panic!("serde_json failed on {}: {}", filename, e));

        let our_count = count_values(&ours);
        let their_count = count_serde_values(&theirs);
        assert_eq!(
            our_count, their_count,
            "{}: value count mismatch: parse_that={} serde={}",
            filename, our_count, their_count
        );

        compare_structure(&ours, &theirs);
    }

    #[test]
    fn test_equivalence_data() {
        test_equivalence_for_file("data.json");
    }

    #[test]
    fn test_equivalence_canada() {
        test_equivalence_for_file("canada.json");
    }

    #[test]
    fn test_equivalence_apache() {
        test_equivalence_for_file("apache-builds.json");
    }

    #[test]
    fn test_equivalence_data_xl() {
        test_equivalence_for_file("data-xl.json");
    }

    #[test]
    fn test_equivalence_twitter() {
        test_equivalence_for_file("twitter.json");
    }

    #[test]
    fn test_equivalence_citm_catalog() {
        test_equivalence_for_file("citm_catalog.json");
    }

    // ── RFC 8259 leading-zero rejection ──────────────────────────────

    #[test]
    fn test_leading_zero_rejected() {
        // RFC 8259: "007" should parse as just "0", not "007"
        let parser = json_parser();
        let mut state = parse_that::state::ParserState::new("007");
        let result = parser.call(&mut state);
        assert!(result.is_some());
        match result.unwrap() {
            JsonValue::Number(n) => {
                assert_eq!(n, 0.0);
                assert_eq!(state.offset, 1); // consumed only "0"
            }
            other => panic!("expected Number, got {:?}", other),
        }
    }

    #[test]
    fn test_leading_zero_standalone() {
        // "0" alone is valid
        let parser = json_parser();
        let mut state = parse_that::state::ParserState::new("0");
        let result = parser.call(&mut state);
        assert!(result.is_some());
        match result.unwrap() {
            JsonValue::Number(n) => assert_eq!(n, 0.0),
            other => panic!("expected Number, got {:?}", other),
        }
    }

    #[test]
    fn test_leading_zero_decimal() {
        // "0.5" is valid (leading zero followed by decimal)
        let parser = json_parser();
        let mut state = parse_that::state::ParserState::new("0.5");
        let result = parser.call(&mut state);
        assert!(result.is_some());
        match result.unwrap() {
            JsonValue::Number(n) => {
                assert_eq!(n, 0.5);
                assert_eq!(state.offset, 3); // consumed all "0.5"
            }
            other => panic!("expected Number, got {:?}", other),
        }
    }
}