how to generate matching arms for an enum in proc macro

55 views Asked by At

i have a enum like

#[derive(Normal)]
pub enum NormalError{
    #[msg = "{name} not found"]
    NotFound{name: &'static str},
    #[msg = "invalid {}"]
    InvalidParameter(&'static str),
    #[msg = "invalid"]
    InvalidCredential
}

and i want to make a proc macro to impl a function to print message like code below:

impl xxx for Normal{
    fn print(&self) -> String{
        match self{
            NormalError::NotFound{name} => format!("{name} not found"),
            NormalError::InvalidParameter(t) => format!("{} not found", t.0),
            NormalError::InvalidCredential => format!("invalid"),
        }
    }
}

but i failed to process the field of variant because the syn::Field cannot convert to syn::Arm easily. Is there any way to solve this problem?

0

There are 0 answers