Riven icon indicating copy to clipboard operation
Riven copied to clipboard

Add EnumVariantNames derive to relevant enums

Open VRichardJP opened this issue 3 years ago • 0 comments

I just stumbled upon EnumVariantNames which can be used to create a static list of an enum variant. I think it would be nice to use it for the constant enums defined in src/consts/*.rs (and maybe some other places too)

For example, let's say I am writing a cli interface and I want the user to input a server (or champion, or tier, etc), currently I have to do something ugly like:

let server_strings = PlatformRoute::iter().map(|server| &server.to_string() as &str).collect::<Vec<&str>>();
let matches = App::new("my awesome app")
    .arg(Arg::new("server")
        .about("Selected server")
        .short("s")
        .long("server")
        .possible_values(&server_strings)
    )

While with EnumVariantNames I could simply write

let matches = App::new("my awesome app")
    .arg(Arg::new("server")
        .about("Selected server")
        .short("s")
        .long("server")
        .possible_values(PlatformRoute::VARIANTS)
    )

VRichardJP avatar Oct 31 '21 06:10 VRichardJP