use std::fmt::Display;
use std::str::FromStr;
#[derive(Clone, Debug)]
pub(crate) struct FieldPath {
segments: Vec<String>,
}
impl FieldPath {
pub fn root() -> Self {
FieldPath {
segments: Vec::new(),
}
}
pub fn with_segment(&self, rhs: &str) -> FieldPath {
let mut new_path = self.clone();
new_path.segments.push(rhs.to_string());
new_path
}
pub fn dotted_path(&self) -> String {
self.segments.join(".")
}
}
impl FromStr for FieldPath {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
Ok(FieldPath::root())
} else {
let segments = s
.split('.')
.map(|segment| segment.to_string())
.collect::<Vec<String>>();
Ok(FieldPath { segments })
}
}
}
impl Display for FieldPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.dotted_path())
}
}