impl std error for directory errors

This commit is contained in:
Ashley Mannix
2017-05-28 19:45:50 +10:00
committed by Paul Masurel
parent 45595234cc
commit d8a7c428f7

View File

@@ -1,3 +1,4 @@
use std::error::Error as StdError;
use std::path::PathBuf;
use std::io;
use std::fmt;
@@ -18,6 +19,16 @@ impl fmt::Display for IOError {
}
}
impl StdError for IOError {
fn description(&self) -> &str {
"io error occurred"
}
fn cause(&self) -> Option<&StdError> {
Some(&self.err)
}
}
impl IOError {
pub(crate) fn with_path(
path: PathBuf,
@@ -48,6 +59,25 @@ pub enum OpenDirectoryError {
NotADirectory(PathBuf),
}
impl fmt::Display for OpenDirectoryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OpenDirectoryError::DoesNotExist(ref path) => write!(f, "the underlying directory '{:?}' does not exist", path),
OpenDirectoryError::NotADirectory(ref path) => write!(f, "the path '{:?}' exists but is not a directory", path)
}
}
}
impl StdError for OpenDirectoryError {
fn description(&self) -> &str {
"error occurred while opening a directory"
}
fn cause(&self) -> Option<&StdError> {
None
}
}
/// Error that may occur when starting to write in a file
#[derive(Debug)]
pub enum OpenWriteError {
@@ -65,6 +95,28 @@ impl From<IOError> for OpenWriteError {
}
}
impl fmt::Display for OpenWriteError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OpenWriteError::FileAlreadyExists(ref path) => write!(f, "the file '{:?}' already exists", path),
OpenWriteError::IOError(ref err) => write!(f, "an io error occurred while opening a file for writing: '{}'", err)
}
}
}
impl StdError for OpenWriteError {
fn description(&self) -> &str {
"error occurred while opening a file for writing"
}
fn cause(&self) -> Option<&StdError> {
match *self {
OpenWriteError::FileAlreadyExists(_) => None,
OpenWriteError::IOError(ref err) => Some(err)
}
}
}
/// Error that may occur when accessing a file read
#[derive(Debug)]
pub enum OpenReadError {
@@ -81,6 +133,28 @@ impl From<IOError> for OpenReadError {
}
}
impl fmt::Display for OpenReadError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
OpenReadError::FileDoesNotExist(ref path) => write!(f, "the file '{:?}' does not exist", path),
OpenReadError::IOError(ref err) => write!(f, "an io error occurred while opening a file for reading: '{}'", err)
}
}
}
impl StdError for OpenReadError {
fn description(&self) -> &str {
"error occurred while opening a file for reading"
}
fn cause(&self) -> Option<&StdError> {
match *self {
OpenReadError::FileDoesNotExist(_) => None,
OpenReadError::IOError(ref err) => Some(err)
}
}
}
/// Error that may occur when trying to delete a file
#[derive(Debug)]
pub enum DeleteError {
@@ -99,3 +173,27 @@ impl From<IOError> for DeleteError {
DeleteError::IOError(err)
}
}
impl fmt::Display for DeleteError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
DeleteError::FileDoesNotExist(ref path) => write!(f, "the file '{:?}' does not exist", path),
DeleteError::FileProtected(ref path) => write!(f, "the file '{:?}' is protected and can't be deleted", path),
DeleteError::IOError(ref err) => write!(f, "an io error occurred while opening a file for reading: '{}'", err)
}
}
}
impl StdError for DeleteError {
fn description(&self) -> &str {
"error occurred while opening a file for reading"
}
fn cause(&self) -> Option<&StdError> {
match *self {
DeleteError::FileDoesNotExist(_) => None,
DeleteError::FileProtected(ref path) => None,
DeleteError::IOError(ref err) => Some(err)
}
}
}