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
use std::{fmt, io};
pub type Result<T> = std::result::Result<T, ErrorKind>;
#[derive(Debug)]
pub enum ErrorKind {
IoError(io::Error),
Interrupted,
Eof,
}
impl ErrorKind {
pub fn map_terminated<F: FnOnce(bool) -> io::Error>(self, f: F) -> io::Error {
match self {
ErrorKind::IoError(e) => e,
ErrorKind::Interrupted => f(true),
ErrorKind::Eof => f(false),
}
}
}
impl std::error::Error for ErrorKind {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ErrorKind::IoError(e) => Some(e),
ErrorKind::Interrupted => None,
ErrorKind::Eof => None,
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorKind::IoError(e) => write!(fmt, "IoError: {}", e),
ErrorKind::Interrupted => write!(fmt, "CTRL+C"),
ErrorKind::Eof => write!(fmt, "EOF"),
}
}
}
impl From<io::Error> for ErrorKind {
fn from(e: io::Error) -> Self {
Self::IoError(e)
}
}