| 1 |
use std::fmt::{self, Display}; |
| 2 |
use std::slice; |
| 3 |
|
| 4 |
use super::Chain; |
| 5 |
|
| 6 |
/// Path to the error value in the input, like `dependencies.serde.typo1`. |
| 7 |
/// |
| 8 |
/// Use `path.to_string()` to get a string representation of the path with |
| 9 |
/// segments separated by periods, or use `path.iter()` to iterate over |
| 10 |
/// individual segments of the path. |
| 11 |
#[derive(Clone, Debug)] |
| 12 |
pub struct Path { |
| 13 |
segments: Vec<Segment>, |
| 14 |
} |
| 15 |
|
| 16 |
/// Single segment of a path. |
| 17 |
#[derive(Clone, Debug)] |
| 18 |
pub enum Segment { |
| 19 |
Seq { index: usize }, |
| 20 |
Map { key: String }, |
| 21 |
Enum { variant: String }, |
| 22 |
Unknown, |
| 23 |
} |
| 24 |
|
| 25 |
impl Path { |
| 26 |
/// Returns an iterator with element type [`&Segment`][Segment]. |
| 27 |
pub fn iter(&self) -> Segments { |
| 28 |
Segments { |
| 29 |
iter: self.segments.iter(), |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl<'a> IntoIterator for &'a Path { |
| 35 |
type Item = &'a Segment; |
| 36 |
type IntoIter = Segments<'a>; |
| 37 |
|
| 38 |
fn into_iter(self) -> Self::IntoIter { |
| 39 |
self.iter() |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// Iterator over segments of a path. |
| 44 |
pub struct Segments<'a> { |
| 45 |
iter: slice::Iter<'a, Segment>, |
| 46 |
} |
| 47 |
|
| 48 |
impl<'a> Iterator for Segments<'a> { |
| 49 |
type Item = &'a Segment; |
| 50 |
|
| 51 |
fn next(&mut self) -> Option<Self::Item> { |
| 52 |
self.iter.next() |
| 53 |
} |
| 54 |
|
| 55 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 56 |
self.iter.size_hint() |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl<'a> DoubleEndedIterator for Segments<'a> { |
| 61 |
fn next_back(&mut self) -> Option<Self::Item> { |
| 62 |
self.iter.next_back() |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl<'a> ExactSizeIterator for Segments<'a> { |
| 67 |
fn len(&self) -> usize { |
| 68 |
self.iter.len() |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl Display for Path { |
| 73 |
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 74 |
if self.segments.is_empty() { |
| 75 |
return formatter.write_str("."); |
| 76 |
} |
| 77 |
|
| 78 |
let mut separator = ""; |
| 79 |
for segment in self { |
| 80 |
match segment { |
| 81 |
Segment::Seq { index } => { |
| 82 |
write!(formatter, "[{}]", index)?; |
| 83 |
} |
| 84 |
Segment::Map { key } | Segment::Enum { variant: key } => { |
| 85 |
write!(formatter, "{}{}", separator, key)?; |
| 86 |
} |
| 87 |
Segment::Unknown => { |
| 88 |
write!(formatter, "{}?", separator)?; |
| 89 |
} |
| 90 |
} |
| 91 |
separator = "."; |
| 92 |
} |
| 93 |
|
| 94 |
Ok(()) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
impl Path { |
| 99 |
pub(crate) fn empty() -> Self { |
| 100 |
Path { |
| 101 |
segments: Vec::new(), |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
pub(crate) fn from_chain(mut chain: &Chain) -> Self { |
| 106 |
let mut segments = Vec::new(); |
| 107 |
loop { |
| 108 |
match chain { |
| 109 |
Chain::Root => break, |
| 110 |
Chain::Seq { parent, index } => { |
| 111 |
segments.push(Segment::Seq { index: *index }); |
| 112 |
chain = parent; |
| 113 |
} |
| 114 |
Chain::Map { parent, key } => { |
| 115 |
segments.push(Segment::Map { key: key.clone() }); |
| 116 |
chain = parent; |
| 117 |
} |
| 118 |
Chain::Struct { parent, key } => { |
| 119 |
let key = *key; |
| 120 |
segments.push(Segment::Map { |
| 121 |
key: key.to_owned(), |
| 122 |
}); |
| 123 |
chain = parent; |
| 124 |
} |
| 125 |
Chain::Enum { parent, variant } => { |
| 126 |
segments.push(Segment::Enum { |
| 127 |
variant: variant.clone(), |
| 128 |
}); |
| 129 |
chain = parent; |
| 130 |
} |
| 131 |
Chain::Some { parent } |
| 132 |
| Chain::NewtypeStruct { parent } |
| 133 |
| Chain::NewtypeVariant { parent } => { |
| 134 |
chain = parent; |
| 135 |
} |
| 136 |
Chain::NonStringKey { parent } => { |
| 137 |
segments.push(Segment::Unknown); |
| 138 |
chain = parent; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
segments.reverse(); |
| 143 |
Path { segments } |
| 144 |
} |
| 145 |
|
| 146 |
pub(crate) fn is_only_unknown(&self) -> bool { |
| 147 |
self.segments.iter().all(Segment::is_unknown) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
impl Segment { |
| 152 |
fn is_unknown(&self) -> bool { |
| 153 |
match self { |
| 154 |
Segment::Unknown => true, |
| 155 |
_ => false, |
| 156 |
} |
| 157 |
} |
| 158 |
} |