use miette::{Diagnostic, LabeledSpan, NamedSource};
use std::fmt;
#[derive(Debug, Clone)]
pub struct CgpDiagnostic {
pub message: String,
pub code: Option<String>,
pub help: Option<String>,
pub source_code: Option<NamedSource<String>>,
pub labels: Vec<LabeledSpan>,
}
impl fmt::Display for CgpDiagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for CgpDiagnostic {}
impl Diagnostic for CgpDiagnostic {
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
None
}
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
self.help
.as_ref()
.map(|h| Box::new(h.clone()) as Box<dyn fmt::Display>)
}
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
self.source_code
.as_ref()
.map(|s| s as &dyn miette::SourceCode)
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
if self.labels.is_empty() {
None
} else {
Some(Box::new(self.labels.clone().into_iter()))
}
}
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
None }
}