Trait macro_tools::AttributeComponent
source · pub trait AttributeComponentwhere
Self: Sized,{
const KEYWORD: &'static str;
// Required method
fn from_meta(attr: &Attribute) -> Result<Self>;
}
Expand description
Trait for components of a structure aggregating attributes that can be constructed from a meta attribute.
The AttributeComponent
trait defines the interface for components that can be created
from a syn::Attribute
meta item. Implementors of this trait are required to define
a constant KEYWORD
that identifies the type of the component and a method from_meta
that handles the construction of the component from the given attribute.
This trait is designed to facilitate modular and reusable parsing of attributes applied to structs, enums, or other constructs. By implementing this trait, you can create specific components from attributes and then aggregate these components into a larger structure.
§Example
use macro_tools::{ AttributeComponent, Result };
use syn::{ Attribute, Error };
struct MyComponent;
impl AttributeComponent for MyComponent
{
const KEYWORD : &'static str = "my_component";
fn from_meta( attr : &Attribute ) -> Result<Self>
{
// Parsing logic here
// Return Ok(MyComponent) if parsing is successful
// Return Err(Error::new_spanned(attr, "error message")) if parsing fails
Ok( MyComponent )
}
}
§Parameters
attr
: A reference to thesyn::Attribute
from which the component is to be constructed.
§Returns
A Result
containing the constructed component if successful, or an error if the parsing fails.
Required Associated Constants§
Required Methods§
sourcefn from_meta(attr: &Attribute) -> Result<Self>
fn from_meta(attr: &Attribute) -> Result<Self>
Constructs the component from the given meta attribute.
This method is responsible for parsing the provided syn::Attribute
and
returning an instance of the component. If the attribute cannot be parsed
into the component, an error should be returned.
§Parameters
attr
: A reference to thesyn::Attribute
from which the component is to be constructed.
§Returns
A Result
containing the constructed component if successful, or an error if the parsing fails.