@@ -101,10 +101,26 @@ macro_rules! __str_enum_sqlx {
101101}
102102
103103/// Declares a documented, closed string vocabulary with serde and parsing support,
104- /// plus `SQLx` text decoding when the `sqlx` feature is on.
104+ /// plus `SQLx` text decoding when the `sqlx` feature is on. Unknown spellings are
105+ /// rejected, unless an `else` variant follows the enum: then they read as it, for
106+ /// labels a newer release may extend that older ones can show coarsely.
107+ ///
108+ /// ```
109+ /// piquel_common::str_enum! {
110+ /// /// A coarse label.
111+ /// pub enum Shade {
112+ /// /// Light.
113+ /// Light = "light",
114+ /// /// Anything else.
115+ /// Other = "other",
116+ /// } else Other
117+ /// }
118+ ///
119+ /// assert_eq!("teal".parse(), Ok(Shade::Other));
120+ /// ```
105121#[ macro_export]
106122macro_rules! str_enum {
107- ( $( #[ $meta: meta] ) * $vis: vis enum $name: ident { $( $( #[ $variant_meta: meta] ) * $variant: ident = $value: literal) ,+ $( , ) ? } ) => {
123+ ( $( #[ $meta: meta] ) * $vis: vis enum $name: ident { $( $( #[ $variant_meta: meta] ) * $variant: ident = $value: literal) ,+ $( , ) ? } $ ( else $fallback : ident ) ? ) => {
108124 $( #[ $meta] ) *
109125 #[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
110126 $vis enum $name { $( $( #[ $variant_meta] ) * $variant) ,+ }
@@ -121,22 +137,24 @@ macro_rules! str_enum {
121137 }
122138 impl :: std:: str :: FromStr for $name {
123139 type Err = $crate:: VocabularyError ;
124- /// Parses the stable spelling and rejects values outside the closed vocabulary .
125- fn from_str( value: & str ) -> Result <Self , Self :: Err > { match value { $( $value => Ok ( Self :: $variant) , ) + _ => Err ( $crate:: VocabularyError { vocabulary : stringify! ( $name) } ) } }
140+ /// Parses the stable spelling; an unknown one is rejected or reads as the `else` variant .
141+ fn from_str( value: & str ) -> Result <Self , Self :: Err > { match value { $( $value => Ok ( Self :: $variant) , ) + _ => $crate:: str_enum! ( @unknown $name $ ( $fallback ) ? ) } }
126142 }
127143 impl $crate:: __serde:: Serialize for $name {
128144 /// Writes the stable vocabulary spelling to its wire representation.
129145 fn serialize<S : $crate:: __serde:: Serializer >( & self , serializer: S ) -> Result <S :: Ok , S :: Error > { serializer. serialize_str( self . as_str( ) ) }
130146 }
131147 impl <' de> $crate:: __serde:: Deserialize <' de> for $name {
132- /// Parses the wire spelling and rejects values outside the closed vocabulary .
148+ /// Parses the wire spelling as `FromStr` does .
133149 fn deserialize<D : $crate:: __serde:: Deserializer <' de>>( deserializer: D ) -> Result <Self , D :: Error > {
134150 let value = <String as $crate:: __serde:: Deserialize >:: deserialize( deserializer) ?;
135151 value. parse( ) . map_err( $crate:: __serde:: de:: Error :: custom)
136152 }
137153 }
138154 $crate:: __str_enum_sqlx!( $name) ;
139155 } ;
156+ ( @unknown $name: ident) => { Err ( $crate:: VocabularyError { vocabulary: stringify!( $name) } ) } ;
157+ ( @unknown $name: ident $fallback: ident) => { Ok ( Self :: $fallback) } ;
140158 ( @count $( $variant: ident) ,+) => { <[ ( ) ] >:: len( & [ $( $crate:: str_enum!( @unit $variant) ) ,+] ) } ;
141159 ( @unit $variant: ident) => { ( ) } ;
142160}
0 commit comments