vexy-json-core 1.5.13

Core parser for Vexy JSON's forgiving JSON syntax
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
//! AST visitor pattern for traversing and transforming JSON values
//!
//! This module provides a visitor pattern implementation for traversing
//! the JSON AST, allowing for analysis, transformation, and validation.

use crate::ast::{Number, Value};
use crate::error::Result;
use rustc_hash::FxHashMap;
use std::fmt;

/// Visitor trait for traversing JSON values
pub trait Visitor {
    /// Visit a value (dispatch method)
    fn visit_value(&mut self, value: &Value) -> Result<()> {
        match value {
            Value::Null => self.visit_null(),
            Value::Bool(b) => self.visit_bool(*b),
            Value::Number(n) => self.visit_number(n),
            Value::String(s) => self.visit_string(s),
            Value::Array(arr) => self.visit_array(arr),
            Value::Object(obj) => self.visit_object(obj),
        }
    }

    /// Visit a null value
    fn visit_null(&mut self) -> Result<()> {
        Ok(())
    }

    /// Visit a boolean value
    fn visit_bool(&mut self, _value: bool) -> Result<()> {
        Ok(())
    }

    /// Visit a number value
    fn visit_number(&mut self, _value: &Number) -> Result<()> {
        Ok(())
    }

    /// Visit a string value
    fn visit_string(&mut self, _value: &str) -> Result<()> {
        Ok(())
    }

    /// Visit an array value
    fn visit_array(&mut self, array: &[Value]) -> Result<()> {
        for value in array {
            self.visit_value(value)?;
        }
        Ok(())
    }

    /// Visit an object value
    fn visit_object(&mut self, object: &FxHashMap<String, Value>) -> Result<()> {
        for value in object.values() {
            self.visit_value(value)?;
        }
        Ok(())
    }
}

/// Mutable visitor trait for transforming JSON values
pub trait MutVisitor {
    /// Visit and potentially transform a value
    fn visit_value_mut(&mut self, value: &mut Value) -> Result<()> {
        match value {
            Value::Null => self.visit_null_mut(),
            Value::Bool(b) => self.visit_bool_mut(b),
            Value::Number(n) => self.visit_number_mut(n),
            Value::String(s) => self.visit_string_mut(s),
            Value::Array(arr) => self.visit_array_mut(arr),
            Value::Object(obj) => self.visit_object_mut(obj),
        }
    }

    /// Visit a null value
    fn visit_null_mut(&mut self) -> Result<()> {
        Ok(())
    }

    /// Visit a boolean value
    fn visit_bool_mut(&mut self, _value: &mut bool) -> Result<()> {
        Ok(())
    }

    /// Visit a number value
    fn visit_number_mut(&mut self, _value: &mut Number) -> Result<()> {
        Ok(())
    }

    /// Visit a string value
    fn visit_string_mut(&mut self, _value: &mut String) -> Result<()> {
        Ok(())
    }

    /// Visit an array value
    fn visit_array_mut(&mut self, array: &mut Vec<Value>) -> Result<()> {
        for value in array {
            self.visit_value_mut(value)?;
        }
        Ok(())
    }

    /// Visit an object value
    fn visit_object_mut(&mut self, object: &mut FxHashMap<String, Value>) -> Result<()> {
        for value in object.values_mut() {
            self.visit_value_mut(value)?;
        }
        Ok(())
    }
}

/// Path-aware visitor that tracks the current path in the JSON structure
pub trait PathVisitor {
    /// Visit a value with its path
    fn visit_value_with_path(&mut self, value: &Value, path: &JsonPath) -> Result<()> {
        match value {
            Value::Null => self.visit_null_with_path(path),
            Value::Bool(b) => self.visit_bool_with_path(*b, path),
            Value::Number(n) => self.visit_number_with_path(n, path),
            Value::String(s) => self.visit_string_with_path(s, path),
            Value::Array(arr) => self.visit_array_with_path(arr, path),
            Value::Object(obj) => self.visit_object_with_path(obj, path),
        }
    }

    /// Visit a null value with path
    fn visit_null_with_path(&mut self, _path: &JsonPath) -> Result<()> {
        Ok(())
    }

