From 3b1c87134a4fec7f40f13bf650569f7ebe6b0ddd Mon Sep 17 00:00:00 2001 From: Brooks J Rady Date: Sat, 26 Apr 2025 15:56:36 +0100 Subject: [PATCH] refactor(miette): remove `thiserror` from `error.rs` --- src/error.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/error.rs b/src/error.rs index 4e57a78..05c8a93 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,25 +1,44 @@ -use std::{fmt, io}; - -use thiserror::Error; +use std::{ + error::Error, + fmt::{self, Display}, + io, +}; use crate::Diagnostic; /** Error enum for miette. Used by certain operations in the protocol. */ -#[derive(Debug, Error)] +#[derive(Debug)] pub enum MietteError { /// Wrapper around [`std::io::Error`]. This is returned when something went /// wrong while reading a [`SourceCode`](crate::SourceCode). - #[error(transparent)] - IoError(#[from] io::Error), + IoError(io::Error), /// Returned when a [`SourceSpan`](crate::SourceSpan) extends beyond the /// bounds of a given [`SourceCode`](crate::SourceCode). - #[error("The given offset is outside the bounds of its Source")] OutOfBounds, } +impl Display for MietteError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + MietteError::IoError(error) => write!(f, "{error}"), + MietteError::OutOfBounds => { + write!(f, "The given offset is outside the bounds of its Source") + } + } + } +} + +impl Error for MietteError {} + +impl From for MietteError { + fn from(value: io::Error) -> Self { + Self::IoError(value) + } +} + impl Diagnostic for MietteError { fn code<'a>(&'a self) -> Option> { match self { @@ -49,3 +68,30 @@ impl Diagnostic for MietteError { ))) } } + +#[cfg(test)] +mod tests { + use std::error::Error; + + use super::*; + + #[test] + fn io_error() { + let io_error = io::Error::new(io::ErrorKind::Other, "halt and catch fire"); + let miette_error = MietteError::from(io_error); + + assert_eq!(miette_error.to_string(), "halt and catch fire"); + assert_eq!(miette_error.source().map(ToString::to_string), None); + } + + #[test] + fn out_of_bounds() { + let miette_error = MietteError::OutOfBounds; + + assert_eq!( + miette_error.to_string(), + "The given offset is outside the bounds of its Source" + ); + assert_eq!(miette_error.source().map(ToString::to_string), None); + } +}