gltf_v1_json/
path.rs

1use std::fmt;
2
3/// An immutable JSON source path.
4#[derive(Default, Clone, Debug, PartialEq)]
5pub struct Path(pub String);
6
7impl Path {
8    pub fn new() -> Self {
9        Path(String::new())
10    }
11
12    pub fn field(&self, name: &str) -> Self {
13        if self.0.is_empty() {
14            Path(name.to_string())
15        } else {
16            Path(format!("{}.{}", self.0, name))
17        }
18    }
19
20    pub fn index(&self, index: usize) -> Self {
21        Path(format!("{}[{}]", self.0, index))
22    }
23
24    pub fn key(&self, key: &str) -> Self {
25        Path(format!("{}[\"{}\"]", self.0, key))
26    }
27
28    pub fn value_str(&self, value: &str) -> Self {
29        Path(format!("{} = \"{}\"", self.0, value))
30    }
31
32    pub fn as_str(&self) -> &str {
33        &self.0
34    }
35}
36
37impl fmt::Display for Path {
38    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39        write!(f, "{}", self.0)
40    }
41}