From 6d521a512a8eb875fa6af0ad3f633fc9b77db3bd Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Sat, 26 Apr 2025 16:39:25 +0100 Subject: [PATCH] refactor(miette): remove `thiserror` from `into_diagnostic.rs` --- src/eyreish/into_diagnostic.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/eyreish/into_diagnostic.rs b/src/eyreish/into_diagnostic.rs index f862ac6..81be3af 100644 --- a/src/eyreish/into_diagnostic.rs +++ b/src/eyreish/into_diagnostic.rs @@ -1,12 +1,20 @@ -use thiserror::Error; +use std::{error::Error, fmt::Display}; use crate::{Diagnostic, Report}; /// Convenience [`Diagnostic`] that can be used as an "anonymous" wrapper for /// Errors. This is intended to be paired with [`IntoDiagnostic`]. -#[derive(Debug, Error)] -#[error(transparent)] +#[derive(Debug)] pub(crate) struct DiagnosticError(pub(crate) Box); + +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 {} + impl Diagnostic for DiagnosticError {} /** @@ -31,3 +39,20 @@ impl IntoDiagnostic for R self.map_err(|e| DiagnosticError(Box::new(e)).into()) } } + +#[cfg(test)] +mod tests { + use std::io; + + use super::*; + + #[test] + fn diagnostic_error() { + let io_error: Result<(), _> = + Err(io::Error::new(io::ErrorKind::Other, "halt and catch fire")); + let diagnostic_error = io_error.into_diagnostic().unwrap_err(); + + assert_eq!(diagnostic_error.to_string(), "halt and catch fire"); + assert_eq!(diagnostic_error.source().map(ToString::to_string), None); + } +}