mirror of https://github.com/zkat/miette.git
refactor(miette): remove `thiserror` from `error.rs`
This commit is contained in:
parent
69c507bfef
commit
3b1c87134a
60
src/error.rs
60
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<io::Error> for MietteError {
|
||||
fn from(value: io::Error) -> Self {
|
||||
Self::IoError(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Diagnostic for MietteError {
|
||||
fn code<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue