use super::*;
use delimited::Delimited;
ast_struct! {
pub struct Variant {
pub ident: Ident,
pub attrs: Vec<Attribute>,
pub data: VariantData,
pub discriminant: Option<Expr>,
pub eq_token: Option<tokens::Eq>,
}
}
ast_enum! {
pub enum VariantData {
Struct(Delimited<Field, tokens::Comma>, tokens::Brace),
Tuple(Delimited<Field, tokens::Comma>, tokens::Paren),
Unit,
}
}
impl VariantData {
}
ast_struct! {
pub struct Field {
pub ident: Option<Ident>,
pub vis: Visibility,
pub attrs: Vec<Attribute>,
pub ty: Ty,
pub colon_token: Option<tokens::Colon>,
}
}
ast_enum_of_structs! {
pub enum Visibility {
pub Public(VisPublic {
pub pub_token: tokens::Pub,
}),
pub Crate(VisCrate {
pub pub_token: tokens::Pub,
pub paren_token: tokens::Paren,
pub crate_token: tokens::Crate,
}),
pub Restricted(VisRestricted {
pub pub_token: tokens::Pub,
pub paren_token: tokens::Paren,
pub in_token: Option<tokens::In>,
pub path: Box<Path>,
}),
pub Inherited(VisInherited {}),
}
}
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
use synom::Synom;
use synom::tokens;
use synom::tokens::*;
impl Field {
named!(pub parse_struct -> Self, do_parse!(
attrs: many0!(call!(Attribute::parse_outer)) >>
vis: syn!(Visibility) >>
id: syn!(Ident) >>
colon: syn!(Colon) >>
ty: syn!(Ty) >>
(Field {
ident: Some(id),
vis: vis,
attrs: attrs,
ty: ty,
colon_token: Some(colon),
})
));
named!(pub parse_tuple -> Self, do_parse!(
attrs: many0!(call!(Attribute::parse_outer)) >>
vis: syn!(Visibility) >>
ty: syn!(Ty) >>
(Field {
ident: None,
colon_token: None,
vis: vis,
attrs: attrs,
ty: ty,
})
));
}
impl Synom for Visibility {
named!(parse -> Self, alt!(
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(syn!(tokens::Crate)) >>
(Visibility::Crate(VisCrate {
crate_token: other.0,
paren_token: other.1,
pub_token: pub_token,
}))
)
|
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(syn!(Self_)) >>
(Visibility::Restricted(VisRestricted {
path: Box::new(other.0.into()),
in_token: None,
paren_token: other.1,
pub_token: pub_token,
}))
)
|
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(syn!(Super)) >>
(Visibility::Restricted(VisRestricted {
path: Box::new(other.0.into()),
in_token: None,
paren_token: other.1,
pub_token: pub_token,
}))
)
|
do_parse!(
pub_token: syn!(Pub) >>
other: parens!(do_parse!(
in_tok: syn!(In) >>
restricted: call!(Path::parse_mod_style) >>
(in_tok, restricted)
)) >>
(Visibility::Restricted(VisRestricted {
path: Box::new((other.0).1),
in_token: Some((other.0).0),
paren_token: other.1,
pub_token: pub_token,
}))
)
|
syn!(Pub) => { |tok| {
Visibility::Public(VisPublic {
pub_token: tok,
})
} }
|
epsilon!() => { |_| Visibility::Inherited(VisInherited {}) }
));
}
}
#[cfg(feature = "printing")]
mod printing {
use super::*;
use quote::{Tokens, ToTokens};
impl ToTokens for Variant {
fn to_tokens(&self, tokens: &mut Tokens) {
tokens.append_all(&self.attrs);
self.ident.to_tokens(tokens);
self.data.to_tokens(tokens);
if let Some(ref disc) = self.discriminant {
TokensOrDefault(&self.eq_token).to_tokens(tokens);
disc.to_tokens(tokens);
}
}
}
impl ToTokens for VariantData {
fn to_tokens(&self, tokens: &mut Tokens) {
match *self {
VariantData::Struct(ref fields, ref brace) => {
brace.surround(tokens, |tokens| {
fields.to_tokens(tokens);
});
}
VariantData::Tuple(ref fields, ref paren) => {
paren.surround(tokens, |tokens| {
fields.to_tokens(tokens);
});
}
VariantData::Unit => {}
}
}
}
impl ToTokens for Field {
fn to_tokens(&self, tokens: &mut Tokens) {
tokens.append_all(&self.attrs);
self.vis.to_tokens(tokens);
if let Some(ref ident) = self.ident {
ident.to_tokens(tokens);
TokensOrDefault(&self.colon_token).to_tokens(tokens);
}
self.ty.to_tokens(tokens);
}
}
impl ToTokens for VisPublic {
fn to_tokens(&self, tokens: &mut Tokens) {
self.pub_token.to_tokens(tokens)
}
}
impl ToTokens for VisCrate {
fn to_tokens(&self, tokens: &mut Tokens) {
self.pub_token.to_tokens(tokens);
self.paren_token.surround(tokens, |tokens| {
self.crate_token.to_tokens(tokens);
})
}
}
impl ToTokens for VisRestricted {
fn to_tokens(&self, tokens: &mut Tokens) {
self.pub_token.to_tokens(tokens);
self.paren_token.surround(tokens, |tokens| {
self.in_token.to_tokens(tokens);
self.path.to_tokens(tokens);
});
}
}
impl ToTokens for VisInherited {
fn to_tokens(&self, _tokens: &mut Tokens) {
}
}
}