1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
use std::{
error::Error,
fmt::{self, Display},
io,
};
use crate::Diagnostic;
/**
Error enum for miette. Used by certain operations in the protocol.
*/
#[derive(Debug)]
pub enum MietteError {
/// Wrapper around [`std::io::Error`]. This is returned when something went
/// wrong while reading a [`SourceCode`](crate::SourceCode).
IoError(io::Error),
/// Returned when a [`SourceSpan`](crate::SourceSpan) extends beyond the
/// bounds of a given [`SourceCode`](crate::SourceCode).
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 {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
MietteError::IoError(error) => error.source(),
MietteError::OutOfBounds => None,
}
}
}
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 {
MietteError::IoError(_) => Some(Box::new("miette::io_error")),
MietteError::OutOfBounds => Some(Box::new("miette::span_out_of_bounds")),
}
}
fn help<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
match self {
MietteError::IoError(_) => None,
MietteError::OutOfBounds => Some(Box::new(
"Double-check your spans. Do you have an off-by-one error?",
)),
}
}
fn url<'a>(&'a self) -> Option<Box<dyn fmt::Display + 'a>> {
let crate_version = env!("CARGO_PKG_VERSION");
let variant = match self {
MietteError::IoError(_) => "#variant.IoError",
MietteError::OutOfBounds => "#variant.OutOfBounds",
};
Some(Box::new(format!(
"https://docs.rs/miette/{}/miette/enum.MietteError.html{}",
crate_version, variant,
)))
}
}
#[cfg(test)]
pub(crate) mod tests {
use std::{error::Error, io::ErrorKind};
use super::*;
#[derive(Debug)]
pub(crate) struct TestError(pub(crate) io::Error);
impl 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 io_error() {
let inner_error = io::Error::new(ErrorKind::Other, "halt and catch fire");
let outer_error = TestError(inner_error);
let io_error = io::Error::new(ErrorKind::Other, outer_error);
let miette_error = MietteError::from(io_error);
assert_eq!(miette_error.to_string(), "testing, testing...");
assert_eq!(
miette_error.source().unwrap().to_string(),
"halt and catch fire"
);
}
#[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);
}
}
|