ploidy-pointer 0.10.0

JSON Pointers for strongly-typed data structures
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use std::{
    any::Any,
    borrow::Cow,
    collections::{BTreeMap, HashMap},
    fmt::{Debug, Display},
    hash::BuildHasher,
    ops::{Deref, Range},
    rc::Rc,
    sync::Arc,
};

use itertools::Itertools;

#[cfg(feature = "derive")]
pub use ploidy_pointer_derive::JsonPointee;

/// A parsed JSON Pointer.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct JsonPointer<'a>(Cow<'a, [JsonPointerSegment<'a>]>);

impl JsonPointer<'static> {
    /// Constructs a pointer from an RFC 6901 string,
    /// with segments that own their contents.
    pub fn parse_owned(s: &str) -> Result<Self, BadJsonPointerSyntax> {
        if s.is_empty() {
            return Ok(Self::empty());
        }
        let Some(s) = s.strip_prefix('/') else {
            return Err(BadJsonPointerSyntax::MissingLeadingSlash);
        };
        let segments = s
            .split('/')
            .map(str::to_owned)
            .map(JsonPointerSegment::from_str)
            .collect_vec();
        Ok(Self(segments.into()))
    }
}

impl<'a> JsonPointer<'a> {
    /// Constructs an empty pointer that resolves to the current value.
    pub fn empty() -> Self {
        Self(Cow::Borrowed(&[]))
    }

    /// Constructs a pointer from an RFC 6901 string,
    /// with segments that borrow from the string.
    pub fn parse(s: &'a str) -> Result<Self, BadJsonPointerSyntax> {
        if s.is_empty() {
            return Ok(Self::empty());
        }
        let Some(s) = s.strip_prefix('/') else {
            return Err(BadJsonPointerSyntax::MissingLeadingSlash);
        };
        let segments = s.split('/').map(JsonPointerSegment::from_str).collect_vec();
        Ok(Self(segments.into()))
    }

    /// Returns `true` if this is an empty pointer.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the first segment of this pointer, or `None`
    /// if this is an empty pointer.
    pub fn head(&self) -> Option<&JsonPointerSegment<'a>> {
        self.0.first()
    }

    /// Returns a new pointer without the first segment of this pointer.
    /// If this pointer has only one segment, or is an empty pointer,
    /// returns an empty pointer.
    pub fn tail(&self) -> JsonPointer<'_> {
        self.0
            .get(1..)
            .map(|tail| JsonPointer(tail.into()))
            .unwrap_or_else(JsonPointer::empty)
    }

    /// Returns a borrowing iterator over this pointer's segments.
    pub fn segments(&self) -> JsonPointerSegments<'_> {
        JsonPointerSegments(self.0.iter())
    }

    /// Returns a consuming iterator over this pointer's segments.
    pub fn into_segments(self) -> IntoJsonPointerSegments<'a> {
        IntoJsonPointerSegments(self.0.into_owned().into_iter())
    }
}

impl Display for JsonPointer<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &*self.0 {
            [] => Ok(()),
            segments => write!(f, "/{}", segments.iter().format("/")),
        }
    }
}

/// A value that a [`JsonPointer`] points to.
pub trait JsonPointee: Any {
    /// Resolves a [`JsonPointer`] against this value.
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer>;
}

impl dyn JsonPointee {
    /// Returns a reference to the pointed-to value if it's of type `T`,
    /// or `None` if it isn't.
    #[inline]
    pub fn downcast_ref<T: JsonPointee>(&self) -> Option<&T> {
        (self as &dyn Any).downcast_ref::<T>()
    }

    /// Returns `true` if the pointed-to value is of type `T`.
    #[inline]
    pub fn is<T: JsonPointee>(&self) -> bool {
        (self as &dyn Any).is::<T>()
    }
}

/// A borrowing iterator over the segments of a [`JsonPointer`].
#[derive(Clone, Debug)]
pub struct JsonPointerSegments<'a>(std::slice::Iter<'a, JsonPointerSegment<'a>>);

impl<'a> Iterator for JsonPointerSegments<'a> {
    type Item = &'a JsonPointerSegment<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }

    #[inline]
    fn count(self) -> usize {
        self.0.count()
    }

    #[inline]
    fn last(mut self) -> Option<Self::Item> {
        self.next_back()
    }
}

impl ExactSizeIterator for JsonPointerSegments<'_> {}

impl DoubleEndedIterator for JsonPointerSegments<'_> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back()
    }
}

/// A consuming iterator over the segments of a [`JsonPointer`].
#[derive(Debug)]
pub struct IntoJsonPointerSegments<'a>(std::vec::IntoIter<JsonPointerSegment<'a>>);

impl<'a> Iterator for IntoJsonPointerSegments<'a> {
    type Item = JsonPointerSegment<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }

    #[inline]
    fn count(self) -> usize {
        self.0.count()
    }

    #[inline]
    fn last(mut self) -> Option<Self::Item> {
        self.next_back()
    }
}

impl ExactSizeIterator for IntoJsonPointerSegments<'_> {}

impl DoubleEndedIterator for IntoJsonPointerSegments<'_> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back()
    }
}

/// A single segment of a [`JsonPointer`].
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct JsonPointerSegment<'a>(Cow<'a, str>);

impl<'a> JsonPointerSegment<'a> {
    #[inline]
    fn from_str(s: impl Into<Cow<'a, str>>) -> Self {
        let s = s.into();
        if s.contains('~') {
            Self(s.replace("~1", "/").replace("~0", "~").into())
        } else {
            Self(s)
        }
    }

    /// Returns the string value of this segment.
    #[inline]
    pub fn as_str(&self) -> &str {
        self
    }

    /// Returns the value of this segment as an array index,
    /// or `None` if this segment can't be used as an index.
    #[inline]
    pub fn to_index(&self) -> Option<usize> {
        match self.as_bytes() {
            [b'0'] => Some(0),
            [b'1'..=b'9', rest @ ..] if rest.iter().all(|b: &u8| b.is_ascii_digit()) => {
                // `usize::from_str` allows a leading `+`, and
                // ignores leading zeros; RFC 6901 forbids both.
                self.parse().ok()
            }
            _ => None,
        }
    }
}

impl Deref for JsonPointerSegment<'_> {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Display for JsonPointerSegment<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.replace("~", "~0").replace("/", "~1"))
    }
}

macro_rules! impl_pointee_for {
    () => {};
    (#[$($attrs:tt)+] $ty:ty $(, $($rest:tt)*)?) => {
        #[$($attrs)*]
        impl_pointee_for!($ty);
        $(impl_pointee_for!($($rest)*);)?
    };
    ($ty:ty $(, $($rest:tt)*)?) => {
        impl JsonPointee for $ty {
            fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
                if pointer.is_empty() {
                    Ok(self)
                } else {
                    Err({
                        #[cfg(feature = "did-you-mean")]
                        let err = BadJsonPointerTy::with_ty(
                            &pointer,
                            JsonPointeeTy::Named(stringify!($ty)),
                        );
                        #[cfg(not(feature = "did-you-mean"))]
                        let err = BadJsonPointerTy::new(&pointer);
                        err
                    })?
                }
            }
        }
        $(impl_pointee_for!($($rest)*);)?
    };
}

impl_pointee_for!(
    i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, f32, f64, bool, String, &'static str,
    #[cfg(feature = "chrono")] chrono::DateTime<chrono::Utc>,
    #[cfg(feature = "url")] url::Url,
);

impl<T: JsonPointee> JsonPointee for Option<T> {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        if let Some(value) = self {
            value.resolve(pointer)
        } else {
            let Some(key) = pointer.head() else {
                return Ok(&None::<T>);
            };
            Err({
                #[cfg(feature = "did-you-mean")]
                let err = BadJsonPointerKey::with_ty(key, JsonPointeeTy::name_of(self));
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerKey::new(key);
                err
            })?
        }
    }
}

impl<T: JsonPointee> JsonPointee for Box<T> {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        (**self).resolve(pointer)
    }
}

impl<T: JsonPointee> JsonPointee for Arc<T> {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        (**self).resolve(pointer)
    }
}

impl<T: JsonPointee> JsonPointee for Rc<T> {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        (**self).resolve(pointer)
    }
}

impl<T: JsonPointee> JsonPointee for Vec<T> {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        let Some(key) = pointer.head() else {
            return Ok(self);
        };
        if let Some(index) = key.to_index() {
            if let Some(item) = self.get(index) {
                item.resolve(pointer.tail())
            } else {
                Err(BadJsonPointer::Index(index, 0..self.len()))
            }
        } else {
            Err({
                #[cfg(feature = "did-you-mean")]
                let err =
                    BadJsonPointerTy::with_ty(&pointer, JsonPointeeTy::Named(stringify!($ty)));
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerTy::new(&pointer);
                err
            })?
        }
    }
}

