| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | /// Represents a sequence of segments in a JSON pointer. |
| 4 | /// |
| 5 | /// Used to track the path during JSON pointer resolution. |
| 6 | pub(crate) struct Segments<'a>(Vec<Segment<'a>>); |
| 7 | |
| 8 | impl<'a> Segments<'a> { |
| 9 | /// Creates a new, empty `Segments` instance. |
| 10 | pub(crate) fn new() -> Self { |
| 11 | Self(Vec::new()) |
| 12 | } |
| 13 | |
| 14 | /// Adds a new segment to the sequence. |
| 15 | pub(crate) fn push(&mut self, segment: impl Into<Segment<'a>>) { |
| 16 | self.0.push(segment.into()); |
| 17 | } |
| 18 | |
| 19 | /// Returns an iterator over the segments. |
| 20 | pub(crate) fn iter(&self) -> impl Iterator<Item = &Segment<'a>> { |
| 21 | self.0.iter() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// Represents a single segment in a JSON pointer. |
| 26 | #[derive(Debug, PartialEq, Eq, Clone)] |
| 27 | pub(crate) enum Segment<'a> { |
| 28 | /// A string key for object properties. |
| 29 | Key(Cow<'a, str>), |
| 30 | /// A numeric index for array elements. |
| 31 | Index(usize), |
| 32 | } |
| 33 | |
| 34 | impl<'a> From<Cow<'a, str>> for Segment<'a> { |
| 35 | fn from(value: Cow<'a, str>) -> Self { |
| 36 | Segment::Key(value) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl From<usize> for Segment<'_> { |
| 41 | fn from(value: usize) -> Self { |
| 42 | Segment::Index(value) |
| 43 | } |
| 44 | } |