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
126
127
128
129
130
131
132
133
134
135
use std::fmt;
use std::io;
use std::process;
use std::sync::atomic::{AtomicBool, Ordering};
use chrono::ParseError;
use clap;
use csv;
macro_rules! write_error {
($($arg:tt)*) => ({
use std::io::{Write, stderr};
(writeln!(&mut stderr(), $($arg)*)).unwrap();
});
}
pub type CliResult<T> = Result<T, Error>;
#[derive(Debug)]
pub enum Error {
CliParsing(clap::Error),
Csv(csv::Error),
Io(io::Error),
TimeParsing(ParseError),
Custom(String),
}
impl Error {
pub fn exit(&mut self) -> ! {
match &self {
Error::CliParsing(err) => err.exit(),
Error::Csv(err) => write_error!("{}", err),
Error::Io(ref err) if err.kind() == io::ErrorKind::BrokenPipe => {}
Error::Io(err) => write_error!("{}", err),
Error::TimeParsing(err) => write_error!("{}", err),
Error::Custom(s) => write_error!("Error: {}", s),
}
process::exit(1);
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self {
Error::CliParsing(ref e) => e.fmt(f),
Error::Csv(ref e) => e.fmt(f),
Error::Io(ref e) => e.fmt(f),
Error::TimeParsing(ref e) => e.fmt(f),
Error::Custom(s) => f.write_str(format!("Error: {}", s).as_str()),
}
}
}
impl From<clap::Error> for Error {
fn from(err: clap::Error) -> Error {
print_backtrace();
Error::CliParsing(err)
}
}
impl From<csv::Error> for Error {
fn from(err: csv::Error) -> Error {
print_backtrace();
if !err.is_io_error() {
return Error::Csv(err);
}
match err.into_kind() {
csv::ErrorKind::Io(v) => From::from(v),
_ => unreachable!(),
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
print_backtrace();
Error::Io(err)
}
}
impl From<ParseError> for Error {
fn from(err: ParseError) -> Error {
print_backtrace();
Error::TimeParsing(err)
}
}
impl From<String> for Error {
fn from(err: String) -> Error {
print_backtrace();
Error::Custom(err)
}
}
impl<'a> From<&'a str> for Error {
fn from(err: &'a str) -> Error {
print_backtrace();
Error::Custom(err.to_owned())
}
}
pub fn handle_drive_error(cli_result: CliResult<()>) {
match cli_result {
Ok(()) => process::exit(0),
Err(e) => {
write_error!("");
match e {
Error::CliParsing(err) => write_error!("{}", err),
Error::Csv(err) => write_error!("{}", err),
Error::Io(ref err) if err.kind() == io::ErrorKind::BrokenPipe => {}
Error::Io(err) => write_error!("{}", err),
Error::TimeParsing(err) => write_error!("{}", err),
Error::Custom(s) => write_error!("Error: {} ", s),
}
process::exit(1)
}
}
}
static PRINT_BACKTRACE: AtomicBool = AtomicBool::new(false);
pub fn turn_on_backtrace() {
PRINT_BACKTRACE.store(true, Ordering::Relaxed);
}
fn print_backtrace() {
if !PRINT_BACKTRACE.load(Ordering::Relaxed) {
return;
}
use backtrace::Backtrace;
let bt = Backtrace::new();
eprintln!("{:?}", bt);
}