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
use std::fmt::{self, Debug, Display};
use std::io;
use std::path::Path;
pub struct Error {
pub(crate) e: Box<ErrorImpl>,
}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) struct ErrorImpl {
pub(crate) path: Option<&'static Path>,
kind: ErrorKind,
}
pub(crate) enum ErrorKind {
Msg(String),
Csv(csv::Error),
Io(io::Error),
Json(serde_json::Error),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(path) = self.e.path {
write!(f, "{}.csv: ", path.display())?;
}
match &self.e.kind {
ErrorKind::Msg(e) => f.write_str(e),
ErrorKind::Io(e) => write!(f, "{}", e),
ErrorKind::Csv(e) => write!(f, "{}", e),
ErrorKind::Json(e) => write!(f, "{}", e),
}
}
}
impl Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "db_dump::Error({:?})", self.to_string())
}
}
pub(crate) fn err(variant: impl Into<ErrorKind>) -> Error {
Error {
e: Box::new(ErrorImpl {
path: None,
kind: variant.into(),
}),
}
}
impl<'a> From<fmt::Arguments<'a>> for ErrorKind {
fn from(e: fmt::Arguments) -> Self {
ErrorKind::Msg(e.to_string())
}
}
impl From<csv::Error> for ErrorKind {
fn from(e: csv::Error) -> Self {
ErrorKind::Csv(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error {
e: Box::new(ErrorImpl {
path: None,
kind: ErrorKind::Io(e),
}),
}
}
}
impl From<serde_json::Error> for ErrorKind {
fn from(e: serde_json::Error) -> Self {
ErrorKind::Json(e)
}
}