@@ -187,6 +187,25 @@ fn concat<T>(mut v1: Vec<T>, mut v2: Vec<T>) -> Vec<T> {
187187 v1
188188}
189189
190+ fn check_for_default ( triples : & mut Vec < ( TokenStream , Ident , TokenTree ) > ) {
191+ let mut default_position: Option < usize > = None ;
192+ for ( attributes, _variant_name, variant_value) in triples. into_iter ( ) {
193+ if attributes. to_string ( ) . contains ( "default" ) {
194+ if default_position. is_some ( ) {
195+ // error!("Multiple variants marked as default");
196+ }
197+ default_position = Some ( variant_value. to_string ( ) . parse :: < usize > ( ) . unwrap ( ) ) ;
198+ }
199+ }
200+ if !default_position. is_some ( ) {
201+ // No default specified, so we'll just use the first variant
202+ triples[ 0 ] . 0 . extend ( vec ! [
203+ punct_token( '#' ) ,
204+ bracket_token( vec![ ident_token( "default" ) ] ) . into( ) ,
205+ ] ) ;
206+ }
207+ }
208+
190209#[ proc_macro]
191210pub fn primitive_enum ( tokens : TokenStream ) -> TokenStream {
192211 let mut iter = tokens. into_iter ( ) ;
@@ -302,6 +321,7 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
302321 offset += 1 ;
303322 triples. push ( ( variant_attributes, variant_name, value) ) ;
304323 }
324+ check_for_default ( & mut triples) ; // make sure there's a default, if the user didn't specify one
305325 triples
306326 } ;
307327
@@ -322,7 +342,6 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
322342 ident_token( "repr" ) ,
323343 paren_token( repr_type. clone( ) ) ,
324344 ] ) ) ;
325-
326345 // #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
327346 out. push ( punct_token ( '#' ) ) ;
328347 out. push ( bracket_token ( vec ! [
@@ -339,8 +358,11 @@ pub fn primitive_enum(tokens: TokenStream) -> TokenStream {
339358 ident_token( "Eq" ) ,
340359 punct_token( ',' ) ,
341360 ident_token( "Hash" ) ,
361+ punct_token( ',' ) ,
362+ ident_token( "Default" ) ,
342363 ] ) ,
343364 ] ) ) ;
365+
344366 out. push ( ident_token ( "pub" ) ) ;
345367 out. push ( ident_token ( "enum" ) ) ;
346368 out. push ( TokenTree :: Ident ( enum_identifier. clone ( ) ) ) ;
0 commit comments