Iterate identifiers of a macro

I need to change the FXME code to iterate each identifier.

macro_rules! sql_string_enum {
( $enumname: ident {
    $($enumvals: ident,)*
    } ) => {
    
        use std::result;
        use std::str::FromStr;
        
        #[derive(Debug)]
        pub enum $enumname {
            $($enumvals,)*
        }
        
        impl FromStr for $enumname {
            type Err = &'static str;

            fn from_str(s: &str) -> result::Result<Self, Self::Err> {
                match s {
                
                    //FIXME: This is where I need to iterate identifiers instead of hardcoding 
                    "ROLE" => Ok($enumname::ROLE),
                    "USER" => Ok($enumname::USER),
                    _ => Err("Not a valid resource value"),
                }
            }
        }
    }
}

fn main() {
    sql_string_enum!( Resource { ROLE, USER, });    
}

https://doc.rust-lang.org/1.7.0/book/macros.html#expansion

The syntax is basically:

$(
    temp_vec.push($x);
)*

You would use your own logic inside of the $( )* and whatever you're referring to instead of $x, in this case $enumvals

1 Like

@xoftware Thank you. Here is my final working version, in case someone comes here.

2 Likes