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
use std::error;
use std::fmt;
#[derive(Clone)]
pub enum ErrorKind {
TypeError,
NotFound,
}
impl ErrorKind {
pub fn description(&self) -> &'static str {
match self {
ErrorKind::TypeError => "型が複製できませんでした",
ErrorKind::NotFound => "存在しません",
}
}
}
pub struct Error {
_error: _Error,
}
impl Error {
fn new<E>(kind: ErrorKind, error: E) -> Self
where
E: Into<Box<dyn error::Error + Send + Sync>>,
{
Error {
_error: _Error::Custom((kind, error.into())),
}
}
}
enum _Error {
Simple(ErrorKind),
Custom((ErrorKind, Box<dyn error::Error + Send + Sync>)),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self._error {
_Error::Simple(s) => f.write_str(s.description()),
_Error::Custom(c) => f.write_str(c.0.description()),
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<Self as fmt::Display>::fmt(self, f)
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match &self._error {
_Error::Simple(_) => None,
_Error::Custom(c) => c.1.source(),
}
}
}