1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::util;
#[derive(Clone, Debug, PartialEq)]
pub enum PathElement {
Field(String),
Repeated(String, usize),
Variant(String, String),
Index(usize),
}
impl std::fmt::Display for PathElement {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if !f.alternate() {
match self {
PathElement::Index(_) => {}
_ => write!(f, ".")?,
}
}
match self {
PathElement::Field(field) => write!(f, "{}", util::string::as_ident_or_string(field)),
PathElement::Repeated(field, index) => {
write!(f, "{}[{index}]", util::string::as_ident_or_string(field))
}
PathElement::Variant(field, variant) => write!(
f,
"{}<{}>",
util::string::as_ident_or_string(field),
util::string::as_ident_or_string(variant)
),
PathElement::Index(index) => write!(f, "[{index}]"),
}
}
}
impl PathElement {
pub fn to_string_without_dot(&self) -> String {
format!("{:#}", self)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PathBuf {
pub root: &'static str,
pub elements: Vec<PathElement>,
}
impl std::fmt::Display for PathBuf {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.root)?;
for element in self.elements.iter() {
write!(f, "{element}")?;
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Path<'a> {
Root(&'static str),
Select(&'a Path<'a>, PathElement),
}
impl Default for Path<'_> {
fn default() -> Self {
Path::Root("")
}
}
impl Path<'_> {
pub fn with(&self, element: PathElement) -> Path {
Path::Select(self, element)
}
pub fn with_field<S: Into<String>>(&self, name: S) -> Path {
self.with(PathElement::Field(name.into()))
}
pub fn with_repeated<S: Into<String>>(&self, name: S, index: usize) -> Path {
self.with(PathElement::Repeated(name.into(), index))
}
pub fn with_variant<S: Into<String>, V: Into<String>>(&self, name: S, variant: V) -> Path {
self.with(PathElement::Variant(name.into(), variant.into()))
}
pub fn with_index(&self, index: usize) -> Path {
self.with(PathElement::Index(index))
}
}
impl std::fmt::Display for Path<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Path::Root(name) => write!(f, "{name}"),
Path::Select(parent, element) => write!(f, "{parent}{element}"),
}
}
}
impl Path<'_> {
pub fn end_to_string(&self) -> String {
match self {
Path::Root(name) => name.to_string(),
Path::Select(_, element) => element.to_string(),
}
}
pub fn to_path_buf(&self) -> PathBuf {
match self {
Path::Root(name) => PathBuf {
root: name,
elements: vec![],
},
Path::Select(parent, element) => {
let mut parent = parent.to_path_buf();
parent.elements.push(element.clone());
parent
}
}
}
}
impl From<Path<'_>> for PathBuf {
fn from(path: Path<'_>) -> Self {
path.to_path_buf()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paths() {
let a = Path::Root("a");
let b = a.with_field("b");
let c = b.with_repeated("c", 42);
let d = c.with_variant("d", "e");
let e = d.with_index(33);
let buf: PathBuf = e.to_path_buf();
assert_eq!(e.to_string(), "a.b.c[42].d<e>[33]");
assert_eq!(buf.to_string(), "a.b.c[42].d<e>[33]");
}
#[test]
fn non_ident_paths() {
let a = Path::Root("a");
let b = a.with_field("4");
let c = b.with_repeated("8", 15);
let d = c.with_variant("16", "23");
let e = d.with_index(42);
let buf: PathBuf = e.to_path_buf();
assert_eq!(e.to_string(), "a.\"4\".\"8\"[15].\"16\"<\"23\">[42]");
assert_eq!(buf.to_string(), "a.\"4\".\"8\"[15].\"16\"<\"23\">[42]");
}
}