Skip to content
Discussion options

You must be logged in to vote

Ended up with this: (hint: to avoid using eunm_map you can impl ToString and use VARIANTS const)

use clap::Parser;
use enum_map::{Enum, EnumMap};

#[derive(Enum, Clone)]
enum Output {
    X,
    Y,
    Z,
    ...
}

pub const OUTPUT_MAP: EnumMap<Output, &'static str> = EnumMap::from_array([
    /* Output::X=>  */ "x",
    /* Output::Y=>  */ "y",
    /* Output::Z =>  */ "z",
    ...
]);

fn parse_outputs(value: &str) -> Result<Vec<Output>, String> {
    if value == "all" {
        Ok(OUTPUT_MAP.iter().map(|(o, _)| o).collect())
    } else {
        OUTPUT_MAP
            .iter()
            .find_map(|(o, s)| (&value == s).then_some(vec![o]))
            .ok_or_else(|| "invalid variant".into(

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@epage
Comment options

epage Jul 6, 2026
Maintainer

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by T-256
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants