diff --git a/lib/src/error.rs b/lib/src/error.rs index 179a119..611a853 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -10,3 +10,9 @@ pub use specialised::*; mod critical; mod runtime; mod specialised; + +/// Helper trait to construct specific IO errors from generic ones. +pub trait SpecificIoError { + /// Add some context to the error or result. + fn about(self, context: &'static str) -> Output; +} diff --git a/lib/src/error/critical.rs b/lib/src/error/critical.rs index 85227b9..8ce356e 100644 --- a/lib/src/error/critical.rs +++ b/lib/src/error/critical.rs @@ -4,7 +4,7 @@ use tokio::{sync::mpsc, task::JoinError}; use crate::event::Event; -use super::RuntimeError; +use super::{RuntimeError, SpecificIoError}; /// Errors which are not recoverable and stop watchexec execution. #[derive(Debug, Diagnostic, Error)] @@ -63,3 +63,18 @@ pub enum CriticalError { #[diagnostic(code(watchexec::critical::internal::missing_handler))] MissingHandler, } + +impl SpecificIoError> for Result { + fn about(self, context: &'static str) -> Result { + self.map_err(|err| err.about(context)) + } +} + +impl SpecificIoError for std::io::Error { + fn about(self, context: &'static str) -> CriticalError { + CriticalError::IoError { + about: context, + err: self, + } + } +} diff --git a/lib/src/error/runtime.rs b/lib/src/error/runtime.rs index eb61e39..1dfbadb 100644 --- a/lib/src/error/runtime.rs +++ b/lib/src/error/runtime.rs @@ -6,6 +6,8 @@ use tokio::sync::mpsc; use crate::{event::Event, fs::Watcher, signal::process::SubSignal}; +use super::SpecificIoError; + /// Errors which _may_ be recoverable, transient, or only affect a part of the operation, and should /// be reported to the user and/or acted upon programatically, but will not outright stop watchexec. #[derive(Debug, Diagnostic, Error)] @@ -225,3 +227,18 @@ pub enum RuntimeError { #[diagnostic(code(watchexec::runtime::set))] Set(#[related] Vec), } + +impl SpecificIoError> for Result { + fn about(self, context: &'static str) -> Result { + self.map_err(|err| err.about(context)) + } +} + +impl SpecificIoError for std::io::Error { + fn about(self, context: &'static str) -> RuntimeError { + RuntimeError::IoError { + about: context, + err: self, + } + } +}