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
use crate::common::*;

#[derive(Debug)]
pub enum Error<E1 = Infallible, E2 = Infallible> {
    Io(io::Error),
    FileDump(FileDumpError<E1>),
    FileLoad(FileLoadError<E2>),
}

impl<E1, E2> StdError for Error<E1, E2>
where
    E1: fmt::Debug + fmt::Display,
    E2: fmt::Debug + fmt::Display,
{
}

impl<E1, E2> fmt::Display for Error<E1, E2>
where
    E1: fmt::Display,
    E2: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Io(err) => write!(f, "{}", err),
            Error::FileDump(err) => write!(f, "{}", err),
            Error::FileLoad(err) => write!(f, "{}", err),
        }
    }
}

impl<E1, E2> From<FileLoadError<E2>> for Error<E1, E2> {
    fn from(v: FileLoadError<E2>) -> Self {
        Self::FileLoad(v)
    }
}

impl<E1, E2> From<FileDumpError<E1>> for Error<E1, E2> {
    fn from(v: FileDumpError<E1>) -> Self {
        Self::FileDump(v)
    }
}

impl<E1, E2> From<io::Error> for Error<E1, E2> {
    fn from(v: io::Error) -> Self {
        Self::Io(v)
    }
}

#[derive(Debug)]
pub struct FileLoadError<E> {
    pub path: PathBuf,
    pub error: E,
}

impl<E> StdError for FileLoadError<E> where E: StdError {}

impl<E> fmt::Display for FileLoadError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "unable to load file '{}'\n{}",
            self.path.display(),
            self.error
        )
    }
}

#[derive(Debug)]
pub struct FileDumpError<E> {
    pub path: PathBuf,
    pub error: E,
}

impl<E> StdError for FileDumpError<E> where E: StdError {}

impl<E> fmt::Display for FileDumpError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "unable to save to file '{}'\n{}",
            self.path.display(),
            self.error
        )
    }
}