Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom string for None/Some(T) #59

Closed
Nokel81 opened this issue Jan 28, 2020 · 6 comments
Closed

Support custom string for None/Some(T) #59

Nokel81 opened this issue Jan 28, 2020 · 6 comments

Comments

@Nokel81
Copy link

Nokel81 commented Jan 28, 2020

It would be very useful to be able to support specialized display strings for when an argument of an enum is of type Option<T> and is the variant None. This would also remove the Some(...) from the display on the Some variant.

@dtolnay
Copy link
Owner

dtolnay commented Jan 28, 2020

Could you show an example of what you would like to be able to write?

Nothing in thiserror automatically puts "None" or "Some" into the display string, that must be from using {:?} on the field in your format string.

@Nokel81
Copy link
Author

Nokel81 commented Jan 28, 2020

I guess I meant that for the type Option<T: Display> you would provided two strings, one for Some(t) which would be the same as {} for the T and the other not having one at all.

Example:

#[derive(Error)]
pub enum MyError {
    #[error(
        1.Some:"a variant error occurred with {1}: {0}"; 
        1.None:"there was an empty variant error: {0}"
    )]
    Variant(String, Option<usize>)
}

@dtolnay
Copy link
Owner

dtolnay commented Jan 28, 2020

I would prefer not to build fancier support for this into thiserror. For now you can handwrite the Display impl, or make this work using the existing features depending on your preference.

#[derive(Error, Debug)]
pub enum MyError {
    Variant(String, Option<usize>)
}

impl Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MyError::Variant(a, Some(b)) => {
                write!(f, "a variant error occurred with {}: {}", b, a)
            }
            MyError::Variant(a, None) => {
                write!(f, "there was an empty variant error: {}", a)
            } 
        }
    }
}
#[derive(Error, Debug)]
pub enum MyError {
    #[error("{}: {0}", match .1 {
        Some(n) => format!("a variant error occurred with {}", n),
        None => format!("there was an empty variant error"),
    })]
    Variant(String, Option<usize>),
}

@Nokel81
Copy link
Author

Nokel81 commented Jan 28, 2020

Would it be possible to get the second one into the documentation? I didn't even know that existed.

@dtolnay
Copy link
Owner

dtolnay commented Jan 28, 2020

I believe this is largely covered by the third bullet of https://github.com/dtolnay/thiserror/tree/ff9524e1fd65fc49cfe905e9456c255e470fb239#details which mentions that format arguments may be arbitrary expressions, with multiple examples.

@Nokel81
Copy link
Author

Nokel81 commented Jan 28, 2020

Excellent, thanks.

@Nokel81 Nokel81 closed this as completed Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants