allow for variant aliases in target_spec_enum!

This commit is contained in:
Eddy (Eduard) Stefes 2026-02-06 10:53:59 +01:00
parent 51affa0394
commit 7c3d096b0d

View file

@ -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()),
})
}