typebox 0.1.0

JSON Schema type construction with validation, code generation, and binary layout
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
//! JSON Pointer (RFC6901) operations for [`Value`].
//!
//! Provides path-based access to nested values using JSON Pointer syntax.
//!
//! # Examples
//!
//! ```
//! use typebox::Value;
//! use typebox::value::pointer::{get_pointer, set_pointer, has_pointer};
//!
//! let mut value = Value::object()
//!     .field("user", Value::object()
//!         .field("name", Value::string("Alice"))
//!         .build())
//!     .build();
//!
//! assert_eq!(get_pointer(&value, "/user/name"), Some(&Value::string("Alice")));
//! assert!(has_pointer(&value, "/user/name"));
//! ```

use crate::error::PointerError;
use crate::value::Value;

/// Parses a JSON Pointer path into components.
///
/// RFC6901 escape sequences are decoded:
/// - `~0` → `~`
/// - `~1` → `/`
fn parse_pointer(pointer: &str) -> Vec<String> {
    if pointer.is_empty() {
        return vec![];
    }

    let pointer = pointer.strip_prefix('/').unwrap_or(pointer);

    pointer
        .split('/')
        .map(|component| component.replace("~1", "/").replace("~0", "~"))
        .collect()
}

/// Gets a reference to the value at the given JSON Pointer path.
///
/// Returns `None` if the path doesn't exist or the value structure
/// doesn't match the path (e.g., trying to access a field on an array).
///
/// # Examples
///
/// ```
/// use typebox::Value;
/// use typebox::value::pointer::get_pointer;
///
/// let value = Value::array(vec![
///     Value::int64(1),
///     Value::int64(2),
/// ]);
///
/// assert_eq!(get_pointer(&value, "/0"), Some(&Value::int64(1)));
/// assert_eq!(get_pointer(&value, "/2"), None);
/// ```
pub fn get_pointer<'a>(value: &'a Value, pointer: &str) -> Option<&'a Value> {
    if pointer.is_empty() {
        return Some(value);
    }

    let components = parse_pointer(pointer);
    let mut current = value;

    for component in components {
        current = match current {
            Value::Object(map) => map.get(&component)?,
            Value::Array(arr) => {
                let index: usize = component.parse().ok()?;
                arr.get(index)?
            }
            _ => return None,
        };
    }

    Some(current)
}

/// Gets a mutable reference to the value at the given JSON Pointer path.
///
/// Returns `None` if the path doesn't exist or the value structure
/// doesn't match the path.
pub fn get_pointer_mut<'a>(value: &'a mut Value, pointer: &str) -> Option<&'a mut Value> {
    if pointer.is_empty() {
        return Some(value);
    }

    let components = parse_pointer(pointer);
    let mut current = value;

    for component in components {
        current = match current {
            Value::Object(map) => map.get_mut(&component)?,
            Value::Array(arr) => {
                let index: usize = component.parse().ok()?;
                arr.get_mut(index)?
            }
            _ => return None,
        };
    }

    Some(current)
}

/// Sets the value at the given JSON Pointer path.
///
/// Creates intermediate objects as needed. Returns an error if:
/// - The pointer is empty (cannot replace root)
/// - A non-object/array is encountered in the path
/// - An invalid array index is specified
///
/// # Examples
///
/// ```
/// use typebox::Value;
/// use typebox::value::pointer::{get_pointer, set_pointer};
///
/// let mut value = Value::object().build();
/// set_pointer(&mut value, "/user/name", Value::string("Bob")).unwrap();
/// assert_eq!(get_pointer(&value, "/user/name"), Some(&Value::string("Bob")));
/// ```
///
/// # Errors
///
/// Returns [`PointerError::EmptyPointer`] if pointer is empty.
/// Returns [`PointerError::InvalidPath`] if the path is invalid.
pub fn set_pointer(value: &mut Value, pointer: &str, new_value: Value) -> Result<(), PointerError> {
    if pointer.is_empty() {
        return Err(PointerError::EmptyPointer);
    }

    let components = parse_pointer(pointer);
    if components.is_empty() {
        return Err(PointerError::EmptyPointer);
    }

    let (last, path) = components.split_last().ok_or(PointerError::EmptyPointer)?;

    let mut current = value;

    for component in path {
        current = match current {
            Value::Object(map) => {
                if !map.contains_key(component) {
                    map.insert(component.clone(), Value::object().build());
                }
                map.get_mut(component).unwrap()
            }
            Value::Array(arr) => {
                let index: usize = component.parse().map_err(|_| {
                    PointerError::InvalidPath(format!("Invalid array index: {}", component))
                })?;
                if index >= arr.len() {
                    return Err(PointerError::InvalidPath(format!(
                        "Array index {} out of bounds (length {})",
                        index,
                        arr.len()
                    )));
                }
                &mut arr[index]
            }
            _ => {
                return Err(PointerError::InvalidPath(format!(
                    "Cannot navigate through {}",
                    current.kind()
                )));
            }
        };
    }

    match current {
        Value::Object(map) => {
            map.insert(last.clone(), new_value);
        }
        Value::Array(arr) => {
            let index: usize = last
                .parse()
                .map_err(|_| PointerError::InvalidPath(format!("Invalid array index: {}", last)))?;
            if index >= arr.len() {
                return Err(PointerError::InvalidPath(format!(
                    "Array index {} out of bounds (length {})",
                    index,
                    arr.len()
                )));
            }
            arr[index] = new_value;
        }
        _ => {
            return Err(PointerError::InvalidPath(format!(
                "Cannot set field on {}",
                current.kind()
            )));
        }
    }

    Ok(())
}

