1 use std::fmt;
2 use std::iter;
3 
4 use crate::gen::rust::component::RustPathComponent;
5 use crate::gen::rust::ident::RustIdent;
6 use crate::gen::rust::path::RustPath;
7 
8 #[derive(Default, Eq, PartialEq, Debug, Clone)]
9 pub(crate) struct RustRelativePath {
10     pub(crate) path: Vec<RustPathComponent>,
11 }
12 
13 impl RustRelativePath {
into_path(self) -> RustPath14     pub fn into_path(self) -> RustPath {
15         RustPath {
16             absolute: false,
17             path: self,
18         }
19     }
20 
_empty() -> RustRelativePath21     pub fn _empty() -> RustRelativePath {
22         RustRelativePath { path: Vec::new() }
23     }
24 
from_components<I: IntoIterator<Item = RustPathComponent>>(i: I) -> RustRelativePath25     pub fn from_components<I: IntoIterator<Item = RustPathComponent>>(i: I) -> RustRelativePath {
26         RustRelativePath {
27             path: i.into_iter().collect(),
28         }
29     }
30 
from_idents<I: IntoIterator<Item = RustIdent>>(i: I) -> RustRelativePath31     pub fn from_idents<I: IntoIterator<Item = RustIdent>>(i: I) -> RustRelativePath {
32         Self::from_components(i.into_iter().map(RustPathComponent::Ident))
33     }
34 
is_empty(&self) -> bool35     pub fn is_empty(&self) -> bool {
36         self.path.is_empty()
37     }
38 
first(&self) -> Option<RustPathComponent>39     pub fn first(&self) -> Option<RustPathComponent> {
40         self.path.iter().cloned().next()
41     }
42 
remove_first(&mut self) -> Option<RustPathComponent>43     pub fn remove_first(&mut self) -> Option<RustPathComponent> {
44         if self.path.is_empty() {
45             None
46         } else {
47             Some(self.path.remove(0))
48         }
49     }
50 
prepend_ident(&mut self, ident: RustIdent)51     pub fn prepend_ident(&mut self, ident: RustIdent) {
52         self.path.insert(0, RustPathComponent::Ident(ident));
53     }
54 
append(mut self, path: RustRelativePath) -> RustRelativePath55     pub fn append(mut self, path: RustRelativePath) -> RustRelativePath {
56         for c in path.path {
57             self.path.push(c);
58         }
59         self
60     }
61 
push_ident(&mut self, ident: RustIdent)62     pub fn push_ident(&mut self, ident: RustIdent) {
63         self.path.push(RustPathComponent::Ident(ident));
64     }
65 
append_ident(mut self, ident: RustIdent) -> RustRelativePath66     pub fn append_ident(mut self, ident: RustIdent) -> RustRelativePath {
67         self.push_ident(ident);
68         self
69     }
70 
to_reverse(&self) -> RustRelativePath71     pub fn to_reverse(&self) -> RustRelativePath {
72         RustRelativePath::from_components(
73             iter::repeat(RustPathComponent::SUPER).take(self.path.len()),
74         )
75     }
76 }
77 
78 impl fmt::Display for RustRelativePath {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result79     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80         for (i, c) in self.path.iter().enumerate() {
81             if i != 0 {
82                 write!(f, "::")?;
83             }
84             write!(f, "{}", c)?;
85         }
86         Ok(())
87     }
88 }
89 
90 impl From<&'_ str> for RustRelativePath {
from(s: &str) -> Self91     fn from(s: &str) -> Self {
92         assert!(!s.starts_with("::"), "path is absolute: {:?}", s);
93         RustRelativePath {
94             path: s.split("::").map(RustPathComponent::parse).collect(),
95         }
96     }
97 }
98