8
8
9
9
use proc_macro:: TokenStream ;
10
10
use proc_macro2:: TokenStream as TokenStream2 ;
11
- use quote:: quote;
11
+ use quote:: { quote, ToTokens } ;
12
12
use syn:: DeriveInput ;
13
13
14
14
/// Derive `prometheus_client::encoding::EncodeLabelSet`.
@@ -60,14 +60,36 @@ pub fn derive_encode_label_set(input: TokenStream) -> TokenStream {
60
60
} )
61
61
. collect ( ) ,
62
62
syn:: Fields :: Unnamed ( _) => {
63
- panic ! ( "Can not derive Encode for struct with unnamed fields." )
63
+ return syn:: Error :: new_spanned (
64
+ name,
65
+ "Can not derive `EncodeLabelSet` for struct with unnamed fields." ,
66
+ )
67
+ . to_compile_error ( )
68
+ . to_token_stream ( )
69
+ . into ( ) ;
70
+ }
71
+ syn:: Fields :: Unit => {
72
+ return syn:: Error :: new_spanned (
73
+ name,
74
+ "Can not derive `EncodeLabelSet` for unit struct." ,
75
+ )
76
+ . to_compile_error ( )
77
+ . to_token_stream ( )
78
+ . into ( ) ;
64
79
}
65
- syn:: Fields :: Unit => panic ! ( "Can not derive Encode for struct with unit field." ) ,
66
80
} ,
67
81
syn:: Data :: Enum ( syn:: DataEnum { .. } ) => {
68
- panic ! ( "Can not derive Encode for enum." )
82
+ return syn:: Error :: new_spanned ( name, "Can not derive `EncodeLabelSet` for enum." )
83
+ . to_compile_error ( )
84
+ . to_token_stream ( )
85
+ . into ( ) ;
86
+ }
87
+ syn:: Data :: Union ( _) => {
88
+ return syn:: Error :: new_spanned ( name, "Can not derive `EncodeLabelSet` for union." )
89
+ . to_compile_error ( )
90
+ . to_token_stream ( )
91
+ . into ( )
69
92
}
70
- syn:: Data :: Union ( _) => panic ! ( "Can not derive Encode for union." ) ,
71
93
} ;
72
94
73
95
let ( impl_generics, ty_generics, where_clause) = ast. generics . split_for_impl ( ) ;
@@ -96,7 +118,10 @@ pub fn derive_encode_label_value(input: TokenStream) -> TokenStream {
96
118
97
119
let body = match ast. clone ( ) . data {
98
120
syn:: Data :: Struct ( _) => {
99
- panic ! ( "Can not derive EncodeLabel for struct." )
121
+ return syn:: Error :: new_spanned ( name, "Can not derive `EncodeLabelValue` for struct." )
122
+ . to_compile_error ( )
123
+ . to_token_stream ( )
124
+ . into ( ) ;
100
125
}
101
126
syn:: Data :: Enum ( syn:: DataEnum { variants, .. } ) => {
102
127
let match_arms: TokenStream2 = variants
@@ -115,7 +140,12 @@ pub fn derive_encode_label_value(input: TokenStream) -> TokenStream {
115
140
}
116
141
}
117
142
}
118
- syn:: Data :: Union ( _) => panic ! ( "Can not derive Encode for union." ) ,
143
+ syn:: Data :: Union ( _) => {
144
+ return syn:: Error :: new_spanned ( name, "Can not derive `EncodeLabelValue` for union." )
145
+ . to_compile_error ( )
146
+ . to_token_stream ( )
147
+ . into ( )
148
+ }
119
149
} ;
120
150
121
151
let gen = quote ! {
0 commit comments