1use std::fmt;
2use std::slice;
3
4use itertools;
5
6use super::ScalarCow;
7
8#[derive(Clone, Debug, PartialEq)]
12pub struct Path<'s>(Vec<ScalarCow<'s>>);
13
14impl<'s> Path<'s> {
15 pub fn with_index<I: Into<ScalarCow<'s>>>(value: I) -> Self {
17 let indexes = vec![value.into()];
18 Path(indexes)
19 }
20
21 pub fn push<I: Into<ScalarCow<'s>>>(&mut self, value: I) {
23 self.0.push(value.into());
24 }
25
26 pub fn reserve(&mut self, additional: usize) {
32 self.0.reserve(additional);
33 }
34
35 pub fn iter(&self) -> PathIter<'_, '_> {
37 PathIter(self.0.iter())
38 }
39
40 #[inline]
42 pub fn as_slice(&self) -> &[ScalarCow<'s>] {
43 self.0.as_slice()
44 }
45}
46
47impl<'s> Extend<ScalarCow<'s>> for Path<'s> {
48 fn extend<T: IntoIterator<Item = ScalarCow<'s>>>(&mut self, iter: T) {
49 self.0.extend(iter);
50 }
51}
52
53impl<'s> ::std::ops::Deref for Path<'s> {
54 type Target = [ScalarCow<'s>];
55
56 #[inline]
57 fn deref(&self) -> &Self::Target {
58 &self.0
59 }
60}
61
62impl<'s> ::std::borrow::Borrow<[ScalarCow<'s>]> for Path<'s> {
63 #[inline]
64 fn borrow(&self) -> &[ScalarCow<'s>] {
65 self
66 }
67}
68
69impl<'s> AsRef<[ScalarCow<'s>]> for Path<'s> {
70 #[inline]
71 fn as_ref(&self) -> &[ScalarCow<'s>] {
72 self
73 }
74}
75
76impl<'s> fmt::Display for Path<'s> {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 let data = itertools::join(self.iter().map(ScalarCow::render), ".");
79 write!(f, "{}", data)
80 }
81}
82
83#[derive(Debug)]
85pub struct PathIter<'i, 's: 'i>(slice::Iter<'i, ScalarCow<'s>>);
86
87impl<'i, 's: 'i> Iterator for PathIter<'i, 's> {
88 type Item = &'i ScalarCow<'s>;
89
90 #[inline]
91 fn next(&mut self) -> Option<&'i ScalarCow<'s>> {
92 self.0.next()
93 }
94
95 #[inline]
96 fn size_hint(&self) -> (usize, Option<usize>) {
97 self.0.size_hint()
98 }
99
100 #[inline]
101 fn count(self) -> usize {
102 self.0.count()
103 }
104}
105
106impl<'i, 's: 'i> ExactSizeIterator for PathIter<'i, 's> {
107 #[inline]
108 fn len(&self) -> usize {
109 self.0.len()
110 }
111}
112
113pub type PathRef<'p, 's> = &'p [ScalarCow<'s>];