use std::{error::Error, fmt::Display};
use crate::{Diagnostic, Report};
#[derive(Debug)]
pub(crate) struct DiagnosticError(pub(crate) Box<dyn std::error::Error + Send + Sync + 'static>);
impl Display for DiagnosticError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let msg = &self.0;
write!(f, "{msg}")
}
}
impl Error for DiagnosticError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.0.source()
}
}
impl Diagnostic for DiagnosticError {}
pub trait IntoDiagnostic<T, E> {
fn into_diagnostic(self) -> Result<T, Report>;
}
impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
fn into_diagnostic(self) -> Result<T, Report> {
self.map_err(|e| DiagnosticError(Box::new(e)).into())
}
}
#[cfg(test)]
mod tests {
use std::io::{self, ErrorKind};
use super::*;
use crate::error::tests::TestError;
#[test]
fn diagnostic_error() {
let inner_error = io::Error::new(ErrorKind::Other, "halt and catch fire");
let outer_error: Result<(), _> = Err(TestError(inner_error));
let diagnostic_error = outer_error.into_diagnostic().unwrap_err();
assert_eq!(diagnostic_error.to_string(), "testing, testing...");
assert_eq!(
diagnostic_error.source().unwrap().to_string(),
"halt and catch fire"
);
}
}