Apply stringify! to an enum variant for serde alias in a declarative macro

Is below possible or serde just does not allow any kind expressions in the alias?

#[test]
fn test_stringify_in_macro(){
    macro_rules! create_enum {
        ($NAME:ident, $($VARIANT:tt : $VALUE:literal),*) => {
            #[derive(Debug, serde::Serialize)]
            pub enum $NAME {
                $(
                    #[serde(rename = $VALUE)]
                    #[serde(alias = "stringify!($VARIANT)")] // how to stringify the variant for serde? is it possible?
                    $VARIANT
                ),*
            }
          
        };
        
    }
    create_enum!(MyEnum, One: "1", Two: "2", Three: "3");
    let x = MyEnum::One;
    println!("{:?}", x);
}

You are literally setting the alias to the string "stringify!($VARIANT)". This has nothing to do with serde.

You'd want #[serde(alias = stringify!($VARIANT))], without the quotes, but that doesn't compile due to macro expansion order.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.