jsonpath_rust/path/
json.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
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
use crate::path::config::cache::{RegexCache, RegexCacheInst};
use regex::Regex;
use serde_json::Value;

/// compare sizes of json elements
/// The method expects to get a number on the right side and array or string or object on the left
/// where the number of characters, elements or fields will be compared respectively.
pub fn size(left: Vec<&Value>, right: Vec<&Value>) -> bool {
    if let Some(Value::Number(n)) = right.first() {
        if let Some(sz) = n.as_f64() {
            for el in left.iter() {
                match el {
                    Value::String(v) if v.len() == sz as usize => true,
                    Value::Array(elems) if elems.len() == sz as usize => true,
                    Value::Object(fields) if fields.len() == sz as usize => true,
                    _ => return false,
                };
            }
            return true;
        }
    }
    false
}

/// ensure the array on the left side is a subset of the array on the right side.
//todo change the naive impl to sets
pub fn sub_set_of(left: Vec<&Value>, right: Vec<&Value>) -> bool {
    if left.is_empty() {
        return true;
    }
    if right.is_empty() {
        return false;
    }

    if let Some(elems) = left.first().and_then(|e| e.as_array()) {
        if let Some(Value::Array(right_elems)) = right.first() {
            if right_elems.is_empty() {
                return false;
            }

            for el in elems {
                let mut res = false;

                for r in right_elems.iter() {
                    if el.eq(r) {
                        res = true
                    }
                }
                if !res {
                    return false;
                }
            }
            return true;
        }
    }
    false
}

/// ensure at least one element in the array  on the left side belongs to the array on the right side.
//todo change the naive impl to sets
pub fn any_of(left: Vec<&Value>, right: Vec<&Value>) -> bool {
    if left.is_empty() {
        return true;
    }
    if right.is_empty() {
        return false;
    }

    if let Some(Value::Array(elems)) = right.first() {
        if elems.is_empty() {
            return false;
        }

        for el in left.iter() {
            if let Some(left_elems) = el.as_array() {
                for l in left_elems.iter() {
                    for r in elems.iter() {
                        if l.eq(r) {
                            return true;
                        }
                    }
                }
            } else {
                for r in elems.iter() {
                    if el.eq(&r) {
                        return true;
                    }
                }
            }
        }
    }

    false
}

/// ensure that the element on the left sides matches the regex on the right side
pub fn regex(
    left: Vec<&Value>,
    right: Vec<&Value>,
    cache: &RegexCache<impl RegexCacheInst + Clone>,
) -> bool {
    if left.is_empty() || right.is_empty() {
        return false;
    }

    match right.first() {
        Some(Value::String(str)) => {
            if cache.is_implemented() {
                cache
                    .get_instance()
                    .and_then(|inst| inst.validate(str, left))
                    .unwrap_or(false)
            } else if let Ok(regex) = Regex::new(str) {
                for el in left.iter() {
                    if let Some(v) = el.as_str() {
                        if regex.is_match(v) {
                            return true;
                        }
                    }
                }
                false
            } else {
                false
            }
        }
        _ => false,
    }
}

/// ensure that the element on the left side belongs to the array on the right side.
pub fn inside(left: Vec<&Value>, right: Vec<&Value>) -> bool {
    if left.is_empty() {
        return false;
    }

    match right.first() {
        Some(Value::Array(elems)) => {
            for el in left.iter() {
                if elems.contains(el) {
                    return true;
                }
            }
            false
        }
        Some(Value::Object(elems)) => {
            for el in left.iter() {
                for r in elems.values() {
                    if el.eq(&r) {
                        return true;
                    }
                }
            }
            false
        }
        _ => false,
    }
}

/// ensure the number on the left side is less the number on the right side
pub fn less(left: Vec<&Value>, right: Vec<&Value>) -> bool {
    if left.len() == 1 && right.len() == 1 {
        match (left.first(), right.first()) {
            (Some(Value::Number(l)), Some(Value::Number(r))) => l
                .as_f64()
                .and_then(|v1| r.as_f64().map(|v2| v1 < v2))
                .unwrap_or(false),
            _ => false,
        }
    } else {
        false
    }
}

