1
1
//! Basic gist is a struct like this:
2
- //! ```
2
+ //! ```ignore
3
3
//! #[derive(Attribute)]
4
4
//! #[attribute(ident = "collection")]
5
5
//! #[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")]
17
17
//! ```
18
18
//!
19
19
//! Will be able to parse an attribute like this:
20
- //! ```
20
+ //! ```ignore
21
21
//! #[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()])]
22
22
//! ```
23
23
//!
@@ -34,11 +34,10 @@ use proc_macro2::{Literal, Span};
34
34
pub use r#macro:: Attribute ;
35
35
use syn:: {
36
36
bracketed, parse:: Parse , punctuated:: Punctuated , Expr , Lit , LitBool , LitByteStr , LitChar ,
37
- LitFloat , LitInt , LitStr , Path , Result , Token , Type , __private:: ToTokens ,
37
+ LitFloat , LitInt , LitStr , Path , Result , Token , Type , __private:: ToTokens , parse_quote ,
38
38
} ;
39
39
40
40
#[ deny( missing_docs) ]
41
-
42
41
#[ doc( hidden) ]
43
42
pub mod __private {
44
43
pub use proc_macro2;
@@ -48,7 +47,7 @@ pub mod __private {
48
47
/// The trait you actually derive on your attribute struct.
49
48
///
50
49
/// Basic gist is a struct like this:
51
- /// ```
50
+ /// ```ignore
52
51
/// #[derive(Attribute)]
53
52
/// #[attribute(ident = "collection")]
54
53
/// #[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")]
@@ -62,12 +61,15 @@ pub mod __private {
62
61
/// #[attribute(default)]
63
62
/// #[attribute(expected = "Error when an error occured while parsing")]
64
63
/// views: Vec<Type>,
64
+ /// // Booleans can be used without assiging a value. as a flag.
65
+ /// // If omitted they are set to false
66
+ /// some_flag: bool
65
67
/// }
66
68
/// ```
67
69
///
68
70
/// Will be able to parse an attribute like this:
69
- /// ```
70
- /// #[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()])]
71
+ /// ```ignore
72
+ /// #[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()], some_flag )]
71
73
/// ```
72
74
pub trait Attribute
73
75
where
77
79
/// [`Vec<Attribute>`](Vec).
78
80
///
79
81
/// It can therefore parse fields set over multiple attributes like:
80
- /// ```
82
+ /// ```ignore
81
83
/// #[collection(authority = "Authority", name = "Name")]
82
84
/// #[collection(views = [A, B])]
83
85
/// ```
@@ -118,6 +120,11 @@ where
118
120
fn default ( ) -> Self {
119
121
unreachable ! ( "default_by_default should only return true if this is overridden" )
120
122
}
123
+ /// Should values of this type be able to be defined as flag i.e. just `#[attr(default)]`
124
+ /// instead of `#[attr(default=true)]`
125
+ fn as_flag ( ) -> Option < Self :: Type > {
126
+ None
127
+ }
121
128
}
122
129
123
130
/// Helper trait to generate sensible errors
@@ -214,6 +221,26 @@ where
214
221
}
215
222
}
216
223
224
+ impl ConvertParsed for bool {
225
+ type Type = LitBool ;
226
+
227
+ fn convert ( value : Self :: Type ) -> Result < Self > {
228
+ Ok ( value. value )
229
+ }
230
+
231
+ fn default_by_default ( ) -> bool {
232
+ true
233
+ }
234
+
235
+ fn default ( ) -> Self {
236
+ false
237
+ }
238
+
239
+ fn as_flag ( ) -> Option < Self :: Type > {
240
+ Some ( parse_quote ! ( true ) )
241
+ }
242
+ }
243
+
217
244
/// Helper struct to parse array literals:
218
245
/// `[a, b, c]`
219
246
pub struct Array < T > {
@@ -253,7 +280,6 @@ convert_parsed!(LitStr => String: LitStr::value);
253
280
convert_parsed ! ( LitChar => char : LitChar :: value) ;
254
281
convert_parsed ! ( LitInt => u8 , i8 , u16 , i16 , u32 , i32 , u64 , i64 , u128 , i128 , usize , isize : ? LitInt :: base10_parse) ;
255
282
convert_parsed ! ( LitFloat => f32 , f64 : ? LitFloat :: base10_parse) ;
256
- convert_parsed ! ( LitBool => bool : LitBool :: value) ;
257
283
258
284
// TODO convert most of these
259
285
// impl Parse for Group
0 commit comments