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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//!
//! A generic boolean attribute property.
//! Defaults to `false`.
//!
use core marker PhantomData;
use crate :: *;
// use component_model_types ::Assign;
/// Default marker for `AttributePropertyBoolean`.
/// Used if no marker is defined as parameter.
;
/// A generic boolean attribute property.
/// Defaults to `false`.
///
/// # Example
///
/// ```rust
/// use macro_tools ::AttributePropertyBoolean;
///
/// #[ derive( Debug, Default, Clone, Copy ) ]
/// pub struct DebugMarker;
///
/// #[ derive( Debug, Default, Clone, Copy ) ]
/// pub struct EnabledMarker;
///
/// pub trait AttributePropertyComponent
/// {
/// const KEYWORD: &'static str;
/// }
///
/// impl AttributePropertyComponent for DebugMarker
/// {
/// const KEYWORD: &'static str = "debug";
/// }
///
/// impl AttributePropertyComponent for EnabledMarker
/// {
/// const KEYWORD: &'static str = "enabled";
/// }
///
/// #[ derive( Debug, Default ) ]
/// struct MyAttributes
/// {
/// pub debug: AttributePropertyBoolean< DebugMarker >,
/// pub enabled: AttributePropertyBoolean< EnabledMarker >,
/// }
///
/// impl syn ::parse ::Parse for MyAttributes
/// {
/// fn parse( input: syn ::parse ::ParseStream< '_ > ) -> syn ::Result< Self >
/// {
/// let mut debug = AttributePropertyBoolean :: < DebugMarker > ::default();
/// let mut enabled = AttributePropertyBoolean :: < EnabledMarker > ::default();
///
/// while !input.is_empty()
/// {
/// let lookahead = input.lookahead1();
/// if lookahead.peek( syn ::Ident )
/// {
/// let ident: syn ::Ident = input.parse()?;
/// match ident.to_string().as_str()
/// {
/// DebugMarker ::KEYWORD => debug = input.parse()?,
/// EnabledMarker ::KEYWORD => enabled = input.parse()?,
/// _ => return Err( lookahead.error() ),
/// }
/// }
/// else
/// {
/// return Err( lookahead.error() );
/// }
///
/// // Optional comma handling
/// if input.peek( syn ::Token![,] )
/// {
/// input.parse :: < syn ::Token![,] >()?;
/// }
/// }
///
/// Ok( MyAttributes { debug, enabled } )
/// }
/// }
///
/// let input: syn ::Attribute = syn ::parse_quote!( #[ attribute( enabled = true ) ] );
/// let meta = match input.meta
/// {
/// syn ::Meta ::List( meta_list ) => meta_list,
/// _ => panic!( "Expected a Meta ::List" ),
/// };
///
/// let nested_meta_stream: proc_macro2 ::TokenStream = meta.tokens;
/// let attrs: MyAttributes = syn ::parse2( nested_meta_stream ).unwrap();
/// println!( "{:?}", attrs );
/// ```
///
/// In this example, the `AttributePropertyBoolean` struct is used to define attributes with boolean properties.
/// The `DebugMarker` and `EnabledMarker` structs act as markers to distinguish between different boolean attributes.
/// The `MyAttributes` struct aggregates these boolean attributes.
///
/// The `Parse` implementation for `MyAttributes` iterates through the attribute's key-value pairs,
/// identifying each by its marker's keyword and parsing the boolean value.
/// It uses the `ParseStream` to parse identifiers and their associated values,
/// matching them to the appropriate marker's keyword.
/// If an unrecognized identifier is encountered, it returns an error.
///
/// The `parse_quote!` macro is used to create a `syn ::Attribute` instance with the attribute syntax,
/// which is then parsed into the `MyAttributes` struct. The resulting `MyAttributes` instance is printed to the console.
;