1use super::*;
2
3#[derive(Default, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
4pub struct Namepath<'src>(Vec<Name<'src>>);
5
6impl<'src> Namepath<'src> {
7 pub fn join(&self, name: Name<'src>) -> Self {
8 Self(self.0.iter().copied().chain(iter::once(name)).collect())
9 }
10
11 pub fn spaced(&self) -> ModulePath {
12 ModulePath {
13 path: self.0.iter().map(|name| name.lexeme().into()).collect(),
14 spaced: true,
15 }
16 }
17}
18
19impl<'src> Display for Namepath<'src> {
20 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
21 for (i, name) in self.0.iter().enumerate() {
22 if i > 0 {
23 write!(f, "::")?;
24 }
25 write!(f, "{name}")?;
26 }
27 Ok(())
28 }
29}
30
31impl<'src> Serialize for Namepath<'src> {
32 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
33 where
34 S: Serializer,
35 {
36 serializer.serialize_str(&format!("{self}"))
37 }
38}