Iterate identifiers of a macro


#1

https://play.rust-lang.org/?gist=8ac6303dbf62d2e74c9f5c041970f2ca&version=nightly

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, });    
}

#2

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


#3

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

https://play.rust-lang.org/?gist=d47c02ccc4a521c878c003e178e25af9&version=nightly