|
1 | | -use std::{fmt, io}; |
2 | | - |
3 | | -use thiserror::Error; |
| 1 | +use std::{ |
| 2 | + error::Error, |
| 3 | + fmt::{self, Display}, |
| 4 | + io, |
| 5 | +}; |
4 | 6 |
|
5 | 7 | use crate::Diagnostic; |
6 | 8 |
|
7 | 9 | /** |
8 | 10 | Error enum for miette. Used by certain operations in the protocol. |
9 | 11 | */ |
10 | | -#[derive(Debug, Error)] |
| 12 | +#[derive(Debug)] |
11 | 13 | pub enum MietteError { |
12 | 14 | /// Wrapper around [`std::io::Error`]. This is returned when something went |
13 | 15 | /// wrong while reading a [`SourceCode`](crate::SourceCode). |
14 | | - #[error(transparent)] |
15 | | - IoError(#[from] io::Error), |
| 16 | + IoError(io::Error), |
16 | 17 |
|
17 | 18 | /// Returned when a [`SourceSpan`](crate::SourceSpan) extends beyond the |
18 | 19 | /// bounds of a given [`SourceCode`](crate::SourceCode). |
19 | | - #[error("The given offset is outside the bounds of its Source")] |
20 | 20 | OutOfBounds, |
21 | 21 | } |
22 | 22 |
|
| 23 | +impl Display for MietteError { |
| 24 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 25 | + match self { |
| 26 | + MietteError::IoError(error) => write!(f, "{error}"), |
| 27 | + MietteError::OutOfBounds => { |
| 28 | + write!(f, "The given offset is outside the bounds of its Source") |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Error for MietteError { |
| 35 | + fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 36 | + match self { |
| 37 | + MietteError::IoError(error) => error.source(), |
| 38 | + MietteError::OutOfBounds => None, |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl From<io::Error> for MietteError { |
| 44 | + fn from(value: io::Error) -> Self { |
| 45 | + Self::IoError(value) |
| 46 | + } |
| 47 | +} |
| 48 | + |
23 | 49 | impl Diagnostic for MietteError { |
24 | 50 | fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> { |
25 | 51 | match self { |
@@ -49,3 +75,51 @@ impl Diagnostic for MietteError { |
49 | 75 | ))) |
50 | 76 | } |
51 | 77 | } |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +pub(crate) mod tests { |
| 81 | + use std::{error::Error, io::ErrorKind}; |
| 82 | + |
| 83 | + use super::*; |
| 84 | + |
| 85 | + #[derive(Debug)] |
| 86 | + pub(crate) struct TestError(pub(crate) io::Error); |
| 87 | + |
| 88 | + impl Display for TestError { |
| 89 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 90 | + write!(f, "testing, testing...") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + impl Error for TestError { |
| 95 | + fn source(&self) -> Option<&(dyn Error + 'static)> { |
| 96 | + Some(&self.0) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn io_error() { |
| 102 | + let inner_error = io::Error::new(ErrorKind::Other, "halt and catch fire"); |
| 103 | + let outer_error = TestError(inner_error); |
| 104 | + let io_error = io::Error::new(ErrorKind::Other, outer_error); |
| 105 | + |
| 106 | + let miette_error = MietteError::from(io_error); |
| 107 | + |
| 108 | + assert_eq!(miette_error.to_string(), "testing, testing..."); |
| 109 | + assert_eq!( |
| 110 | + miette_error.source().unwrap().to_string(), |
| 111 | + "halt and catch fire" |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn out_of_bounds() { |
| 117 | + let miette_error = MietteError::OutOfBounds; |
| 118 | + |
| 119 | + assert_eq!( |
| 120 | + miette_error.to_string(), |
| 121 | + "The given offset is outside the bounds of its Source" |
| 122 | + ); |
| 123 | + assert_eq!(miette_error.source().map(ToString::to_string), None); |
| 124 | + } |
| 125 | +} |
0 commit comments