1
1
use proc_macro2:: TokenStream ;
2
2
use proc_macro_error:: { abort_call_site, proc_macro_error, ResultExt } ;
3
3
use quote:: format_ident;
4
+ use quote_use:: quote_use as quote;
5
+ use syn:: punctuated:: Punctuated ;
4
6
use syn:: {
5
- parse_macro_input, punctuated :: Punctuated , DataStruct , DeriveInput , Field , Fields , FieldsNamed ,
6
- Lit , Meta , MetaNameValue , NestedMeta , Token ,
7
+ parse_macro_input, DataStruct , DeriveInput , Field , Fields , FieldsNamed , Lit , Meta ,
8
+ MetaNameValue , NestedMeta , Token ,
7
9
} ;
8
10
9
- use quote_use:: quote_use as quote;
10
-
11
11
// TODO generally should use fully qualified names for trait function calls
12
12
13
13
#[ proc_macro_error]
@@ -30,7 +30,7 @@ pub fn attribute_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre
30
30
. into_iter ( )
31
31
. filter ( |attribute| attribute. path . is_ident ( "attribute" ) )
32
32
{
33
- const VALID_FORMAT : & str = r#"Expected `#[attribute(ident="name_of_your_attribute", invalid_field="error message")]`"# ;
33
+ const VALID_FORMAT : & str = r#"Expected `#[attribute(ident="name_of_your_attribute", invalid_field="error message", missing="error messag", conflicts(a, b)" )]`"# ;
34
34
let meta: Meta = attribute. parse_meta ( ) . unwrap_or_abort ( ) ;
35
35
if let Meta :: List ( meta) = meta {
36
36
for meta in meta. nested {
@@ -208,16 +208,16 @@ pub fn attribute_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStre
208
208
_ => abort_call_site ! ( "Only works on structs with named fields" ) ,
209
209
} ;
210
210
211
- let error_invalid_name = invalid_field. unwrap_or_else ( || {
212
- if possible_variables. len ( ) > 1 {
211
+ let error_invalid_name = invalid_field. unwrap_or_else ( || match possible_variables. len ( ) {
212
+ 0 => format ! ( "Expected empty attribute" ) ,
213
+ 1 => format ! ( "Expected supported field {}" , possible_variables[ 0 ] ) ,
214
+ _ => {
213
215
let last = possible_variables. pop ( ) . unwrap ( ) ;
214
216
format ! (
215
217
"Supported fields are {} and {}" ,
216
218
possible_variables. join( ", " ) ,
217
219
last
218
220
)
219
- } else {
220
- format ! ( "Expected supported field {}" , possible_variables[ 0 ] )
221
221
}
222
222
} ) ;
223
223
0 commit comments