macro_rules! diagnostic_from {
($error:ty, |$value:ident| $code:expr) => { ... };
($error:ty, |$value:ident| $code:expr, |$diagnostic:ident, $inner:ident| $context:expr) => { ... };
}Expand description
Implements From<&E> and From<E> for Diagnostic.
The code is chosen by the expression given and the message is the error’s
own Display text, so nothing the error already says is lost. An optional
third argument adds structured context to the diagnostic.
use molframe_core::{Code, Diagnostic, diagnostic_from};
#[derive(Debug)]
struct TooBig(usize);
impl std::fmt::Display for TooBig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} is too big", self.0)
}
}
diagnostic_from!(TooBig, |_error| Code::E7001, |diagnostic, error| {
diagnostic.with_context("size", error.0.to_string())
});
let diagnostic = Diagnostic::from(TooBig(7));
assert_eq!(diagnostic.code(), Code::E7001);