From ebc61b5cf809e4215ca92c32d892cfc4ffb05fa9 Mon Sep 17 00:00:00 2001 From: Lucas Kent Date: Tue, 31 Jan 2023 05:21:50 +1100 Subject: [PATCH] docs: Mention miette::miette! macro under "... in application code" (#233) --- README.md | 18 ++++++++++++++++++ src/lib.rs | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 6f9dcfb..c12734d 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,23 @@ pub fn some_tool() -> Result { } ``` +To construct your own simple adhoc error use the `miette::miette!` macro: + +```rust +// my_app/lib/my_internal_file.rs +use miette::{IntoDiagnostic, Result, WrapErr, miette}; +use semver::Version; + +pub fn some_tool() -> Result { + let version = "1.2.x"; + Ok(version + .parse() + .map_err(|_| miette!("Invalid version {}", version))?) +} +``` + +There are also similar `miette::bail!` and `miette::ensure!` macros. + #### ... in `main()` `main()` is just like any other part of your application-internal code. Use @@ -379,6 +396,7 @@ pub struct MyErrorType { ``` ##### ... help text + `miette` provides two facilities for supplying help text for your errors: The first is the `#[help()]` format attribute that applies to structs or diff --git a/src/lib.rs b/src/lib.rs index f6c69e4..44c57a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -246,6 +246,21 @@ //! } //! ``` //! +//! To construct your own simple adhoc error use the [miette!] macro: +//! ```rust +//! // my_app/lib/my_internal_file.rs +//! use miette::{IntoDiagnostic, Result, WrapErr, miette}; +//! use semver::Version; +//! +//! pub fn some_tool() -> Result { +//! let version = "1.2.x"; +//! Ok(version +//! .parse() +//! .map_err(|_| miette!("Invalid version {}", version))?) +//! } +//! ``` +//! There are also similar [bail!] and [ensure!] macros. +//! //! ### ... in `main()` //! //! `main()` is just like any other part of your application-internal code. Use