Skip to content

Commit dc74a05

Browse files
committed
Document the feature flag (perfect-derive) inside the Readme and on the main docs page
Signed-off-by: Justus Flügel <[email protected]>
1 parent 53e16c5 commit dc74a05

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ diagnostic error code: ruget::api::bad_json
5353
- [... syntax highlighting](#-syntax-highlighting)
5454
- [... primary label](#-primary-label)
5555
- [... collection of labels](#-collection-of-labels)
56+
- [... with generic errors](#-with-generic-errors)
5657
- [Acknowledgements](#acknowledgements)
5758
- [License](#license)
5859

@@ -771,6 +772,58 @@ let report: miette::Report = MyError {
771772

772773
println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string()));
773774
```
775+
#### ... with generic errors
776+
777+
When tring to build more complex error types, it can often be useful to use generics.
778+
779+
```rust
780+
#[derive(Debug, Diagnostic, Error)]
781+
enum MyError<T> {
782+
#[error(transparent)]
783+
#[diagnostic(transparent)]
784+
Base(T),
785+
#[error("Some other error occured")]
786+
#[diagnostic(help = "See the manual.")]
787+
OtherError
788+
}
789+
```
790+
To enable this pattern, you can enable **the `perfect-derive` feature** on miette.
791+
This will add trait bounds on generics in the `Diagnostic` implementation, depending on how
792+
they are used inside the struct/enum.
793+
794+
This should work for all other attributes as well, like `#[label]` or `#[diagnostic_source]`.
795+
796+
<details>
797+
<summary>
798+
799+
##### ⚠ Warning: (Small) Gotcha with the `#[related]` attribute
800+
801+
</summary>
802+
803+
Because of current lifetime constraints, only generic collection elements but not generic
804+
collections are currently supported, meaning the following works:
805+
806+
```rust
807+
#[derive(Debug, Diagnostic, Error)]
808+
#[error("Some example error")]
809+
struct MyError<T> {
810+
#[related]
811+
related_errors: Vec<T>
812+
}
813+
```
814+
but the following does not:
815+
```rust
816+
#[derive(Debug, Diagnostic, Error)]
817+
#[error("Some example error")]
818+
struct MyError<T> {
819+
// See the difference here?
820+
// Note that the collection is general, and not
821+
// the elements inside the vec. This is **not** supported.
822+
#[related]
823+
related_errors: T // <- here
824+
}
825+
```
826+
</details>
774827

775828
### MSRV
776829

src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
//! - [... syntax highlighting](#-syntax-highlighting)
5454
//! - [... primary label](#-primary-label)
5555
//! - [... collection of labels](#-collection-of-labels)
56+
//! - [... with generic errors](#-with-generic-errors)
5657
//! - [Acknowledgements](#acknowledgements)
5758
//! - [License](#license)
5859
//!
@@ -772,6 +773,59 @@
772773
//!
773774
//! println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string()));
774775
//! ```
776+
//! ### ... with generic errors
777+
//!
778+
//! When tring to build more complex error types, it can often be useful to use generics.
779+
//!
780+
//! ```rust,ignore
781+
//! #[derive(Debug, Diagnostic, Error)]
782+
//! enum MyError<T> {
783+
//! #[error(transparent)]
784+
//! #[diagnostic(transparent)]
785+
//! Base(T),
786+
//! #[error("Some other error occured")]
787+
//! #[diagnostic(help = "See the manual.")]
788+
//! OtherError
789+
//! }
790+
//! ```
791+
//! To enable this pattern, you can enable **the `perfect-derive` feature** on miette.
792+
//! This will add trait bounds on generics in the `Diagnostic` implementation, depending on how
793+
//! they are used inside the struct/enum.
794+
//!
795+
//! This should work for all other attributes as well, like `#[label]` or `#[diagnostic_source]`.
796+
//!
797+
//! <details>
798+
//! <summary>
799+
//!
800+
//! #### ⚠ Warning: (Small) Gotcha with the `#[related]` attribute
801+
//!
802+
//! </summary>
803+
//!
804+
//! Because of current lifetime constraints, only generic collection elements but not generic
805+
//! collections are currently supported, meaning the following works:
806+
//!
807+
//! ```rust,ignore
808+
//! #[derive(Debug, Diagnostic, Error)]
809+
//! #[error("Some example error")]
810+
//! struct MyError<T> {
811+
//! #[related]
812+
//! related_errors: Vec<T>
813+
//! }
814+
//! ```
815+
//! but the following does not:
816+
//! ```rust,ignore
817+
//! #[derive(Debug, Diagnostic, Error)]
818+
//! #[error("Some example error")]
819+
//! struct MyError<T> {
820+
//! // See the difference here?
821+
//! // Note that the collection is general, and not
822+
//! // the elements inside the vec. This is **not** supported.
823+
//! #[related]
824+
//! related_errors: T // <- here
825+
//! }
826+
//! ```
827+
828+
//! </details>
775829
//!
776830
//! ## MSRV
777831
//!

0 commit comments

Comments
 (0)