use std::fmt;
use std::path::PathBuf;
#[derive(Debug)]
#[non_exhaustive]
pub struct WatchError {
repr: WatchErrorRepr,
}
#[derive(Debug)]
enum WatchErrorRepr {
Create {
source: Box<dyn std::error::Error + Send + Sync>,
},
Watch {
path: PathBuf,
source: Box<dyn std::error::Error + Send + Sync>,
},
Runtime,
}
impl WatchError {
pub(crate) fn create(
source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> WatchError {
WatchError {
repr: WatchErrorRepr::Create {
source: source.into(),
},
}
}
pub(crate) fn watch(
path: PathBuf,
source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
) -> WatchError {
WatchError {
repr: WatchErrorRepr::Watch {
path,
source: source.into(),
},
}
}
pub(crate) fn runtime() -> WatchError {
WatchError {
repr: WatchErrorRepr::Runtime,
}
}
#[must_use]
pub fn path(&self) -> Option<&std::path::Path> {
match &self.repr {
WatchErrorRepr::Watch { path, .. } => Some(path.as_path()),
_ => None,
}
}
}
impl fmt::Display for WatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.repr {
WatchErrorRepr::Create { .. } => f.write_str("create the filesystem watcher"),
WatchErrorRepr::Watch { path, .. } => write!(f, "watch {}", path.display()),
WatchErrorRepr::Runtime => {
f.write_str("start a filesystem watch outside a tokio runtime")
}
}
}
}
impl std::error::Error for WatchError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.repr {
WatchErrorRepr::Create { source } | WatchErrorRepr::Watch { source, .. } => {
Some(source.as_ref())
}
WatchErrorRepr::Runtime => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum WatchErrorKind {
Create,
Watch,
Runtime,
}
impl WatchError {
#[must_use]
pub fn kind(&self) -> WatchErrorKind {
match &self.repr {
WatchErrorRepr::Create { .. } => WatchErrorKind::Create,
WatchErrorRepr::Watch { .. } => WatchErrorKind::Watch,
WatchErrorRepr::Runtime => WatchErrorKind::Runtime,
}
}
}