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

Add example for expressions in error attribute #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ pub enum DataStoreError {
}
```

The attribute accepts complex expressions as well. For example, if you'd like
to transform an `Option<T>` into something else in the error messsage, you
can do so.

```rust
#[derive(Error, Debug)]
pub enum Error {
#[error("lost connection to the server: {}", match .0 {
Some(reason) => &reason,
None => "unknown reason",
})]
ConnectionLost(Option<String>),
}

- A `From` impl is generated for each variant containing a `#[from]` attribute.

Note that the variant must not contain any other fields beyond the source
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@
//! }
//! ```
//!
//! The attribute accepts complex expressions as well. For example, if you'd like
//! to transform an `Option<T>` into something else in the error messsage, you
//! can do so.
//!
//! ```rust
//! # use thiserror::Error;
//!
//! #[derive(Error, Debug)]
//! pub enum Error {
//! #[error("lost connection to the server: {}", match .0 {
//! Some(reason) => &reason,
//! None => "unknown reason",
//! })]
//! ConnectionLost(Option<String>),
//! }
//!
//! - A `From` impl is generated for each variant containing a `#[from]`
//! attribute.
//!
Expand Down