/// compare elements
pub fn eq(left: Vec<&Value>, right: Vec<&Value>) -> bool {
    if left.len() != right.len() {
        false
    } else {
        left.iter().zip(right).map(|(a, b)| a.eq(&b)).all(|a| a)
    }
}

#[cfg(test)]
mod tests {
    use crate::path::config::cache::RegexCache;
    use crate::path::json::{any_of, eq, less, regex, size, sub_set_of};
    use serde_json::{json, Value};

    #[test]
    fn value_eq_test() {
        let left = json!({"value":42});
        let right = json!({"value":42});
        let right_uneq = json!([42]);

        assert!(&left.eq(&right));
        assert!(!&left.eq(&right_uneq));
    }

    #[test]
    fn vec_value_test() {
        let left = json!({"value":42});
        let left1 = json!(42);
        let left2 = json!([1, 2, 3]);
        let left3 = json!({"value2":[42],"value":[42]});

        let right = json!({"value":42});
        let right1 = json!(42);
        let right2 = json!([1, 2, 3]);
        let right3 = json!({"value":[42],"value2":[42]});

        assert!(eq(vec![&left], vec![&right]));

        assert!(!eq(vec![], vec![&right]));
        assert!(!eq(vec![&right], vec![]));

        assert!(eq(
            vec![&left, &left1, &left2, &left3],
            vec![&right, &right1, &right2, &right3],
        ));

        assert!(!eq(
            vec![&left1, &left, &left2, &left3],
            vec![&right, &right1, &right2, &right3],
        ));
    }

    #[test]
    fn less_value_test() {
        let left = json!(10);
        let right = json!(11);

        assert!(less(vec![&left], vec![&right]));
        assert!(!less(vec![&right], vec![&left]));

        let left = json!(-10);
        let right = json!(-11);

        assert!(!less(vec![&left], vec![&right]));
        assert!(less(vec![&right], vec![&left]));

        let left = json!(-10.0);
        let right = json!(-11.0);

        assert!(!less(vec![&left], vec![&right]));
        assert!(less(vec![&right], vec![&left]));

        assert!(!less(vec![], vec![&right]));
        assert!(!less(vec![&right, &right], vec![&left]));
    }

    #[test]
    fn regex_test() {
        let right = json!("[a-zA-Z]+[0-9]#[0-9]+");
        let left1 = json!("a11#");
        let left2 = json!("a1#1");
        let left3 = json!("a#11");
        let left4 = json!("#a11");

        assert!(regex(
            vec![&left1, &left2, &left3, &left4],
            vec![&right],
            &RegexCache::default()
        ));
        assert!(!regex(
            vec![&left1, &left3, &left4],
            vec![&right],
            &RegexCache::default()
        ))
    }

    #[test]
    fn any_of_test() {
        let right = json!([1, 2, 3, 4, 5, 6]);
        let left = json!([1, 100, 101]);
        assert!(any_of(vec![&left], vec![&right]));

        let left = json!([11, 100, 101]);
        assert!(!any_of(vec![&left], vec![&right]));

        let left1 = json!(1);
        let left2 = json!(11);
        assert!(any_of(vec![&left1, &left2], vec![&right]));
    }

    #[test]
    fn sub_set_of_test() {
        let left1 = json!(1);
        let left2 = json!(2);
        let left3 = json!(3);
        let left40 = json!(40);
        let right = json!([1, 2, 3, 4, 5, 6]);
        assert!(sub_set_of(
            vec![&Value::Array(vec![
                left1.clone(),
                left2.clone(),
                left3.clone(),
            ])],
            vec![&right],
        ));
        assert!(!sub_set_of(
            vec![&Value::Array(vec![left1, left2, left3, left40])],
            vec![&right],
        ));
    }

    #[test]
    fn size_test() {
        let left1 = json!("abc");
        let left2 = json!([1, 2, 3]);
        let left3 = json!([1, 2, 3, 4]);
        let right = json!(3);
        assert!(size(vec![&left1], vec![&right]));
        assert!(size(vec![&left2], vec![&right]));
        assert!(!size(vec![&left3], vec![&right]));
    }
}