impl<T, H> JsonPointee for HashMap<String, T, H>
where
    T: JsonPointee,
    H: BuildHasher + 'static,
{
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        let Some(key) = pointer.head() else {
            return Ok(self);
        };
        if let Some(value) = self.get(key.as_str()) {
            value.resolve(pointer.tail())
        } else {
            Err({
                #[cfg(feature = "did-you-mean")]
                let err = BadJsonPointerKey::with_suggestions(
                    key,
                    JsonPointeeTy::name_of(self),
                    self.keys().map(|key| key.as_str()),
                );
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerKey::new(key);
                err
            })?
        }
    }
}

impl<T: JsonPointee> JsonPointee for BTreeMap<String, T> {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        let Some(key) = pointer.head() else {
            return Ok(self);
        };
        if let Some(value) = self.get(key.as_str()) {
            value.resolve(pointer.tail())
        } else {
            Err({
                #[cfg(feature = "did-you-mean")]
                let err = BadJsonPointerKey::with_suggestions(
                    key,
                    JsonPointeeTy::name_of(self),
                    self.keys().map(|key| key.as_str()),
                );
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerKey::new(key);
                err
            })?
        }
    }
}

#[cfg(feature = "indexmap")]
impl<T, H> JsonPointee for indexmap::IndexMap<String, T, H>
where
    T: JsonPointee,
    H: BuildHasher + 'static,
{
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        let Some(key) = pointer.head() else {
            return Ok(self);
        };
        if let Some(value) = self.get(key.as_str()) {
            value.resolve(pointer.tail())
        } else {
            Err({
                #[cfg(feature = "did-you-mean")]
                let err = BadJsonPointerKey::with_suggestions(
                    key,
                    JsonPointeeTy::name_of(self),
                    self.keys().map(|key| key.as_str()),
                );
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerKey::new(key);
                err
            })?
        }
    }
}

#[cfg(feature = "serde_json")]
impl JsonPointee for serde_json::Value {
    fn resolve(&self, pointer: JsonPointer<'_>) -> Result<&dyn JsonPointee, BadJsonPointer> {
        let Some(key) = pointer.head() else {
            return Ok(self);
        };
        match self {
            serde_json::Value::Object(map) => {
                if let Some(value) = map.get(key.as_str()) {
                    value.resolve(pointer.tail())
                } else {
                    Err({
                        #[cfg(feature = "did-you-mean")]
                        let err = BadJsonPointerKey::with_suggestions(
                            key,
                            JsonPointeeTy::name_of(map),
                            map.keys().map(|key| key.as_str()),
                        );
                        #[cfg(not(feature = "did-you-mean"))]
                        let err = BadJsonPointerKey::new(key);
                        err
                    })?
                }
            }
            serde_json::Value::Array(array) => {
                let Some(index) = key.to_index() else {
                    return Err({
                        #[cfg(feature = "did-you-mean")]
                        let err =
                            BadJsonPointerTy::with_ty(&pointer, JsonPointeeTy::name_of(array));
                        #[cfg(not(feature = "did-you-mean"))]
                        let err = BadJsonPointerTy::new(&pointer);
                        err
                    })?;
                };
                if let Some(item) = array.get(index) {
                    item.resolve(pointer.tail())
                } else {
                    Err(BadJsonPointer::Index(index, 0..array.len()))
                }
            }
            serde_json::Value::Null => Err({
                #[cfg(feature = "did-you-mean")]
                let err = BadJsonPointerKey::with_ty(key, JsonPointeeTy::name_of(self));
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerKey::new(key);
                err
            })?,
            _ => Err({
                #[cfg(feature = "did-you-mean")]
                let err = BadJsonPointerTy::with_ty(&pointer, JsonPointeeTy::name_of(self));
                #[cfg(not(feature = "did-you-mean"))]
                let err = BadJsonPointerTy::new(&pointer);
                err
            })?,
        }
    }
}

/// An error that occurs during parsing.
#[derive(Debug, thiserror::Error)]
pub enum BadJsonPointerSyntax {
    #[error("JSON Pointer must start with `/`")]
    MissingLeadingSlash,
}

