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
use std::convert;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Item {
path: Vec<Component>,
}
impl Item {
pub const fn empty() -> Self {
Self { path: Vec::new() }
}
pub fn new(path: Vec<Component>) -> Self {
Self { path }
}
pub fn of<I>(iter: I) -> Self
where
I: IntoIterator,
I::Item: Into<Component>,
{
Self {
path: iter.into_iter().map(Into::into).collect::<Vec<Component>>(),
}
}
pub fn is_empty(&self) -> bool {
self.path.is_empty()
}
pub fn push<C>(&mut self, component: C)
where
C: Into<Component>,
{
self.path.push(component.into());
}
pub fn pop(&mut self) -> Option<Component> {
self.path.pop()
}
pub fn as_vec(&self) -> Vec<Component> {
self.path.clone()
}
pub fn as_local(&self) -> Option<&str> {
match self.path.last() {
Some(Component::String(last)) if self.path.len() == 1 => Some(&*last),
_ => None,
}
}
pub fn join<I>(&self, other: I) -> Self
where
I: IntoIterator,
I::Item: Into<Component>,
{
let mut path = self.path.to_vec();
for part in other {
path.push(part.into());
}
Self::new(path)
}
pub fn extended<C>(&self, part: C) -> Self
where
C: Into<Component>,
{
let mut path = self.path.clone();
path.push(part.into());
Self::new(path)
}
pub fn last(&self) -> Option<&Component> {
self.path.last()
}
}
impl fmt::Display for Item {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut it = self.path.iter().peekable();
while let Some(part) = it.next() {
write!(fmt, "{}", part)?;
if it.peek().is_some() {
write!(fmt, "::")?;
}
}
Ok(())
}
}
impl<'a> IntoIterator for Item {
type IntoIter = std::vec::IntoIter<Component>;
type Item = Component;
fn into_iter(self) -> Self::IntoIter {
self.path.to_vec().into_iter()
}
}
impl<'a> IntoIterator for &'a Item {
type IntoIter = std::slice::Iter<'a, Component>;
type Item = &'a Component;
fn into_iter(self) -> Self::IntoIter {
self.path.iter()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Component {
String(String),
Block(usize),
Closure(usize),
AsyncBlock(usize),
}
impl fmt::Display for Component {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::String(s) => write!(fmt, "{}", s),
Self::Block(n) => write!(fmt, "$block{}", n),
Self::Closure(n) => write!(fmt, "$closure{}", n),
Self::AsyncBlock(n) => write!(fmt, "$async{}", n),
}
}
}
impl convert::AsRef<Component> for Component {
fn as_ref(&self) -> &Component {
self
}
}
impl From<&str> for Component {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}
impl From<&&str> for Component {
fn from(value: &&str) -> Self {
Self::String((*value).to_owned())
}
}
impl From<String> for Component {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&String> for Component {
fn from(value: &String) -> Self {
Self::String(value.clone())
}
}
impl From<&Component> for Component {
fn from(value: &Component) -> Self {
value.clone()
}
}