summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2023-12-19 16:23:29 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2023-12-19 16:23:29 +1300
commit47b25c05a6be51b93c909d38a19440d1c04ba2f8 (patch)
tree448c23c6d58a8407de45a4730cf972bd83713aa0 /src/error.rs
parent8f410d1ead74b979481f1488a4dcddd33ea829c7 (diff)
downloadvagabond-47b25c05a6be51b93c909d38a19440d1c04ba2f8.zip
Large collection of changesv1.0.0
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs87
1 files changed, 54 insertions, 33 deletions
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<IoError> 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<EntryReadError> 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<T>(io_result: Result<T, IoError>, path: &Path) -> ReadResult<T> {
+ 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<IoError> 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<T>(io_result: Result<T, IoError>, path: &Path) -> WriteResult<T> {
+ 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<EntryErrorKind, IoErrorKind> {
+ 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)
+ }
+}