use crate::Report;
pub trait IntoDiagnostic<T, E> {
fn into_diagnostic(self) -> Result<T, Report>;
}
impl<T, E: core::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
fn into_diagnostic(self) -> Result<T, Report> {
self.map_err(Report::from_error)
}
}
#[cfg(test)]
mod tests {
use std::{error::Error, fmt, io, string::ToString};
use super::*;
#[derive(Debug)]
struct TestError(io::Error);
impl fmt::Display for TestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "testing, testing...")
}
}
impl Error for TestError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.0)
}
}
#[test]
fn diagnostic_error() {
let inner_error = io::Error::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");
}
}