Module :: proc_macro_tools

Tools for writing procedural macros.
Example: Trivial One
The purpose of typ::type_parameters
is to extract type parameters from a given Rust type.
In this example, we generate a type core::option::Option<i8, i16, i32, i64>
and extract its type parameters.
#[ cfg( not( all( feature = "enabled", feature = "typ" ) ) ) ]
fn main(){}
#[ cfg( all( feature = "enabled", feature = "typ" ) ) ]
fn main()
{
use macro_tools::{ typ, qt };
let code = qt!( core::option::Option< i8, i16, i32, i64 > );
let tree_type = syn::parse2::< syn::Type >( code ).unwrap();
let got = typ::type_parameters( &tree_type, 0..=2 );
got.iter().for_each( | e | println!( "{}", qt!( #e ) ) );
}
Try out cargo run --example macro_tools_trivial
.
See code.
Example: Attribute Properties
This example demonstrates an approach to parsing attributes and their properties.
The attributes are collected into a struct that aggregates them, and attribute properties
are parsed using reusable components from a library. The example shows how to use
AttributePropertyBoolean
for parsing boolean properties and the roles of the traits
AttributePropertyComponent
and AttributeComponent
. The Assign
trait is
also used to simplify the logic of assigning fields.
Attributes are collected into a ItemAttributes
struct, and attribute properties are parsed
using reusable components like AttributePropertyBoolean
.
AttributeComponent
: A trait that defines how an attribute should be parsed from a syn::Attribute
.
AttributePropertyComponent
: A trait that defines a marker for attribute properties.
Assign
: A trait that simplifies the logic of assigning fields to a struct. Using a
component-based approach requires each field to have a unique type, which aligns with the
strengths of strongly-typed languages. This method ensures that the logic of
assigning values to fields is encapsulated within the fields themselves, promoting modularity
and reusability.
The reusable property components from the library come with parameters that distinguish
different properties of the same type. This is useful when an attribute has multiple boolean
properties, for instance. Such an approach helps to avoid limitations where it is
always possible to define traits for custom types, while it may not be possible for types
defined in other crates.
#[ cfg( not( all( feature = "enabled", feature = "attr_prop", debug_assertions ) ) ) ]
fn main(){}
#[ cfg( all( feature = "enabled", feature = "attr_prop", debug_assertions ) ) ]
fn main()
{
use macro_tools::
{
attr,
ct,
syn_err,
return_syn_err,
qt,
Result,
AttributeComponent,
AttributePropertyComponent,
AttributePropertyBoolean,
AttributePropertySingletone,
Assign,
};
#[ derive( Debug, Default ) ]
pub struct ItemAttributes
{
pub mutator : AttributeMutator,
}
impl ItemAttributes
{
pub fn from_attrs< 'a >( attrs : impl Iterator< Item = & 'a syn::Attribute > ) -> Result< Self >
{
let mut result = Self::default();
let error = | attr : & syn::Attribute | -> syn::Error
{
let known_attributes = ct::str::format!
(
"Known attributes are: {}, {}.",
"debug",
AttributeMutator::KEYWORD,
);
syn_err!
(
attr,
"Expects an attribute of format '#[ attribute( key1 = val1, key2 = val2 ) ]'\n {known_attributes}\n But got: '{}'",
qt! { #attr }
)
};
for attr in attrs
{
let key_ident = attr.path().get_ident().ok_or_else( || error( attr ) )?;
let key_str = format!( "{}", key_ident );
match key_str.as_ref()
{
AttributeMutator::KEYWORD => result.assign( AttributeMutator::from_meta( attr )? ),
"debug" => {},
_ => {},
}
}
Ok( result )
}
}
#[ derive( Debug, Default ) ]
pub struct AttributeMutator
{
pub custom : AttributePropertyCustom,
pub debug : AttributePropertyDebug,
}
impl AttributeComponent for AttributeMutator
{
const KEYWORD : & 'static str = "mutator";
fn from_meta( attr : & syn::Attribute ) -> Result< Self >
{
match attr.meta
{
syn::Meta::List( ref meta_list ) =>
{
return syn::parse2::< AttributeMutator >( meta_list.tokens.clone() );
},
syn::Meta::Path( ref _path ) =>
{
return Ok( Default::default() )
},
_ => return_syn_err!
(
attr,
"Expects an attribute of format `#[ mutator( custom = true ) ]`. \nGot: {}",
qt! { #attr }
),
}
}
}
impl< IntoT > Assign< AttributeMutator, IntoT > for ItemAttributes
where
IntoT : Into< AttributeMutator >,
{
#[ inline( always ) ]
fn assign( & mut self, component : IntoT )
{
self.mutator = component.into();
}
}
impl< IntoT > Assign< AttributePropertyDebug, IntoT > for AttributeMutator
where
IntoT : Into< AttributePropertyDebug >,
{
#[ inline( always ) ]
fn assign( & mut self, component : IntoT )
{
self.debug = component.into();
}
}
impl< IntoT > Assign< AttributePropertyCustom, IntoT > for AttributeMutator
where
IntoT : Into< AttributePropertyCustom >,
{
#[ inline( always ) ]
fn assign( & mut self, component : IntoT )
{
self.custom = component.into();
}
}
impl syn::parse::Parse for AttributeMutator
{
fn parse( input : syn::parse::ParseStream< '_ > ) -> syn::Result< Self >
{
let mut result = Self::default();
let error = | ident : & syn::Ident | -> syn::Error
{
let known = ct::str::format!
(
"Known entries of attribute {} are: {}, {}.",
AttributeMutator::KEYWORD,
AttributePropertyCustom::KEYWORD,
AttributePropertyDebug::KEYWORD,
);
syn_err!
(
ident,
r#"Expects an attribute of format '#[ mutator( custom = false ) ]'
{known}
But got: '{}'
"#,
qt! { #ident }
)
};
while !input.is_empty()
{
let lookahead = input.lookahead1();
if lookahead.peek( syn::Ident )
{
let ident : syn::Ident = input.parse()?;
match ident.to_string().as_str()
{
AttributePropertyCustom::KEYWORD => result.assign( AttributePropertyCustom::parse( input )? ),
AttributePropertyDebug::KEYWORD => result.assign( AttributePropertyDebug::from( true ) ),
_ => return Err( error( & ident ) ),
}
}
else
{
return Err( lookahead.error() );
}
if input.peek( syn::Token![,] )
{
input.parse::< syn::Token![,] >()?;
}
}
Ok( result )
}
}
#[ derive( Debug, Default, Clone, Copy ) ]
pub struct AttributePropertyDebugMarker;
impl AttributePropertyComponent for AttributePropertyDebugMarker
{
const KEYWORD : & 'static str = "debug";
}
pub type AttributePropertyDebug = AttributePropertySingletone< AttributePropertyDebugMarker >;
#[ derive( Debug, Default, Clone, Copy ) ]
pub struct AttributePropertyCustomMarker;
impl AttributePropertyComponent for AttributePropertyCustomMarker
{
const KEYWORD : & 'static str = "custom";
}
pub type AttributePropertyCustom = AttributePropertyBoolean< AttributePropertyCustomMarker >;
let input : syn::Attribute = syn::parse_quote!( #[ mutator( custom = true ) ] );
let attrs : ItemAttributes = ItemAttributes::from_attrs( std::iter::once( & input ) ).unwrap();
println!( "{:?}", attrs );
let attr : AttributePropertyBoolean< AttributePropertyDebugMarker > = AttributePropertyBoolean::default();
assert_eq!( attr.internal(), false );
let attr : AttributePropertyBoolean< AttributePropertyDebugMarker > = true.into();
assert_eq!( attr.internal(), true );
let attr : AttributePropertyBoolean< AttributePropertyDebugMarker > = false.into();
assert_eq!( attr.internal(), false );
}
Try out cargo run --example macro_tools_attr_prop
.
See code.
To add to your project
cargo add proc_macro_tools
Try out from the repository
git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/macro_tools_trivial
cargo run