/// Deletes the value at the given JSON Pointer path.
///
/// For arrays, removes the element (indices shift). For objects, removes the key.
/// Returns an error if:
/// - The pointer is empty (cannot delete root)
/// - The path doesn't exist
/// - An invalid array index is specified
///
/// # Examples
///
/// ```
/// use typebox::Value;
/// use typebox::value::pointer::{delete_pointer, has_pointer};
///
/// let mut value = Value::object()
///     .field("name", Value::string("Alice"))
///     .field("age", Value::int64(30))
///     .build();
///
/// delete_pointer(&mut value, "/age").unwrap();
/// assert!(!has_pointer(&value, "/age"));
/// ```
///
/// # Errors
///
/// Returns [`PointerError::EmptyPointer`] if pointer is empty.
/// Returns [`PointerError::NotFound`] if the path doesn't exist.
pub fn delete_pointer(value: &mut Value, pointer: &str) -> Result<(), PointerError> {
    if pointer.is_empty() {
        return Err(PointerError::EmptyPointer);
    }

    let components = parse_pointer(pointer);
    if components.is_empty() {
        return Err(PointerError::EmptyPointer);
    }

    let (last, path) = components.split_last().ok_or(PointerError::EmptyPointer)?;

    let current = if path.is_empty() {
        value
    } else {
        let mut current = value;
        for component in path {
            current = match current {
                Value::Object(map) => map
                    .get_mut(component)
                    .ok_or_else(|| PointerError::NotFound(format!("/{}", component)))?,
                Value::Array(arr) => {
                    let index: usize = component.parse().map_err(|_| {
                        PointerError::InvalidPath(format!("Invalid array index: {}", component))
                    })?;
                    arr.get_mut(index)
                        .ok_or_else(|| PointerError::NotFound(format!("/{}", component)))?
                }
                _ => {
                    return Err(PointerError::InvalidPath(format!(
                        "Cannot navigate through {}",
                        current.kind()
                    )));
                }
            };
        }
        current
    };

    match current {
        Value::Object(map) => {
            if map.shift_remove(last).is_none() {
                return Err(PointerError::NotFound(format!("/{}", last)));
            }
        }
        Value::Array(arr) => {
            let index: usize = last
                .parse()
                .map_err(|_| PointerError::InvalidPath(format!("Invalid array index: {}", last)))?;
            if index >= arr.len() {
                return Err(PointerError::NotFound(format!("/{}", last)));
            }
            arr.remove(index);
        }
        _ => {
            return Err(PointerError::InvalidPath(format!(
                "Cannot delete from {}",
                current.kind()
            )));
        }
    }

    Ok(())
}

/// Returns `true` if a value exists at the given JSON Pointer path.
///
/// # Examples
///
/// ```
/// use typebox::Value;
/// use typebox::value::pointer::has_pointer;
///
/// let value = Value::object()
///     .field("name", Value::string("Alice"))
///     .build();
///
/// assert!(has_pointer(&value, "/name"));
/// assert!(!has_pointer(&value, "/age"));
/// assert!(has_pointer(&value, "")); // Root always exists
/// ```
pub fn has_pointer(value: &Value, pointer: &str) -> bool {
    if pointer.is_empty() {
        return true;
    }
    get_pointer(value, pointer).is_some()
}

#[cfg(test)]
mod tests {
    use super::*;
    use indexmap::indexmap;

    #[test]
    fn test_parse_pointer() {
        assert_eq!(parse_pointer(""), Vec::<String>::new());
        assert_eq!(parse_pointer("/"), vec![""]);
        assert_eq!(parse_pointer("/a"), vec!["a"]);
        assert_eq!(parse_pointer("/a/b"), vec!["a", "b"]);
        assert_eq!(parse_pointer("/a/b/c"), vec!["a", "b", "c"]);
    }

    #[test]
    fn test_parse_pointer_escape() {
        assert_eq!(parse_pointer("/~0"), vec!["~"]);
        assert_eq!(parse_pointer("/~1"), vec!["/"]);
        assert_eq!(parse_pointer("/a~0b"), vec!["a~b"]);
        assert_eq!(parse_pointer("/a~1b"), vec!["a/b"]);
        assert_eq!(parse_pointer("/~0~1"), vec!["~/"]);
    }

    #[test]
    fn test_get_pointer_root() {
        let value = Value::int64(42);
        assert_eq!(get_pointer(&value, ""), Some(&Value::int64(42)));
    }

