dirscent 0.1.0

Directory descent
Documentation
use std::path::{Path, PathBuf};

/// [`std::io::Error`] with the path that caused it.
#[derive(Debug)]
pub struct Error {
    io_error: std::io::Error,
    path: PathBuf,
}

impl Error {
    /// Creates an error from the path and IO error.
    pub fn new(io_error: std::io::Error, path: PathBuf) -> Self {
        Error { io_error, path }
    }

    /// Returns the underlying IO error in its full glory.
    pub fn io_error(&self) -> &std::io::Error {
        &self.io_error
    }

    /// Returns the full path to the cause of the error.
    pub fn path(&self) -> &Path {
        &self.path
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.path.display(), self.io_error)
    }
}

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