|
1 | 1 | # Attribution |
2 | 2 |
|
3 | | -_This crate is a work in progress. This README will be valid once it is complete._ |
| 3 | +Attribution is a crate that makes parsing attribute-style procedural macros as |
| 4 | +easy as declaring a struct. |
4 | 5 |
|
5 | | -Attribution is a crate containing macros that are designed to help in the |
6 | | -development of attribute style procedural macros. Specifically it is designed to |
7 | | -help in the parsing of attribute arguments. Attribution is able to do this |
8 | | -without the user having to know how to parse the attribute arguments. |
9 | | -Attribution's goal is to make parsing the attribute as easy as declaring its |
10 | | -parameters |
| 6 | +## Example Usage |
11 | 7 |
|
12 | | -## Attribution in Action |
13 | | - |
14 | | -Here's a quick example how Attribution helps make implementing attribute style |
15 | | -proc macros easier. For this example, a simple attribute called `ez_log` will be |
16 | | -used. The purpose of the attribute is to log a message at the beginning of the |
17 | | -function call. The actual implementation of the proc macro won't be written but |
18 | | -the logic to parse it attribute arguments using Attribution will be shown. |
19 | | - |
20 | | -**DISCLAIMER:** At the time of this writing `ez_log` is not an existing rust |
21 | | -crate. If that should change in the future and you (as the owner) would like me |
22 | | -to change this example, please feel free to open an issue. |
23 | | - |
24 | | -Let the following be an example usage of this example macro. |
| 8 | +Let's see it in action. Imagine that we want to create a procedural macro |
| 9 | +attribute called `ez_trace`. That adds a custom message to the beginning and |
| 10 | +end of a method call. See the example below for how the usage of this attribute |
| 11 | +would appear. |
25 | 12 |
|
26 | 13 | ```rust |
27 | | -use ez_log::ez_log; |
28 | | - |
29 | | -#[ez_log(msg = "inside my_function")] |
30 | | -fn my_function(a: i32, b: i32) -> i32 { |
31 | | - a + b |
| 14 | +#[ez_trace(start = "Starting...", end = "Ending...")] |
| 15 | +fn my_func() { |
| 16 | + println!("Do some work"); |
32 | 17 | } |
33 | 18 | ``` |
34 | 19 |
|
35 | | -The attribute takes a single parameter, `msg` which is a string. To parse the |
36 | | -argument within the proc macro write the following. |
| 20 | +In order to parse such an attribute we would need to add the following code to |
| 21 | +our hypothetical procedural macro crate. |
37 | 22 |
|
| 23 | +**lib.rs** |
38 | 24 | ```rust |
39 | | -use attribution::attribute_args; |
40 | | -use proc_macro::TokenStream; |
41 | | -use syn::parse_macro_input; |
42 | | - |
43 | | -#[attribute_args] |
44 | | -struct EzLogArgs { |
45 | | - msg: &'static str |
| 25 | +#[attr_args] |
| 26 | +struct EzTraceArgs { |
| 27 | + start: String, |
| 28 | + end: String |
46 | 29 | } |
47 | 30 |
|
48 | | -fn ez_log(attr: TokenStream, input: TokenStream) -> TokenStream { |
49 | | - let args = parse_macro_input!(attr as EzLogArgs); |
50 | | - // ... |
| 31 | +fn ez_trace(attr_ts: TokenStream, func_ts: TokenStream) -> TokenStream { |
| 32 | + let args = syn::parse_macro_input!(attr_ts as EzTraceArgs); |
| 33 | + /// Remaining macro implementation... |
51 | 34 | } |
52 | 35 | ``` |
53 | 36 |
|
54 | | -The `attribute_args` attribute generates the code necessary to parse an |
55 | | -`EzLogArgs` struct from the `TokenStream` using the |
56 | | -[parse_macro_input!](https://docs.rs/syn/0.15.39/syn/macro.parse_macro_input.html) |
57 | | -macro from the [syn](https://crates.io/crates/syn) crate. adding new parameters |
58 | | -to the macro is as easy as adding new fields to the `EzLogArgs` struct. |
| 37 | +With the above code, `args` will contain the string values `start` and `end` |
| 38 | +("Starting..." and "Ending..." respectively). To parse additional parameters |
| 39 | +simply add more fields to the `EzTraceArgs` struct. Currently the supported |
| 40 | +types are: `String`, `u64`, and `bool`. Support for more standard library types |
| 41 | +will arrive in the future and the ability to use custom types will also arrive |
| 42 | +later. |
| 43 | + |
| 44 | +## Contributing |
| 45 | + |
| 46 | +If you'd like to support further development of attribution there are a few |
| 47 | +things you can do. Feel free to open an issue [here] |
| 48 | +(https://github.com/chuck-flowers/attribution/issues/new) for any bugs you come |
| 49 | +across or any feature requests you have for future releases of attribution. For |
| 50 | +bugs, please include the minimal code required to produce the bug in the issue |
| 51 | +description along with what you expect to happen and what actually happens. |
| 52 | + |
| 53 | +I will be adding a link soon to accept financial donations if you so wish. I |
| 54 | +will continue to work on the project regardless of financial donations but if |
| 55 | +you find attribution useful and would like to buy me a cup of coffee I would |
| 56 | +be very grateful. |
| 57 | + |
| 58 | +I don't intend to accept pull requests at this time because I feel it would be |
| 59 | +unethical to solicit donations and accept pull requests without financially |
| 60 | +compensating the submitter. I don't want to get bogged down in the process of |
| 61 | +determining what a fair share of the donations would be. I also feel the scope |
| 62 | +and codebase of the project is small enough that I should be able to handle any |
| 63 | +issues that appear in a timely manner. |
0 commit comments