| David Tolnay | 754e21c | 2020-03-29 20:58:46 -0700 | [diff] [blame] | 1 | use std::fmt::{self, Display}; |
| 2 | use std::slice::Iter; |
| David Tolnay | 754e21c | 2020-03-29 20:58:46 -0700 | [diff] [blame] | 3 | |
| 4 | #[derive(Clone)] |
| 5 | pub struct Namespace { |
| 6 | segments: Vec<String>, |
| 7 | } |
| 8 | |
| 9 | impl Namespace { |
| 10 | pub fn new(segments: Vec<String>) -> Self { |
| 11 | Namespace { segments } |
| 12 | } |
| 13 | |
| 14 | pub fn iter(&self) -> Iter<String> { |
| 15 | self.segments.iter() |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl Display for Namespace { |
| 20 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 21 | for segment in self { |
| 22 | f.write_str(segment)?; |
| 23 | f.write_str("$")?; |
| 24 | } |
| 25 | Ok(()) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl<'a> IntoIterator for &'a Namespace { |
| 30 | type Item = &'a String; |
| 31 | type IntoIter = Iter<'a, String>; |
| 32 | fn into_iter(self) -> Self::IntoIter { |
| 33 | self.iter() |
| 34 | } |
| 35 | } |