mattro
is a proc_macro attribute parser for Rust.
Add this to your Cargo.toml:
[dependencies]
mattro = "0.1.1"
You can parse:
- Attribute
using MacroAttribute::new(attribute)
- AttributeArgs
using MacroAttribute::from_attribute_args(path, args, style)
Parsing AttributeArgs
:
main.rs
```rust
fn main() {} ```
lib.rs
```rust
use mattro::MacroAttribute;
use proc_macro::TokenStream;
pub fn myattribute(attribute: TokenStream, item: TokenStream) -> TokenStream { let tokens = attribute.clone(); let attributeargs: syn::AttributeArgs = syn::parsemacroinput!(tokens);
// Creates a `MacroAttribute` from `AttributeArgs`.
let attr = MacroAttribute::from_attribute_args(
// Path of the attribute
"my_attribute",
// The `AttributeArgs`
attribute_args,
// The attribute style `inner` or `outer`
syn::AttrStyle::Outer
);
// Prints all the `MetaItem`s
for meta_item in &attr {
println!("{:#?}", meta_item);
}
// Returns the decorated item
item
} ```
This prints out:
scala
NameValue(
NameValue {
name: "text",
value: Literal(
Str(
LitStr {
token: "some text",
},
),
),
},
)
NameValue(
NameValue {
name: "number",
value: Literal(
Int(
LitInt {
token: 120,
},
),
),
},
)
NameValue(
NameValue {
name: "array",
value: Array(
[
Int(
LitInt {
token: 1,
},
),
Int(
LitInt {
token: 2,
},
),
Int(
LitInt {
token: 3,
},
),
],
),
},
)
name-value
pairs``rust
// Converts the attribute into a
name-value` attribute
let namevaluesattributes = attr.intonamevalues().unwrap();
// Iterate over the name-value
pairs
for (name, value) in &namevaluesattributes {
println!("{:7} => {}", name, value);
}
```
This prints out:
js
text => "some text"
number => 120
array => [1, 2, 3]