/// An error that occurs during traversal.
#[derive(Debug, thiserror::Error)]
pub enum BadJsonPointer {
    #[error(transparent)]
    Key(#[from] BadJsonPointerKey),
    #[error("index {} out of range {}..{}", .0, .1.start, .1.end)]
    Index(usize, Range<usize>),
    #[error(transparent)]
    Ty(#[from] BadJsonPointerTy),
}

/// An error that occurs when a pointed-to value doesn't have a key
/// that the pointer references, with an optional suggestion
/// for the correct key.
#[derive(Debug)]
pub struct BadJsonPointerKey {
    pub key: String,
    pub context: Option<BadJsonPointerKeyContext>,
}

impl BadJsonPointerKey {
    #[cold]
    pub fn new(key: &JsonPointerSegment<'_>) -> Self {
        Self {
            key: key.to_string(),
            context: None,
        }
    }

    #[cfg(feature = "did-you-mean")]
    #[cold]
    pub fn with_ty(key: &JsonPointerSegment<'_>, ty: JsonPointeeTy) -> Self {
        Self {
            key: key.to_string(),
            context: Some(BadJsonPointerKeyContext {
                ty,
                suggestion: None,
            }),
        }
    }

    #[cfg(feature = "did-you-mean")]
    #[cold]
    pub fn with_suggestions<'a>(
        key: &'a JsonPointerSegment<'_>,
        ty: JsonPointeeTy,
        suggestions: impl IntoIterator<Item = &'a str>,
    ) -> Self {
        let suggestion = suggestions
            .into_iter()
            .map(|suggestion| (suggestion, strsim::jaro_winkler(key.as_str(), suggestion)))
            .max_by(|&(_, a), &(_, b)| {
                // `strsim::jaro_winkler` returns the Jaro-Winkler _similarity_,
                // not distance; so higher values mean the strings are closer.
                a.partial_cmp(&b).unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|(suggestion, _)| suggestion.to_owned());
        Self {
            key: key.to_string(),
            context: Some(BadJsonPointerKeyContext { ty, suggestion }),
        }
    }
}

impl std::error::Error for BadJsonPointerKey {}

impl Display for BadJsonPointerKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.context {
            Some(BadJsonPointerKeyContext {
                ty,
                suggestion: Some(suggestion),
            }) => write!(
                f,
                "unknown key {:?} for value of {ty}; did you mean {suggestion:?}?",
                self.key
            ),
            Some(BadJsonPointerKeyContext {
                ty,
                suggestion: None,
            }) => write!(f, "unknown key {:?} for value of {ty}", self.key),
            None => write!(f, "unknown key {:?}", self.key),
        }
    }
}

#[derive(Debug)]
pub struct BadJsonPointerKeyContext {
    pub ty: JsonPointeeTy,
    pub suggestion: Option<String>,
}

/// An error that occurs when a pointer can't be resolved
/// against a value of the given type.
#[derive(Debug)]
pub struct BadJsonPointerTy {
    pub pointer: String,
    pub ty: Option<JsonPointeeTy>,
}

impl BadJsonPointerTy {
    pub fn new(pointer: &JsonPointer<'_>) -> Self {
        Self {
            pointer: pointer.to_string(),
            ty: None,
        }
    }

    #[cfg(feature = "did-you-mean")]
    #[cold]
    pub fn with_ty(pointer: &JsonPointer<'_>, ty: JsonPointeeTy) -> Self {
        Self {
            pointer: pointer.to_string(),
            ty: Some(ty),
        }
    }
}

impl std::error::Error for BadJsonPointerTy {}

impl Display for BadJsonPointerTy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.ty {
            Some(ty) => write!(f, "can't resolve {:?} against value of {ty}", self.pointer),
            None => write!(f, "can't resolve {:?}", self.pointer),
        }
    }
}

/// The name of a pointed-to type, for reporting traversal errors.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum JsonPointeeTy {
    Struct(JsonPointeeStructTy),
    Variant(&'static str, JsonPointeeStructTy),
    Named(&'static str),
}

impl JsonPointeeTy {
    #[inline]
    pub fn struct_named(ty: &'static str) -> Self {
        Self::Struct(JsonPointeeStructTy::Named(ty))
    }

    #[inline]
    pub fn tuple_struct_named(ty: &'static str) -> Self {
        Self::Struct(JsonPointeeStructTy::Tuple(ty))
    }

    #[inline]
    pub fn unit_struct_named(ty: &'static str) -> Self {
        Self::Struct(JsonPointeeStructTy::Unit(ty))
    }

    #[inline]
    pub fn struct_variant_named(ty: &'static str, variant: &'static str) -> Self {
        Self::Variant(ty, JsonPointeeStructTy::Named(variant))
    }

    #[inline]
    pub fn tuple_variant_named(ty: &'static str, variant: &'static str) -> Self {
        Self::Variant(ty, JsonPointeeStructTy::Tuple(variant))
    }

    #[inline]
    pub fn unit_variant_named(ty: &'static str, variant: &'static str) -> Self {
        Self::Variant(ty, JsonPointeeStructTy::Unit(variant))
    }

    #[inline]
    pub fn named<T: ?Sized>() -> Self {
        Self::Named(std::any::type_name::<T>())
    }

    #[inline]
    pub fn name_of<T: ?Sized>(value: &T) -> Self {
        Self::Named(std::any::type_name_of_val(value))
    }
}

impl Display for JsonPointeeTy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Struct(JsonPointeeStructTy::Named(ty)) => write!(f, "struct `{ty}`"),
            Self::Struct(JsonPointeeStructTy::Tuple(ty)) => write!(f, "tuple struct `{ty}`"),
            Self::Struct(JsonPointeeStructTy::Unit(ty)) => write!(f, "unit struct `{ty}`"),
            Self::Variant(ty, JsonPointeeStructTy::Named(variant)) => {
                write!(f, "variant `{variant}` of `{ty}`")
            }
            Self::Variant(ty, JsonPointeeStructTy::Tuple(variant)) => {
                write!(f, "tuple variant `{variant}` of `{ty}`")
            }
            Self::Variant(ty, JsonPointeeStructTy::Unit(variant)) => {
                write!(f, "unit variant `{variant}` of `{ty}`")
            }
            Self::Named(ty) => write!(f, "type `{ty}`"),
        }
    }
}

