serde_loader/
error.rs

1use crate::common::*;
2
3#[derive(Debug)]
4pub enum Error<E1 = Infallible, E2 = Infallible> {
5    Io(io::Error),
6    FileDump(FileDumpError<E1>),
7    FileLoad(FileLoadError<E2>),
8}
9
10impl<E1, E2> StdError for Error<E1, E2>
11where
12    E1: fmt::Debug + fmt::Display,
13    E2: fmt::Debug + fmt::Display,
14{
15}
16
17impl<E1, E2> fmt::Display for Error<E1, E2>
18where
19    E1: fmt::Display,
20    E2: fmt::Display,
21{
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Error::Io(err) => write!(f, "{}", err),
25            Error::FileDump(err) => write!(f, "{}", err),
26            Error::FileLoad(err) => write!(f, "{}", err),
27        }
28    }
29}
30
31impl<E1, E2> From<FileLoadError<E2>> for Error<E1, E2> {
32    fn from(v: FileLoadError<E2>) -> Self {
33        Self::FileLoad(v)
34    }
35}
36
37impl<E1, E2> From<FileDumpError<E1>> for Error<E1, E2> {
38    fn from(v: FileDumpError<E1>) -> Self {
39        Self::FileDump(v)
40    }
41}
42
43impl<E1, E2> From<io::Error> for Error<E1, E2> {
44    fn from(v: io::Error) -> Self {
45        Self::Io(v)
46    }
47}
48
49#[derive(Debug)]
50pub struct FileLoadError<E> {
51    pub path: PathBuf,
52    pub error: E,
53}
54
55impl<E> StdError for FileLoadError<E> where E: StdError {}
56
57impl<E> fmt::Display for FileLoadError<E>
58where
59    E: fmt::Display,
60{
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        write!(
63            f,
64            "unable to load file '{}'\n{}",
65            self.path.display(),
66            self.error
67        )
68    }
69}
70
71#[derive(Debug)]
72pub struct FileDumpError<E> {
73    pub path: PathBuf,
74    pub error: E,
75}
76
77impl<E> StdError for FileDumpError<E> where E: StdError {}
78
79impl<E> fmt::Display for FileDumpError<E>
80where
81    E: fmt::Display,
82{
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(
85            f,
86            "unable to save to file '{}'\n{}",
87            self.path.display(),
88            self.error
89        )
90    }
91}