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

Common prefix #412

Open
jbirnick opened this issue Feb 1, 2025 · 2 comments
Open

Common prefix #412

jbirnick opened this issue Feb 1, 2025 · 2 comments

Comments

@jbirnick
Copy link

jbirnick commented Feb 1, 2025

Let's say I want to have a common prefix to all my errors, like:

#[derive(Error)]
enum MatrixParseError {
    #[error("Couldn't parse matrix: foo")]
    Foo
    #[error("Couldn't parse matrix: bar")]
    Bar
}

It would be cool to have the ability to specify such a prefix. (E.g. as [error("Couldn't parse matrix: {}")] on the enum.)

Do you think you could add that? Or do you recommend another idiomatic way of achieving this behavior?

@afetisov
Copy link

afetisov commented Feb 7, 2025

You can extract the common part into a wrapping struct:

#[derive(Debug, Error)]
#[error("Couldn't parse matrix: {}")]
struct MatrixParseError(#[from] MatrixParseErrorKind);

#[derive(Debug, Error)]
enum MatrixParseErrorKind {
    #[error("foo")]
    Foo
    #[error("bar")]
    Bar
}

@jbirnick
Copy link
Author

jbirnick commented Feb 7, 2025

Right, thank you!

It would still be great to be able to do this without an extra wrapping struct.

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