miette

Macro miette

source
macro_rules! miette {
    ($($key:ident = $value:expr,)* $fmt:literal $($arg:tt)*) => { ... };
    ($err:expr $(,)?) => { ... };
}
Expand description

Construct an ad-hoc Report.

§Examples

With string literal and interpolation:

let x = 1;
let y = 2;
let report = miette!("{x} + {} = {z}", y, z = x + y);

assert_eq!(report.to_string().as_str(), "1 + 2 = 3");

let z = x + y;
let report = miette!("{x} + {y} = {z}");
assert_eq!(report.to_string().as_str(), "1 + 2 = 3");

With diagnostic!-like arguments:

use miette::{miette, LabeledSpan, Severity};

let source = "(2 + 2".to_string();
let report = miette!(
    // Those fields are optional
    severity = Severity::Error,
    code = "expected::rparen",
    help = "always close your parens",
    labels = vec![LabeledSpan::at_offset(6, "here")],
    url = "https://example.com",
    // Rest of the arguments are passed to `format!`
    // to form diagnostic message
    "expected closing ')'"
)
.with_source_code(source);

§anyhow/eyre Users

You can just replace uses of the anyhow!/eyre! macros with miette!.