allow for variant aliases in target_spec_enum!
This commit is contained in:
parent
51affa0394
commit
7c3d096b0d
1 changed files with 12 additions and 4 deletions
|
|
@ -76,7 +76,7 @@ macro_rules! target_spec_enum {
|
|||
pub enum $Name:ident {
|
||||
$(
|
||||
$( #[$variant_attr:meta] )*
|
||||
$Variant:ident = $string:literal,
|
||||
$Variant:ident = $string:literal $(,$alias:literal)* ,
|
||||
)*
|
||||
}
|
||||
parse_error_type = $parse_error_type:literal;
|
||||
|
|
@ -88,6 +88,7 @@ macro_rules! target_spec_enum {
|
|||
$(
|
||||
$( #[$variant_attr] )*
|
||||
#[serde(rename = $string)] // for JSON schema generation only
|
||||
$( #[serde(alias = $alias)] )*
|
||||
$Variant,
|
||||
)*
|
||||
}
|
||||
|
|
@ -97,7 +98,10 @@ macro_rules! target_spec_enum {
|
|||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(match s {
|
||||
$( $string => Self::$Variant, )*
|
||||
$(
|
||||
$string => Self::$Variant,
|
||||
$($alias => Self::$Variant,)*
|
||||
)*
|
||||
_ => {
|
||||
let all = [$( concat!("'", $string, "'") ),*].join(", ");
|
||||
return Err(format!("invalid {}: '{s}'. allowed values: {all}", $parse_error_type));
|
||||
|
|
@ -123,7 +127,7 @@ macro_rules! target_spec_enum {
|
|||
pub enum $Name:ident {
|
||||
$(
|
||||
$( #[$variant_attr:meta] )*
|
||||
$Variant:ident = $string:literal,
|
||||
$Variant:ident = $string:literal $(,$alias:literal)* ,
|
||||
)*
|
||||
}
|
||||
$( #[$other_variant_attr:meta] )*
|
||||
|
|
@ -134,6 +138,7 @@ macro_rules! target_spec_enum {
|
|||
pub enum $Name {
|
||||
$(
|
||||
$( #[$variant_attr:meta] )*
|
||||
$( #[serde(alias = $alias)] )*
|
||||
$Variant,
|
||||
)*
|
||||
/// The vast majority of the time, the compiler deals with a fixed
|
||||
|
|
@ -165,7 +170,10 @@ macro_rules! target_spec_enum {
|
|||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(match s {
|
||||
$( $string => Self::$Variant, )*
|
||||
$(
|
||||
$string => Self::$Variant,
|
||||
$($alias => Self::$Variant,)*
|
||||
)*
|
||||
_ => Self::$OtherVariant(s.to_owned().into()),
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue