From 47b25c05a6be51b93c909d38a19440d1c04ba2f8 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 19 Dec 2023 16:23:29 +1300 Subject: Large collection of changes --- src/error.rs | 87 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 33 deletions(-) (limited to 'src/error.rs') diff --git a/src/error.rs b/src/error.rs index 0e8d99f..be0ebb7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,48 +1,69 @@ -use std::io::Error as IoError; -use std::io::ErrorKind; +use std::io::{ErrorKind as IoErrorKind, Error as IoError}; +use crate::*; -#[derive(Debug)] -pub enum EntryReadError { +#[derive(Debug, PartialEq)] +pub enum EntryErrorKind { NotFound, PermissionDenied, } -impl From for EntryReadError { - fn from(io_error: IoError) -> Self { - match io_error.kind() { - ErrorKind::NotFound => Self::NotFound, - // An intermediate path component was a plain file, not a directory - ErrorKind::NotADirectory => Self::NotFound, - // A cyclic symbolic link chain was included in the provided path - ErrorKind::FilesystemLoop => Self::NotFound, - ErrorKind::PermissionDenied => Self::PermissionDenied, - err => panic!("Unexpected IoError encountered: {:?}", err), - } - } + +pub struct EntryReadError { + pub path: PathBuf, + pub error_kind: EntryErrorKind, } -#[derive(Debug)] -pub enum EntryWriteError { - NotFound, - PermissionDenied, +pub struct EntryWriteError { + pub path: PathBuf, + pub error_kind: EntryErrorKind, } + impl From for EntryWriteError { fn from(error: EntryReadError) -> Self { - match error { - EntryReadError::NotFound => EntryWriteError::NotFound, - EntryReadError::PermissionDenied => EntryWriteError::PermissionDenied, + EntryWriteError { path: error.path, error_kind: error.error_kind } + } +} + +pub(crate) fn io_result_to_read_result(io_result: Result, path: &Path) -> ReadResult { + match io_result { + Ok(t) => Ok(t), + Err(io_error) => { + match io_error_to_entry_error(io_error) { + Ok(error_kind) => Err( EntryReadError { path: path.to_path_buf(), error_kind }), + Err(err) => panic!("Unexpected IO error while attempting to read from {path:?}: {err:?}"), + } } } } -impl From for EntryWriteError { - fn from(io_error: IoError) -> Self { - match io_error.kind() { - ErrorKind::NotFound => Self::NotFound, - // An intermediate path component was a plain file, not a directory - ErrorKind::NotADirectory => Self::NotFound, - // A cyclic symbolic link chain was included in the provided path - ErrorKind::FilesystemLoop => Self::NotFound, - ErrorKind::PermissionDenied => Self::PermissionDenied, - err => panic!("Unexpected IoError encountered: {:?}", err), +pub(crate) fn io_result_to_write_result(io_result: Result, path: &Path) -> WriteResult { + match io_result { + Ok(t) => Ok(t), + Err(io_error) => { + match io_error_to_entry_error(io_error) { + Ok(error_kind) => Err( EntryWriteError { path: path.to_path_buf(), error_kind }), + Err(err) => panic!("Unexpected IO error while attempting to write to {path:?}: {err:?}"), + } } } } +fn io_error_to_entry_error(io_error: IoError) -> Result { + match io_error.kind() { + IoErrorKind::NotFound => Ok(EntryErrorKind::NotFound), + // An intermediate path component was a plain file, not a directory + IoErrorKind::NotADirectory => Ok(EntryErrorKind::NotFound), + // A cyclic symbolic link chain was included in the provided path + IoErrorKind::FilesystemLoop => Ok(EntryErrorKind::NotFound), + IoErrorKind::PermissionDenied => Ok(EntryErrorKind::PermissionDenied), + err => Err(err), + } +} + +impl std::fmt::Debug for EntryReadError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "Error while attempting to read from file '{:?}': {:?}", self.path, self.error_kind) + } +} +impl std::fmt::Debug for EntryWriteError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "Error while attempting to write to file '{:?}': {:?}", self.path, self.error_kind) + } +} -- cgit v1.2.3-70-g09d2