use std::fmt;
use thiserror::Error;
#[derive(Debug, Error)]
#[error("{message}")]
pub struct Diagnostic {
pub rule: String,
pub message: String,
pub help: Option<String>,
}
impl miette::Diagnostic for Diagnostic {
fn code(&self) -> Option<Box<dyn fmt::Display + '_>> {
Some(Box::new(self.rule.as_str()))
}
fn severity(&self) -> Option<miette::Severity> {
Some(miette::Severity::Error)
}
fn help(&self) -> Option<Box<dyn fmt::Display + '_>> {
self.help
.as_deref()
.map(|help| Box::new(help) as Box<dyn fmt::Display>)
}
}
#[derive(Debug, Error)]
#[error("the fuse blew: {} electrical violation(s)", .violations.len())]
pub struct FuseBlown {
pub violations: Vec<Diagnostic>,
}
impl FuseBlown {
pub fn new(violations: Vec<Diagnostic>) -> Self {
Self { violations }
}
}
impl miette::Diagnostic for FuseBlown {
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn miette::Diagnostic> + 'a>> {
Some(Box::new(
self.violations
.iter()
.map(|diagnostic| diagnostic as &dyn miette::Diagnostic),
))
}
fn help(&self) -> Option<Box<dyn fmt::Display + '_>> {
Some(Box::new(
"fix the violations above: generation stays blocked while the \
fuse is blown",
))
}
}