diff --git a/README.md b/README.md index 8ecc16d..0fb3a71 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ diagnostic error code: ruget::api::bad_json - [... syntax highlighting](#-syntax-highlighting) - [... primary label](#-primary-label) - [... collection of labels](#-collection-of-labels) + - [... with generic errors](#-with-generic-errors) - [Acknowledgements](#acknowledgements) - [License](#license) @@ -782,6 +783,58 @@ let report: miette::Report = MyError { println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string())); ``` +#### ... with generic errors + +When tring to build more complex error types, it can often be useful to use generics. + +```rust +#[derive(Debug, Diagnostic, Error)] +enum MyError { + #[error(transparent)] + #[diagnostic(transparent)] + Base(T), + #[error("Some other error occured")] + #[diagnostic(help = "See the manual.")] + OtherError +} +``` +To enable this pattern, you can enable **the `perfect-derive` feature** on miette. +This will add trait bounds on generics in the `Diagnostic` implementation, depending on how +they are used inside the struct/enum. + +This should work for all other attributes as well, like `#[label]` or `#[diagnostic_source]`. + +
+ + +##### ⚠ Warning: (Small) Gotcha with the `#[related]` attribute + + + +Because of current lifetime constraints, only generic collection elements but not generic +collections are currently supported, meaning the following works: + +```rust +#[derive(Debug, Diagnostic, Error)] +#[error("Some example error")] +struct MyError { + #[related] + related_errors: Vec +} +``` +but the following does not: +```rust +#[derive(Debug, Diagnostic, Error)] +#[error("Some example error")] +struct MyError { + // See the difference here? + // Note that the collection is general, and not + // the elements inside the vec. This is **not** supported. + #[related] + related_errors: T // <- here +} +``` +
### MSRV diff --git a/src/lib.rs b/src/lib.rs index 21ad61d..1d4a950 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,6 +53,7 @@ //! - [... syntax highlighting](#-syntax-highlighting) //! - [... primary label](#-primary-label) //! - [... collection of labels](#-collection-of-labels) +//! - [... with generic errors](#-with-generic-errors) //! - [Acknowledgements](#acknowledgements) //! - [License](#license) //! @@ -783,6 +784,59 @@ //! //! println!("{:?}", report.with_source_code("About something or another or yet another ...".to_string())); //! ``` +//! ### ... with generic errors +//! +//! When tring to build more complex error types, it can often be useful to use generics. +//! +//! ```rust,ignore +//! #[derive(Debug, Diagnostic, Error)] +//! enum MyError { +//! #[error(transparent)] +//! #[diagnostic(transparent)] +//! Base(T), +//! #[error("Some other error occured")] +//! #[diagnostic(help = "See the manual.")] +//! OtherError +//! } +//! ``` +//! To enable this pattern, you can enable **the `perfect-derive` feature** on miette. +//! This will add trait bounds on generics in the `Diagnostic` implementation, depending on how +//! they are used inside the struct/enum. +//! +//! This should work for all other attributes as well, like `#[label]` or `#[diagnostic_source]`. +//! +//!
+//! +//! +//! #### ⚠ Warning: (Small) Gotcha with the `#[related]` attribute +//! +//! +//! +//! Because of current lifetime constraints, only generic collection elements but not generic +//! collections are currently supported, meaning the following works: +//! +//! ```rust,ignore +//! #[derive(Debug, Diagnostic, Error)] +//! #[error("Some example error")] +//! struct MyError { +//! #[related] +//! related_errors: Vec +//! } +//! ``` +//! but the following does not: +//! ```rust,ignore +//! #[derive(Debug, Diagnostic, Error)] +//! #[error("Some example error")] +//! struct MyError { +//! // See the difference here? +//! // Note that the collection is general, and not +//! // the elements inside the vec. This is **not** supported. +//! #[related] +//! related_errors: T // <- here +//! } +//! ``` + +//!
//! //! ## MSRV //!