cxx/syntax/namespace.rs

66 lines
1.4 KiB
Rust
Raw Normal View History

2020-09-06 23:00:17 -07:00
use crate::syntax::qualified::QualifiedName;
2020-04-19 20:56:09 -07:00
use quote::IdentFragment;
2020-03-29 20:58:46 -07:00
use std::fmt::{self, Display};
use std::slice::Iter;
2020-04-19 20:56:09 -07:00
use syn::parse::{Parse, ParseStream, Result};
2020-09-06 23:00:17 -07:00
use syn::{Ident, Token};
2020-04-19 20:56:09 -07:00
mod kw {
syn::custom_keyword!(namespace);
}
2020-03-29 20:58:46 -07:00
#[derive(Clone)]
pub struct Namespace {
2020-04-30 20:40:20 -07:00
segments: Vec<Ident>,
2020-03-29 20:58:46 -07:00
}
impl Namespace {
2020-04-19 20:56:09 -07:00
pub fn none() -> Self {
Namespace {
segments: Vec::new(),
}
2020-03-29 20:58:46 -07:00
}
2020-04-30 20:40:20 -07:00
pub fn iter(&self) -> Iter<Ident> {
2020-03-29 20:58:46 -07:00
self.segments.iter()
}
}
2020-04-19 20:56:09 -07:00
impl Parse for Namespace {
fn parse(input: ParseStream) -> Result<Self> {
let mut segments = Vec::new();
if !input.is_empty() {
input.parse::<kw::namespace>()?;
input.parse::<Token![=]>()?;
segments = input
.call(QualifiedName::parse_quoted_or_unquoted)?
.segments;
2020-04-19 20:56:09 -07:00
input.parse::<Option<Token![,]>>()?;
}
Ok(Namespace { segments })
}
}
2020-03-29 20:58:46 -07:00
impl Display for Namespace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for segment in self {
2020-04-30 20:40:20 -07:00
write!(f, "{}$", segment)?;
2020-03-29 20:58:46 -07:00
}
Ok(())
}
}
2020-04-19 20:56:09 -07:00
impl IdentFragment for Namespace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
2020-03-29 20:58:46 -07:00
impl<'a> IntoIterator for &'a Namespace {
2020-04-30 20:40:20 -07:00
type Item = &'a Ident;
type IntoIter = Iter<'a, Ident>;
2020-03-29 20:58:46 -07:00
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}