|
77 | 77 | <a href="#76" id="76">76</a> |
78 | 78 | <a href="#77" id="77">77</a> |
79 | 79 | <a href="#78" id="78">78</a> |
80 | | -<a href="#79" id="79">79</a> |
81 | | -<a href="#80" id="80">80</a> |
82 | | -<a href="#81" id="81">81</a> |
83 | | -<a href="#82" id="82">82</a> |
84 | | -<a href="#83" id="83">83</a></pre></div><pre class="rust"><code><span class="doccomment">//! `eventually-macros` contains useful macros that provides |
| 80 | +<a href="#79" id="79">79</a></pre></div><pre class="rust"><code><span class="doccomment">//! `eventually-macros` contains useful macros that provides |
85 | 81 | //! different implementations of traits and functionalities from [eventually]. |
86 | 82 |
|
87 | 83 | </span><span class="attr">#![deny(unsafe_code, unused_qualifications, trivial_casts, missing_docs)] |
88 | 84 | #![deny(clippy::all, clippy::pedantic, clippy::cargo)] |
89 | 85 |
|
90 | 86 | </span><span class="kw">use </span>proc_macro::TokenStream; |
91 | 87 | <span class="kw">use </span>quote::quote; |
92 | | -<span class="kw">use </span>syn::{parse_macro_input, AttributeArgs, Fields, ItemStruct, Meta, NestedMeta, Path}; |
| 88 | +<span class="kw">use </span>syn::punctuated::Punctuated; |
| 89 | +<span class="kw">use </span>syn::{parse_macro_input, Fields, ItemStruct, Token, Type}; |
93 | 90 |
|
94 | 91 | <span class="doccomment">/// Implements a newtype to use the [`eventually::aggregate::Root`] instance with |
95 | 92 | /// user-defined [`eventually::aggregate::Aggregate`] types. |
|
105 | 102 | /// being an example of user-defined `Aggregate` type) outside the `eventually` crate (E0116). |
106 | 103 | /// Therefore, a newtype that uses `aggregate::Root<T>` is required. |
107 | 104 | /// |
108 | | -/// This attribute macro makes the implementation of a newtype easy, as it Implements |
| 105 | +/// This attribute macro makes the implementation of a newtype easy, as it implements |
109 | 106 | /// conversion traits from and to `aggregate::Root<T>` and implements automatic deref |
110 | 107 | /// through [`std::ops::Deref`] and [`std::ops::DerefMut`]. |
111 | 108 | /// |
|
114 | 111 | /// This method will panic if the Aggregate Root type is not provided as a macro parameter. |
115 | 112 | </span><span class="attr">#[proc_macro_attribute] |
116 | 113 | </span><span class="kw">pub fn </span>aggregate_root(args: TokenStream, item: TokenStream) -> TokenStream { |
117 | | - <span class="kw">let </span>args = <span class="macro">parse_macro_input!</span>(args <span class="kw">as </span>AttributeArgs); |
118 | 114 | <span class="kw">let </span><span class="kw-2">mut </span>item = <span class="macro">parse_macro_input!</span>(item <span class="kw">as </span>ItemStruct); |
119 | 115 | <span class="kw">let </span>item_ident = item.ident.clone(); |
120 | 116 |
|
121 | | - <span class="kw">let </span>aggregate_type = args |
122 | | - .first() |
123 | | - .and_then(|meta| <span class="kw">match </span>meta { |
124 | | - NestedMeta::Meta(Meta::Path(Path { segments, .. })) => <span class="prelude-val">Some</span>(segments), |
125 | | - <span class="kw">_ </span>=> <span class="prelude-val">None</span>, |
126 | | - }) |
127 | | - .and_then(|segments| segments.first()) |
128 | | - .map(|segment| segment.ident.clone()) |
129 | | - .expect(<span class="string">"the aggregate root type must be provided as macro parameter"</span>); |
| 117 | + <span class="kw">let </span>aggregate_type: Type = |
| 118 | + <span class="macro">parse_macro_input!</span>(args with Punctuated::<Type, <span class="macro">Token!</span>[,]>::parse_terminated) |
| 119 | + .into_iter() |
| 120 | + .next() |
| 121 | + .expect(<span class="string">"the aggregate root type must be provided as macro parameter"</span>); |
130 | 122 |
|
131 | 123 | item.fields = Fields::Unnamed( |
132 | 124 | syn::parse2(<span class="macro">quote!</span> { (eventually::aggregate::Root<#aggregate_type>) }).unwrap(), |
|
0 commit comments