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
use std::fmt;
#[derive(Debug)]
pub struct Error {
inner: Inner,
}
#[derive(Debug)]
enum Inner {
#[cfg(feature = "std")]
Boxed(std_support::BoxedError),
Msg(&'static str),
Fmt,
}
impl Error {
pub fn msg(msg: &'static str) -> Self {
Error {
inner: Inner::Msg(msg),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Inner::*;
match &self.inner {
#[cfg(feature = "std")]
&Boxed(ref err) => err.fmt(f),
&Msg(ref msg) => msg.fmt(f),
&Fmt => fmt::Error.fmt(f),
}
}
}
impl From<fmt::Error> for Error {
fn from(_: fmt::Error) -> Self {
Error { inner: Inner::Fmt }
}
}
#[cfg(feature = "std")]
mod std_support {
use super::*;
use std::{error, io};
pub(super) type BoxedError = Box<dyn error::Error + Send + Sync>;
impl Error {
pub fn boxed<E>(err: E) -> Self
where
E: Into<BoxedError>,
{
Error {
inner: Inner::Boxed(err.into()),
}
}
}
impl error::Error for Error {}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::boxed(err)
}
}
}