@@ -3,22 +3,6 @@ use quote::quote;
3
3
use syn:: parse:: * ;
4
4
use syn:: * ;
5
5
6
- mod kw {
7
- syn:: custom_keyword!( DEBUG_FORMAT ) ;
8
- syn:: custom_keyword!( MAX ) ;
9
- syn:: custom_keyword!( custom) ;
10
- }
11
-
12
- #[ derive( Debug ) ]
13
- enum DebugFormat {
14
- // The user will provide a custom `Debug` impl, so we shouldn't generate
15
- // one
16
- Custom ,
17
- // Use the specified format string in the generated `Debug` impl
18
- // By default, this is "{}"
19
- Format ( String ) ,
20
- }
21
-
22
6
// We parse the input and emit the output in a single step.
23
7
// This field stores the final macro output
24
8
struct Newtype ( TokenStream ) ;
@@ -35,7 +19,7 @@ impl Parse for Newtype {
35
19
36
20
// Any additional `#[derive]` macro paths to apply
37
21
let mut derive_paths: Vec < Path > = Vec :: new ( ) ;
38
- let mut debug_format: Option < DebugFormat > = None ;
22
+ let mut debug_format: Option < Lit > = None ;
39
23
let mut max = None ;
40
24
let mut consts = Vec :: new ( ) ;
41
25
let mut encodable = true ;
@@ -65,7 +49,18 @@ impl Parse for Newtype {
65
49
} ;
66
50
67
51
if let Some ( old) = max. replace ( literal. lit ) {
68
- panic ! ( "Specified multiple MAX: {:?}" , old) ;
52
+ panic ! ( "Specified multiple max: {:?}" , old) ;
53
+ }
54
+
55
+ false
56
+ }
57
+ "debug_format" => {
58
+ let Ok ( Meta :: NameValue ( literal) ) = attr. parse_meta ( ) else {
59
+ panic ! ( "#[debug_format = FMT] attribute requires a format" ) ;
60
+ } ;
61
+
62
+ if let Some ( old) = debug_format. replace ( literal. lit ) {
63
+ panic ! ( "Specified multiple debug format options: {:?}" , old) ;
69
64
}
70
65
71
66
false
@@ -79,23 +74,6 @@ impl Parse for Newtype {
79
74
body. parse :: < Token ! [ ..] > ( ) ?;
80
75
} else {
81
76
loop {
82
- if body. lookahead1 ( ) . peek ( kw:: DEBUG_FORMAT ) {
83
- body. parse :: < kw:: DEBUG_FORMAT > ( ) ?;
84
- body. parse :: < Token ! [ =] > ( ) ?;
85
- let new_debug_format = if body. lookahead1 ( ) . peek ( kw:: custom) {
86
- body. parse :: < kw:: custom > ( ) ?;
87
- DebugFormat :: Custom
88
- } else {
89
- let format_str: LitStr = body. parse ( ) ?;
90
- DebugFormat :: Format ( format_str. value ( ) )
91
- } ;
92
- try_comma ( ) ?;
93
- if let Some ( old) = debug_format. replace ( new_debug_format) {
94
- panic ! ( "Specified multiple debug format options: {:?}" , old) ;
95
- }
96
- continue ;
97
- }
98
-
99
77
// We've parsed everything that the user provided, so we're done
100
78
if body. is_empty ( ) {
101
79
break ;
@@ -112,7 +90,9 @@ impl Parse for Newtype {
112
90
}
113
91
}
114
92
115
- let debug_format = debug_format. unwrap_or ( DebugFormat :: Format ( "{}" . to_string ( ) ) ) ;
93
+ let debug_format =
94
+ debug_format. unwrap_or_else ( || Lit :: Str ( LitStr :: new ( "{}" , Span :: call_site ( ) ) ) ) ;
95
+
116
96
// shave off 256 indices at the end to allow space for packing these indices into enums
117
97
let max = max. unwrap_or_else ( || Lit :: Int ( LitInt :: new ( "0xFFFF_FF00" , Span :: call_site ( ) ) ) ) ;
118
98
@@ -167,18 +147,14 @@ impl Parse for Newtype {
167
147
quote ! { }
168
148
} ;
169
149
170
- let debug_impl = match debug_format {
171
- DebugFormat :: Custom => quote ! { } ,
172
- DebugFormat :: Format ( format) => {
173
- quote ! {
174
- impl :: std:: fmt:: Debug for #name {
175
- fn fmt( & self , fmt: & mut :: std:: fmt:: Formatter <' _>) -> :: std:: fmt:: Result {
176
- write!( fmt, #format, self . as_u32( ) )
177
- }
178
- }
150
+ let debug_impl = quote ! {
151
+ impl :: std:: fmt:: Debug for #name {
152
+ fn fmt( & self , fmt: & mut :: std:: fmt:: Formatter <' _>) -> :: std:: fmt:: Result {
153
+ write!( fmt, #debug_format, self . as_u32( ) )
179
154
}
180
155
}
181
156
} ;
157
+
182
158
let spec_partial_eq_impl = if let Lit :: Int ( max) = & max {
183
159
if let Ok ( max_val) = max. base10_parse :: < u32 > ( ) {
184
160
quote ! {
0 commit comments