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

Document #[from] attribute #3

Open
jalil-salame opened this issue May 11, 2023 · 2 comments
Open

Document #[from] attribute #3

jalil-salame opened this issue May 11, 2023 · 2 comments

Comments

@jalil-salame
Copy link

jalil-salame commented May 11, 2023

Document #[from] attribute, it is otherwise not clear that you can use it.

Previous (wrong) issue:

thiserror supports the #[from] attribute (see details) to reduce manually written that code, justerror should support it too:

use thiserror::Error;

#[derive(Error, Debug)]
pub enum DataStoreError {
    #[error("data store disconnected")]
    Disconnect(#[from] io::Error), /// **HERE**
    #[error("the data for key `{0}` is not available")]
    Redaction(String),
    #[error("invalid header (expected {expected:?}, found {found:?})")]
    InvalidHeader {
        expected: String,
        found: String,
    },
    #[error("unknown data store error")]
    Unknown,
}

You otherwise need to manually impl the From trait:

impl From<io::Error> for DataStoreError {
    fn from(value: io::Error) -> Self {
        Self::Disconnect(value)
    }
}
@alex35mil
Copy link
Member

It is supported. I use it all the time. What error do you see when you apply it?

@jalil-salame
Copy link
Author

jalil-salame commented May 11, 2023

It is not detailed anywhere in the documentation so I assumed it didn't exist... I will change the issue title and description.

@jalil-salame jalil-salame changed the title Add #[from] attribute Document #[from] attribute May 11, 2023
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