    /// Visit a boolean value with path
    fn visit_bool_with_path(&mut self, _value: bool, _path: &JsonPath) -> Result<()> {
        Ok(())
    }

    /// Visit a number value with path
    fn visit_number_with_path(&mut self, _value: &Number, _path: &JsonPath) -> Result<()> {
        Ok(())
    }

    /// Visit a string value with path
    fn visit_string_with_path(&mut self, _value: &str, _path: &JsonPath) -> Result<()> {
        Ok(())
    }

    /// Visit an array value with path
    fn visit_array_with_path(&mut self, array: &[Value], path: &JsonPath) -> Result<()> {
        for (i, value) in array.iter().enumerate() {
            let mut child_path = path.clone();
            child_path.push(PathSegment::Index(i));
            self.visit_value_with_path(value, &child_path)?;
        }
        Ok(())
    }

    /// Visit an object value with path
    fn visit_object_with_path(
        &mut self,
        object: &FxHashMap<String, Value>,
        path: &JsonPath,
    ) -> Result<()> {
        for (key, value) in object {
            let mut child_path = path.clone();
            child_path.push(PathSegment::Key(key.clone()));
            self.visit_value_with_path(value, &child_path)?;
        }
        Ok(())
    }
}

/// JSON path representation
#[derive(Debug, Clone, PartialEq)]
pub struct JsonPath {
    segments: Vec<PathSegment>,
}

/// Path segment in a JSON structure
#[derive(Debug, Clone, PartialEq)]
pub enum PathSegment {
    /// Object key
    Key(String),
    /// Array index
    Index(usize),
}

impl JsonPath {
    /// Create a new empty path
    pub fn new() -> Self {
        JsonPath {
            segments: Vec::new(),
        }
    }

    /// Create a root path
    pub fn root() -> Self {
        Self::new()
    }

    /// Push a segment to the path
    pub fn push(&mut self, segment: PathSegment) {
        self.segments.push(segment);
    }

    /// Pop the last segment
    pub fn pop(&mut self) -> Option<PathSegment> {
        self.segments.pop()
    }
}

impl fmt::Display for JsonPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "$")?;
        for segment in &self.segments {
            match segment {
                PathSegment::Key(key) => write!(f, ".{key}")?,
                PathSegment::Index(idx) => write!(f, "[{idx}]")?,
            }
        }
        Ok(())
    }
}

impl Default for JsonPath {
    fn default() -> Self {
        Self::new()
    }
}

/// Helper function to walk a value with a visitor
pub fn walk<V: Visitor>(value: &Value, visitor: &mut V) -> Result<()> {
    visitor.visit_value(value)
}

/// Helper function to walk a value with a mutable visitor
pub fn walk_mut<V: MutVisitor>(value: &mut Value, visitor: &mut V) -> Result<()> {
    visitor.visit_value_mut(value)
}

/// Helper function to walk a value with a path-aware visitor
pub fn walk_with_path<V: PathVisitor>(value: &Value, visitor: &mut V) -> Result<()> {
    let path = JsonPath::root();
    visitor.visit_value_with_path(value, &path)
}

// Example visitor implementations

/// A visitor that counts occurrences of each JSON value type.
///
/// This visitor can be used to gather statistics about a JSON document,
/// counting how many nulls, booleans, numbers, strings, arrays, and objects it contains.
#[derive(Debug, Default)]
pub struct CountingVisitor {
    /// Number of null values encountered.
    pub null_count: usize,
    /// Number of boolean values encountered.
    pub bool_count: usize,
    /// Number of numeric values encountered.
    pub number_count: usize,
    /// Number of string values encountered.
    pub string_count: usize,
    /// Number of arrays encountered.
    pub array_count: usize,
    /// Number of objects encountered.
    pub object_count: usize,
}

impl Visitor for CountingVisitor {
    fn visit_null(&mut self) -> Result<()> {
        self.null_count += 1;
        Ok(())
    }

    fn visit_bool(&mut self, _value: bool) -> Result<()> {
        self.bool_count += 1;
        Ok(())
    }

    fn visit_number(&mut self, _value: &Number) -> Result<()> {
        self.number_count += 1;
        Ok(())
    }

    fn visit_string(&mut self, _value: &str) -> Result<()> {
        self.string_count += 1;
        Ok(())
    }

    fn visit_array(&mut self, array: &[Value]) -> Result<()> {
        self.array_count += 1;
        for value in array {
            self.visit_value(value)?;
        }
        Ok(())
    }

    fn visit_object(&mut self, object: &FxHashMap<String, Value>) -> Result<()> {
        self.object_count += 1;
        for value in object.values() {
            self.visit_value(value)?;
        }
        Ok(())
    }
}

/// Visitor that collects all string values from a JSON document.
#[derive(Debug, Default)]
pub struct StringCollector {
    /// All string values found in the document.
    pub strings: Vec<String>,
}

impl Visitor for StringCollector {
    fn visit_string(&mut self, value: &str) -> Result<()> {
        self.strings.push(value.to_string());
        Ok(())
    }
}

/// Visitor that finds values at specific paths
pub struct PathFinder {
    target_path: String,
    results: Vec<Value>,
}

impl PathFinder {
    /// Create a new path finder for the given path
    pub fn new(path: &str) -> Self {
        PathFinder {
            target_path: path.to_string(),
            results: Vec::new(),
        }
    }

    /// Get the found values
    pub fn results(self) -> Vec<Value> {
        self.results
    }
}

impl PathVisitor for PathFinder {
    fn visit_value_with_path(&mut self, value: &Value, path: &JsonPath) -> Result<()> {
        if path.to_string() == self.target_path {
            self.results.push(value.clone());
        }

        // Continue traversing
        match value {
            Value::Array(arr) => self.visit_array_with_path(arr, path)?,
            Value::Object(obj) => self.visit_object_with_path(obj, path)?,
            _ => {}
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::builder::ObjectBuilder;

    #[test]
    fn test_counting_visitor() {
        let value = ObjectBuilder::new()
            .string("name", "test")
            .integer("count", 42)
            .bool("active", true)
            .null("optional")
            .build()
            .unwrap();

        let mut visitor = CountingVisitor::default();
        walk(&value, &mut visitor).unwrap();

        assert_eq!(visitor.object_count, 1);
        assert_eq!(visitor.string_count, 1);
        assert_eq!(visitor.number_count, 1);
        assert_eq!(visitor.bool_count, 1);
        assert_eq!(visitor.null_count, 1);
    }

    #[test]
    fn test_string_collector() {
        let value = Value::Array(vec![
            Value::String("hello".to_string()),
            Value::Object({
                let mut map = FxHashMap::default();
                map.insert("key".to_string(), Value::String("world".to_string()));
                map
            }),
            Value::String("rust".to_string()),
        ]);

        let mut visitor = StringCollector::default();
        walk(&value, &mut visitor).unwrap();

        assert_eq!(visitor.strings, vec!["hello", "world", "rust"]);
    }

    #[test]
    fn test_path_visitor() {
        let value = ObjectBuilder::new()
            .insert(
                "user",
                ObjectBuilder::new()
                    .string("name", "Alice")
                    .integer("age", 30)
                    .build()
                    .unwrap(),
            )
            .build()
            .unwrap();

        let mut finder = PathFinder::new("$.user.name");
        walk_with_path(&value, &mut finder).unwrap();

        let results = finder.results();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0], Value::String("Alice".to_string()));
    }

    #[test]
    fn test_mut_visitor() {
        let mut value = Value::Array(vec![
            Value::String("hello".to_string()),
            Value::String("world".to_string()),
        ]);

        struct UppercaseVisitor;
        impl MutVisitor for UppercaseVisitor {
            fn visit_string_mut(&mut self, value: &mut String) -> Result<()> {
                *value = value.to_uppercase();
                Ok(())
            }
        }

        let mut visitor = UppercaseVisitor;
        walk_mut(&mut value, &mut visitor).unwrap();

        match value {
            Value::Array(arr) => {
                assert_eq!(arr[0], Value::String("HELLO".to_string()));
                assert_eq!(arr[1], Value::String("WORLD".to_string()));
            }
            _ => panic!("Expected array"),
        }
    }
}