use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, Eq, Hash, facet::Facet)]
#[repr(u8)]
pub enum PathSegment {
Field(Cow<'static, str>),
Index(usize),
Key(Cow<'static, str>),
Variant(Cow<'static, str>),
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, facet::Facet)]
pub struct Path(pub Vec<PathSegment>);
impl Path {
pub const fn new() -> Self {
Self(Vec::new())
}
pub fn push(&mut self, segment: PathSegment) {
self.0.push(segment);
}
pub fn with(&self, segment: PathSegment) -> Self {
let mut new = self.clone();
new.push(segment);
new
}
}
impl core::fmt::Display for Path {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
for (i, segment) in self.0.iter().enumerate() {
match segment {
PathSegment::Field(name) => {
if i > 0 {
write!(f, ".")?;
}
write!(f, "{}", name)?;
}
PathSegment::Index(idx) => {
if i > 0 {
write!(f, ".")?;
}
write!(f, "{}", idx)?;
}
PathSegment::Key(key) => write!(f, "[{:?}]", key)?,
PathSegment::Variant(name) => write!(f, "::{}", name)?,
}
}
Ok(())
}
}