Skip to content

Support custom FromStr error types #7

@rossmacarthur

Description

@rossmacarthur

It would be cool to if it was possible to specify a custom error type for the FromStr implementation.
I think it could either be of the form struct Error; or struct Error(String); where in the latter it would be passed a copy of the string that we attempted to parse.

use thiserror::Error;
use parse_display::FromStr;

#[derive(Debug, Error)]
#[error("expected one of `var_a` or `var_b`, got `{}`", self.0)]
pub struct ParseMyEnumError(String);

#[derive(Display, FromStr, PartialEq, Debug)]
#[display(style = "snake_case", error = ParseMyEnumError)]
enum MyEnum {
  VarA,
  VarB,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions