Skip to content
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