    #[test]
    fn test_get_pointer_object() {
        let value = Value::Object(indexmap! {
            "name".to_string() => Value::string("Alice"),
            "age".to_string() => Value::int64(30),
        });

        assert_eq!(get_pointer(&value, "/name"), Some(&Value::string("Alice")));
        assert_eq!(get_pointer(&value, "/age"), Some(&Value::int64(30)));
        assert_eq!(get_pointer(&value, "/missing"), None);
    }

    #[test]
    fn test_get_pointer_array() {
        let value = Value::array(vec![Value::int64(1), Value::int64(2), Value::int64(3)]);

        assert_eq!(get_pointer(&value, "/0"), Some(&Value::int64(1)));
        assert_eq!(get_pointer(&value, "/1"), Some(&Value::int64(2)));
        assert_eq!(get_pointer(&value, "/2"), Some(&Value::int64(3)));
        assert_eq!(get_pointer(&value, "/3"), None);
        assert_eq!(get_pointer(&value, "/invalid"), None);
    }

    #[test]
    fn test_get_pointer_nested() {
        let inner = Value::Object(indexmap! {
            "city".to_string() => Value::string("NYC"),
        });
        let value = Value::Object(indexmap! {
            "user".to_string() => Value::Object(indexmap! {
                "name".to_string() => Value::string("Bob"),
                "address".to_string() => inner,
            }),
        });

        assert_eq!(
            get_pointer(&value, "/user/name"),
            Some(&Value::string("Bob"))
        );
        assert_eq!(
            get_pointer(&value, "/user/address/city"),
            Some(&Value::string("NYC"))
        );
    }

    #[test]
    fn test_set_pointer_object() {
        let mut value = Value::object().build();
        set_pointer(&mut value, "/name", Value::string("Alice")).unwrap();
        assert_eq!(get_pointer(&value, "/name"), Some(&Value::string("Alice")));
    }

    #[test]
    fn test_set_pointer_nested() {
        let mut value = Value::object().build();
        set_pointer(&mut value, "/user/name", Value::string("Bob")).unwrap();
        assert_eq!(
            get_pointer(&value, "/user/name"),
            Some(&Value::string("Bob"))
        );
    }

    #[test]
    fn test_set_pointer_array() {
        let mut value = Value::array(vec![Value::int64(1), Value::int64(2)]);
        set_pointer(&mut value, "/0", Value::int64(10)).unwrap();
        assert_eq!(get_pointer(&value, "/0"), Some(&Value::int64(10)));
        assert_eq!(get_pointer(&value, "/1"), Some(&Value::int64(2)));
    }

    #[test]
    fn test_set_pointer_empty_error() {
        let mut value = Value::int64(42);
        assert!(matches!(
            set_pointer(&mut value, "", Value::int64(0)),
            Err(PointerError::EmptyPointer)
        ));
    }

    #[test]
    fn test_delete_pointer_object() {
        let mut value = Value::object()
            .field("name", Value::string("Alice"))
            .field("age", Value::int64(30))
            .build();

        delete_pointer(&mut value, "/age").unwrap();
        assert!(!has_pointer(&value, "/age"));
    }

    #[test]
    fn test_delete_pointer_array() {
        let mut value = Value::array(vec![Value::int64(1), Value::int64(2), Value::int64(3)]);
        delete_pointer(&mut value, "/1").unwrap();
        assert_eq!(get_pointer(&value, "/0"), Some(&Value::int64(1)));
        assert_eq!(get_pointer(&value, "/1"), Some(&Value::int64(3)));
    }

    #[test]
    fn test_delete_pointer_not_found() {
        let mut value = Value::object()
            .field("name", Value::string("Alice"))
            .build();
        assert!(matches!(
            delete_pointer(&mut value, "/missing"),
            Err(PointerError::NotFound(_))
        ));
    }

    #[test]
    fn test_has_pointer() {
        let value = Value::object()
            .field("name", Value::string("Alice"))
            .build();

        assert!(has_pointer(&value, ""));
        assert!(has_pointer(&value, "/name"));
        assert!(!has_pointer(&value, "/missing"));
    }

    #[test]
    fn test_get_pointer_mut() {
        let mut value = Value::object().field("count", Value::int64(0)).build();

        if let Some(count) = get_pointer_mut(&mut value, "/count") {
            *count = Value::int64(42);
        }

        assert_eq!(get_pointer(&value, "/count"), Some(&Value::int64(42)));
    }

    #[test]
    fn test_set_pointer_creates_intermediate() {
        let mut value = Value::object().build();
        set_pointer(&mut value, "/a/b/c", Value::int64(42)).unwrap();
        assert_eq!(get_pointer(&value, "/a/b/c"), Some(&Value::int64(42)));
    }

    #[test]
    fn test_set_pointer_invalid_path_through_primitive() {
        let mut value = Value::object().field("name", Value::string("test")).build();
        assert!(matches!(
            set_pointer(&mut value, "/name/invalid", Value::int64(42)),
            Err(PointerError::InvalidPath(_))
        ));
    }
}