• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::syntax::qualified::QualifiedName;
2 use quote::IdentFragment;
3 use std::fmt::{self, Display};
4 use std::iter::FromIterator;
5 use std::slice::Iter;
6 use syn::parse::{Parse, ParseStream, Result};
7 use syn::{Ident, Token};
8 
9 mod kw {
10     syn::custom_keyword!(namespace);
11 }
12 
13 #[derive(Clone, Default)]
14 pub struct Namespace {
15     segments: Vec<Ident>,
16 }
17 
18 impl Namespace {
19     pub const ROOT: Self = Namespace {
20         segments: Vec::new(),
21     };
22 
iter(&self) -> Iter<Ident>23     pub fn iter(&self) -> Iter<Ident> {
24         self.segments.iter()
25     }
26 
parse_bridge_attr_namespace(input: ParseStream) -> Result<Namespace>27     pub fn parse_bridge_attr_namespace(input: ParseStream) -> Result<Namespace> {
28         if input.is_empty() {
29             return Ok(Namespace::ROOT);
30         }
31 
32         input.parse::<kw::namespace>()?;
33         input.parse::<Token![=]>()?;
34         let namespace = input.parse::<Namespace>()?;
35         input.parse::<Option<Token![,]>>()?;
36         Ok(namespace)
37     }
38 }
39 
40 impl Default for &Namespace {
default() -> Self41     fn default() -> Self {
42         const ROOT: &Namespace = &Namespace::ROOT;
43         ROOT
44     }
45 }
46 
47 impl Parse for Namespace {
parse(input: ParseStream) -> Result<Self>48     fn parse(input: ParseStream) -> Result<Self> {
49         let segments = QualifiedName::parse_quoted_or_unquoted(input)?.segments;
50         Ok(Namespace { segments })
51     }
52 }
53 
54 impl Display for Namespace {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result55     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56         for segment in self {
57             write!(f, "{}$", segment)?;
58         }
59         Ok(())
60     }
61 }
62 
63 impl IdentFragment for Namespace {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result64     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65         Display::fmt(self, f)
66     }
67 }
68 
69 impl<'a> IntoIterator for &'a Namespace {
70     type Item = &'a Ident;
71     type IntoIter = Iter<'a, Ident>;
into_iter(self) -> Self::IntoIter72     fn into_iter(self) -> Self::IntoIter {
73         self.iter()
74     }
75 }
76 
77 impl<'a> FromIterator<&'a Ident> for Namespace {
from_iter<I>(idents: I) -> Self where I: IntoIterator<Item = &'a Ident>,78     fn from_iter<I>(idents: I) -> Self
79     where
80         I: IntoIterator<Item = &'a Ident>,
81     {
82         let segments = idents.into_iter().cloned().collect();
83         Namespace { segments }
84     }
85 }
86