pub( crate ) mod private
{
use crate::exposed::*;
use type_constructor::prelude::*;
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub struct Pair< T1, T2 >
( pub T1, pub T2 )
where
T1 : syn::parse::Parse + quote::ToTokens,
T2 : syn::parse::Parse + quote::ToTokens,
;
impl< T1, T2 > Pair< T1, T2 >
where
T1 : syn::parse::Parse + quote::ToTokens,
T2 : syn::parse::Parse + quote::ToTokens,
{
pub fn new( src1 : T1, src2 : T2 ) -> Self
{
Self( src1, src2 )
}
}
impl< T1, T2 > syn::parse::Parse for Pair< T1, T2 >
where
T1 : syn::parse::Parse + quote::ToTokens,
T2 : syn::parse::Parse + quote::ToTokens,
{
fn parse( input : ParseStream< '_ > ) -> Result< Self >
{
Ok( Self( input.parse()?, input.parse()? ) )
}
}
impl< T1, T2 > quote::ToTokens for Pair< T1, T2 >
where
T1 : syn::parse::Parse + quote::ToTokens,
T2 : syn::parse::Parse + quote::ToTokens,
{
fn to_tokens( &self, tokens : &mut proc_macro2::TokenStream )
{
self.0.to_tokens( tokens );
self.1.to_tokens( tokens );
}
}
types!
{
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub many Many : < T : quote::ToTokens >
}
impl< T > Many< T >
where
T : quote::ToTokens,
{
pub fn new() -> Self
{
Self( Vec::new() )
}
pub fn new_with( src : Vec< T > ) -> Self
{
Self( src )
}
}
impl< T > quote::ToTokens
for Many< T >
where
T : quote::ToTokens,
{
fn to_tokens( &self, tokens : &mut proc_macro2::TokenStream )
{
use crate::quote::TokenStreamExt;
tokens.append_all( self.0.iter() );
}
}
impl< T > From< Many< T > > for Vec< T >
where
T : quote::ToTokens,
{
fn from( src : Many< T > ) -> Self
{
src.0
}
}
impl syn::parse::Parse
for Many< AttributeInner >
{
fn parse( input : ParseStream< '_ > ) -> Result< Self >
{
let mut result = Self::new();
loop
{
let lookahead = input.lookahead1();
if !lookahead.peek( Token![ # ] )
{
break;
}
result.0.push( input.parse()? );
}
Ok( result )
}
}
impl syn::parse::Parse
for Many< AttributesOuter >
{
fn parse( input : ParseStream< '_ > ) -> Result< Self >
{
let mut result = Self::new();
loop
{
let lookahead = input.lookahead1();
if !lookahead.peek( Token![ # ] )
{
break;
}
result.0.push( input.parse()? );
}
Ok( result )
}
}
impl syn::parse::Parse
for Many< syn::Item >
{
fn parse( input : syn::parse::ParseStream< '_ > ) -> Result< Self >
{
let mut items = vec![];
while !input.is_empty()
{
let item : syn::Item = input.parse()?;
items.push( item );
}
Ok( Self( items ) )
}
}
types!
{
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub many AttributeInner : syn::Attribute;
}
impl syn::parse::Parse
for AttributeInner
{
fn parse( input : ParseStream< '_ > ) -> Result< Self >
{
let input2;
Ok( Self( vec![ syn::Attribute
{
pound_token : input.parse()?,
style : syn::AttrStyle::Inner( input.parse()? ),
bracket_token : bracketed!( input2 in input ),
path : input2.call( syn::Path::parse_mod_style )?,
tokens : input2.parse()?,
}]))
}
}
impl quote::ToTokens
for AttributeInner
{
fn to_tokens( &self, tokens : &mut proc_macro2::TokenStream )
{
use crate::quote::TokenStreamExt;
tokens.append_all( self.0.iter() );
}
}
types!
{
#[ derive( Debug, PartialEq, Eq, Clone ) ]
pub many AttributesOuter : syn::Attribute;
}
impl syn::parse::Parse
for AttributesOuter
{
fn parse( input : ParseStream< '_ > ) -> Result< Self >
{
let mut result : Self = make!();
loop
{
let lookahead = input.lookahead1();
if !lookahead.peek( Token![ # ] )
{
break;
}
let input2;
let element = syn::Attribute
{
pound_token : input.parse()?,
style : syn::AttrStyle::Outer,
bracket_token : bracketed!( input2 in input ),
path : input2.call( syn::Path::parse_mod_style )?,
tokens : input2.parse()?,
};
result.0.push( element );
}
Ok( result )
}
}
impl quote::ToTokens
for AttributesOuter
{
fn to_tokens( &self, tokens : &mut proc_macro2::TokenStream )
{
use crate::quote::TokenStreamExt;
tokens.append_all( self.0.iter() );
}
}
pub type AttributedIdent = Pair< Many< AttributeInner >, syn::Ident >;
impl From< syn::Ident > for AttributedIdent
{
fn from( src : syn::Ident ) -> Self
{
Self( Vec::< AttributeInner >::new().into(), src )
}
}
impl From< AttributedIdent > for syn::Ident
{
fn from( src : AttributedIdent ) -> Self
{
src.1
}
}
}
pub mod exposed
{
pub use super::prelude::*;
pub use super::private::
{
Pair,
Many,
AttributeInner,
AttributesOuter,
AttributedIdent,
};
}
pub use exposed::*;
pub mod prelude
{
}