Skip to content

How do you implement a parser for a custom type that is fallible? #5416

Discussion options

You must be logged in to vote

Alright got it working. For some reason adding impl Display to my FromStr Error type didn't work but making the value parser function forcibly return a String error with Result<StorageUnit, String> did.

#[derive(Debug, Args)]
#[command(args_conflicts_with_subcommands = false)]
pub struct TuneCommand {
    #[arg(
        value_parser = parse_storage_unit,
        help = "Amount of memory to provide postgres. ie: '32GB'. (default system memory)"
    )]
    memory_limit: Option<StorageUnit>,
}
// Does work
fn parse_storage_unit(s: &str) -> Result<StorageUnit, String> {
    Ok(StorageUnit::from_str(s).map_err(|_| "Invalid storage unit")?)
}

// Doesn't work. Trait bound errors on `IntoResetable`

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@theelderbeever
Comment options

@theelderbeever
Comment options

@epage
Comment options

@theelderbeever
Comment options

Answer selected by theelderbeever
@epage
Comment options

@theelderbeever
Comment options

@epage
Comment options

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