json_repair/
sanitize.rs

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
crate::ix!();

pub fn sanitize_json_str(s: &str) -> String {
    s.chars()
        .filter(|c| {
            // Allow standard whitespace characters and visible characters
            !c.is_control() || *c == '\n' || *c == '\r' || *c == '\t'
        })
        .collect()
}

pub fn assert_expected_matches_output_result<X: Clone + Debug + PartialEq>(
    input:    &str, 
    output:   &Result<X,JsonRepairError>, 
    expected: &X

) {
    if output != &Ok(expected.clone()) {
        println!("input: {:#?}", input);
        println!("output: {:#?}", output);
        println!("expected: {:#?}", expected);
        assert_eq!(output,&Ok(expected.clone()));
    }
}

pub fn assert_expected_value_matches_output_result(
    input:    &str, 
    output:   &Result<String,JsonRepairError>, 
    expected: &Value

) {
    assert!(output.is_ok());

    let expected = expected.to_string();
    let output   = output.as_ref().unwrap();

    if output != &expected {
        println!("input: {:#?}", input);
        println!("output: {:#?}", output);
        println!("expected: {:#?}", expected);
        assert_eq!(output,&expected);
    }
}


pub fn skip_whitespace(
    chars:    &mut Peekable<Chars>, 
    repaired: &mut String
) {
    while let Some(&nc) = chars.peek() {
        if nc.is_whitespace() {
            repaired.push(chars.next().unwrap());
        } else {
            break;
        }
    }
}

pub fn is_valid_json_value_start(c: char) -> bool {
    c == '"' || c == '\'' || c == '{' || c == '[' || c.is_digit(10) || c == '-' || matches!(c, 't' | 'f' | 'n')
}