@@ -3,7 +3,7 @@ mod serialize;
33
44use proc_macro:: TokenStream ;
55use proc_macro2:: Ident ;
6- use syn:: { Attribute , DeriveInput , Field , Lit , LitInt , Meta , NestedMeta } ;
6+ use syn:: { Attribute , DeriveInput , Expr , Field , Lit , LitInt , Meta , Token , punctuated :: Punctuated } ;
77
88#[ proc_macro_derive(
99 Deserialize ,
@@ -23,25 +23,20 @@ pub fn derive_serialize(input: TokenStream) -> TokenStream {
2323
2424fn get_enum_type ( input : & DeriveInput ) -> Ident {
2525 for attr in & input. attrs {
26- if !attr. path . is_ident ( "repr" ) {
26+ if !attr. path ( ) . is_ident ( "repr" ) {
2727 continue ;
2828 }
29- let meta = match attr. parse_meta ( ) {
30- Err ( _) => panic ! ( "encountered unparseable repr attribute" ) ,
31- Ok ( x) => x,
32- } ;
33- let list = match meta {
29+ let list = match & attr. meta {
3430 Meta :: List ( x) => x,
3531 _ => continue ,
3632 } ;
37- if list. nested . is_empty ( ) {
33+ if list. tokens . is_empty ( ) {
3834 panic ! ( "encountered repr attribute with no arguments" ) ;
3935 }
40- for nested_meta in list. nested {
41- let meta = match nested_meta {
42- NestedMeta :: Meta ( x) => x,
43- NestedMeta :: Lit ( _) => continue ,
44- } ;
36+ let nested = attr
37+ . parse_args_with ( Punctuated :: < Meta , Token ! [ , ] > :: parse_terminated)
38+ . unwrap ( ) ;
39+ for meta in nested {
4540 let path = match meta {
4641 Meta :: Path ( x) => x,
4742 _ => continue ,
@@ -57,20 +52,19 @@ fn get_enum_type(input: &DeriveInput) -> Ident {
5752
5853fn get_padding ( attrs : & Vec < Attribute > , attr_name : & str ) -> Option < LitInt > {
5954 for attr in attrs {
60- if !attr. path . is_ident ( attr_name) {
55+ if !attr. path ( ) . is_ident ( attr_name) {
6156 continue ;
6257 }
63- let meta = match attr. parse_meta ( ) {
64- Err ( _) => panic ! ( "encountered unparseable {} attribute" , attr_name) ,
65- Ok ( x) => x,
66- } ;
67- let lit = match meta {
68- Meta :: NameValue ( x) => x. lit ,
69- _ => panic ! ( "{} needs to be name=value" , attr_name) ,
58+ let lit = match & attr. meta {
59+ Meta :: NameValue ( x) => match & x. value {
60+ Expr :: Lit ( expr_lit) => & expr_lit. lit ,
61+ _ => panic ! ( "{attr_name} value must be a literal" ) ,
62+ } ,
63+ _ => panic ! ( "{attr_name} needs to be name=value" ) ,
7064 } ;
7165 let int_lit = match lit {
72- Lit :: Int ( x) => x,
73- _ => panic ! ( "{} needs to be an integer" , attr_name ) ,
66+ Lit :: Int ( x) => x. clone ( ) ,
67+ _ => panic ! ( "{attr_name } needs to be an integer" ) ,
7468 } ;
7569 return Some ( int_lit) ;
7670 }
0 commit comments