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
#[derive(Debug)]
pub struct IoError {
    pub(crate) original: std::io::Error,
    pub(crate) path: Option<std::path::PathBuf>,
    #[cfg(feature = "backtrace")]
    pub(crate) backtrace: backtrace::Backtrace,
}

pub type IoResult<T> = Result<T, IoError>;

impl std::error::Error for IoError {}

impl std::fmt::Display for IoError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let path = if let Some(path) = &self.path {
            path.to_string_lossy()
        } else {
            "(unknown path)".into()
        };

        cfg_if::cfg_if! {
            if #[cfg(feature = "backtrace")] {
                write!(
                    f,
                    "ffs::IoError at path '{}': {}\nbacktrace:\n{:?}",
                    path, self.original, self.backtrace
                )
            } else {
                write!(
                    f,
                    "ffs::IoError at path '{}': {}",
                    path, self.original
                )
            }
        }
    }
}

impl IoError {
    pub fn into_inner(self) -> std::io::Error {
        self.original
    }

    pub fn new(original: std::io::Error, path: std::path::PathBuf) -> Self {
        let path = Some(path);
        cfg_if::cfg_if! {
            if #[cfg(feature = "backtrace")] {
                Self {original, path, backtrace: backtrace::Backtrace::new() }
            } else {
                Self {original, path }
            }
        }
    }
}