/// The name of a pointed-to struct type or enum variant,
/// for reporting traversal errors.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum JsonPointeeStructTy {
    Named(&'static str),
    Tuple(&'static str),
    Unit(&'static str),
}

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

    #[test]
    fn test_parse_pointer() {
        let pointer = JsonPointer::parse("/foo/bar/0").unwrap();
        let mut segments = pointer.into_segments();
        assert_eq!(segments.len(), 3);
        assert_eq!(segments.next(), Some(JsonPointerSegment::from_str("foo")));
        assert_eq!(segments.next(), Some(JsonPointerSegment::from_str("bar")));
        // `"0"` is parsed as a string segment, but implementations for `Vec`
        // and tuple structs will parse it as an index.
        assert_eq!(segments.next(), Some(JsonPointerSegment::from_str("0")));
        assert_eq!(segments.next(), None);
    }

    #[test]
    fn test_parse_pointer_escaping() {
        let pointer = JsonPointer::parse("/foo~1bar/baz~0qux").unwrap();
        let mut segments = pointer.into_segments();
        assert_eq!(segments.len(), 2);
        assert_eq!(
            segments.next(),
            Some(JsonPointerSegment::from_str("foo~1bar"))
        );
        assert_eq!(
            segments.next(),
            Some(JsonPointerSegment::from_str("baz~0qux"))
        );
        assert_eq!(segments.next(), None);
    }

    #[test]
    fn test_resolve_vec() {
        let data = vec![1, 2, 3];
        let pointer = JsonPointer::parse("/1").unwrap();
        let result = data.resolve(pointer).unwrap();
        assert_eq!(result.downcast_ref::<i32>(), Some(&2));
    }

    #[test]
    fn test_resolve_hashmap() {
        let mut data = HashMap::new();
        data.insert("foo".to_string(), 42);

        let pointer = JsonPointer::parse("/foo").unwrap();
        let result = data.resolve(pointer).unwrap();
        assert_eq!(result.downcast_ref::<i32>(), Some(&42));
    }

    #[test]
    fn test_resolve_option() {
        let data = Some(42);
        let pointer = JsonPointer::parse("").unwrap();
        let result = data.resolve(pointer).unwrap();
        assert_eq!(result.downcast_ref::<i32>(), Some(&42));
    }

    #[test]
    fn test_primitive_empty_path() {
        let data = 42;
        let pointer = JsonPointer::parse("").unwrap();
        let result = data.resolve(pointer).unwrap();
        assert_eq!(result.downcast_ref::<i32>(), Some(&42));
    }

    #[test]
    fn test_primitive_non_empty_path() {
        let data = 42;
        let pointer = JsonPointer::parse("/foo").unwrap();
        assert!(data.resolve(pointer).is_err());
    }

    #[test]
    fn test_segments() {
        let pointer = JsonPointer::parse("/foo/bar/baz").unwrap();

        // Can iterate multiple times with borrowing iterator.
        let segments: Vec<_> = pointer.segments().map(|s| s.as_str()).collect();
        assert_eq!(segments, vec!["foo", "bar", "baz"]);

        // Pointer is still usable after borrowing iteration.
        let segments_again: Vec<_> = pointer.segments().map(|s| s.as_str()).collect();
        assert_eq!(segments_again, vec!["foo", "bar", "baz"]);

        // Verify iterator traits.
        assert_eq!(pointer.segments().len(), 3);
        assert_eq!(pointer.segments().last().map(|s| s.as_str()), Some("baz"));
        assert_eq!(
            pointer.segments().next_back().map(|s| s.as_str()),
            Some("baz")
        );
    }
}