-
Notifications
You must be signed in to change notification settings - Fork 499
/
namepath.rs
38 lines (33 loc) · 877 Bytes
/
namepath.rs
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
use super::*;
#[derive(Default, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) struct Namepath<'src>(Vec<Name<'src>>);
impl<'src> Namepath<'src> {
pub(crate) fn join(&self, name: Name<'src>) -> Self {
Self(self.0.iter().copied().chain(iter::once(name)).collect())
}
pub(crate) fn spaced(&self) -> ModulePath {
ModulePath {
path: self.0.iter().map(|name| name.lexeme().into()).collect(),
spaced: true,
}
}
}
impl Display for Namepath<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
for (i, name) in self.0.iter().enumerate() {
if i > 0 {
write!(f, "::")?;
}
write!(f, "{name}")?;
}
Ok(())
}
}
impl Serialize for Namepath<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("{self}"))
}
}