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
use crate::prelude::*;
use crate::string::SharedString;
#[derive(Clone, Default)]
pub struct Path {
pub(super) components: im::Vector<SharedString>,
}
pub type Components<'a> = im::vector::Iter<'a, SharedString>;
pub type ComponentStrs<'a> = iter::Map<Components<'a>, fn(&'a SharedString) -> &'a str>;
impl Path {
pub fn components(&self) -> Components {
self.components.iter()
}
pub fn component_strs(&self) -> ComponentStrs {
self.components.iter().map(SharedString::as_str)
}
}
impl Debug for Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.components)
}
}
impl Display for Path {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.components.is_empty() {
return write!(f, "(root)");
}
let mut components = self.components.iter();
if let Some(first) = components.next() {
write!(f, "{}", first)?;
}
for component in components {
let first_char = match component.chars().next() {
Some(c) => c,
None => continue,
};
if first_char.is_whitespace() || [':', '(', '<'].contains(&first_char) {
write!(f, "{}", component)?;
} else {
write!(f, " {}", component)?;
}
}
Ok(())
}
}