summaryrefslogtreecommitdiffstats
path: root/buffered-reader/src/file_error.rs
blob: add00e6b9aa047e64fcd7b907bb28e8edffa79cc (plain)
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
/// Common error type for file operations.

use std::error::Error;
use std::fmt;
use std::io;
use std::path::{Path, PathBuf};

/// Common error type for file operations.
#[derive(Debug)]
pub(crate) struct FileError {
    path: PathBuf,
    source: io::Error,
}

#[allow(clippy::new_ret_no_self)]
impl FileError {
    /// Returns a new `io::Error` backed by a `FileError`.
    pub fn new<P: AsRef<Path>>(path: P, source: io::Error) -> io::Error {
        io::Error::new(source.kind(), FileError {
            path: path.as_ref().into(),
            source,
        })
    }
}

impl fmt::Display for FileError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Reading {:?}: {}", self.path.display(), self.source)
    }
}

impl Error for FileError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.source)
    }
}