1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
//! # mattro-rs //! //! `mattro` is a **proc_macro** attribute parser for Rust. //! //! ## Usage //! Add this to your Cargo.toml: //! ``` //! [dependencies] //! mattro = "0.1.2" //! ``` //! //! You can parse: //! - `Attribute` using `MacroAttribute::new(attribute)` //! - `AttributeArgs` using `MacroAttribute::from_attribute_args(path, args, style)` //! //! ## Example //! Parsing `AttributeArgs`: //! //! `main.rs` //! ```rust //! #[my_attribute(text="some text", number=120, array=1,2,3)] //! fn main() {} //! ``` //! //! `lib.rs` //! ```rust //! use mattro::MacroAttribute; //! use proc_macro::TokenStream; //! //! #[proc_macro_attribute] //! pub fn my_attribute(attribute: TokenStream, item: TokenStream) -> TokenStream { //! let tokens = attribute.clone(); //! let attribute_args: syn::AttributeArgs = syn::parse_macro_input!(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, //! }, //! ), //! ], //! ), //! }, //! ) //! ``` //! //! ### You could convert the attribute into a `name-value` pairs //! //! ```rust //! // Converts the attribute into a `name-value` attribute //! let name_values_attributes = attr.into_name_values().unwrap(); //! //! // Iterate over the `name-value` pairs //! for (name, value) in &name_values_attributes { //! println!("{:7} => {}", name, value); //! } //! ``` //! //! This prints out: //! ```js //! text => "some text" //! number => 120 //! array => [1, 2, 3] //! ``` mod macro_attr; mod name_value_attr; mod visitor; mod name_value; pub use crate::{ macro_attr::*, name_value_attr::*, name_value::* };