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
use std::fmt;
use std::io;
use std::path;
pub type Result<T> = std::result::Result<T, WrappedError>;
#[derive(Debug, Default)]
pub struct WrappedError {
pub(crate) inner: Repr,
}
#[derive(Debug)]
pub(crate) enum Repr {
IO {
base: io::Error,
file: path::PathBuf,
},
Simple(Kind),
}
#[derive(Debug)]
pub enum Kind {
Unknown,
}
impl Default for Repr {
fn default() -> Self {
Repr::Simple(Kind::Unknown)
}
}
impl WrappedError {
pub fn into_inner(self) -> Option<Box<dyn std::error::Error>> {
match self.inner {
Repr::IO { base, .. } => Some(Box::new(base)),
Repr::Simple(_) => None,
}
}
pub fn get_io_error(&self) -> Option<&io::Error> {
match &self.inner {
Repr::IO { base, .. } => Some(base),
_ => None,
}
}
pub fn from(kind: Kind) -> Self {
Self {
inner: Repr::Simple(kind),
}
}
pub fn io_error(base: io::Error, file: impl AsRef<path::Path>) -> Self {
Self {
inner: Repr::IO {
file: file.as_ref().to_owned(),
base,
},
}
}
}
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Kind::Unknown => write!(f, "Unknown error"),
}
}
}
impl fmt::Display for WrappedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.inner {
#[cfg(feature = "fs")]
Repr::IO { base, file } => {
write!(f, "{}, file: {:?}", base.to_string(), file.as_os_str())
}
Repr::Simple(kind) => write!(f, "{}", kind.to_string()),
}
}
}
impl std::error::Error for WrappedError {}