watchexec/lib/src/error.rs

87 lines
2.4 KiB
Rust
Raw Normal View History

use std::{error::Error as StdError, fmt, io, sync::PoisonError};
pub type Result<T> = ::std::result::Result<T, Error>;
#[non_exhaustive]
pub enum Error {
Canonicalization(String, io::Error),
Glob(globset::Error),
Io(io::Error),
Notify(notify::Error),
2021-04-10 15:09:06 +02:00
Generic(String),
PoisonedLock,
ClearScreen(clearscreen::Error),
}
2021-04-10 15:09:06 +02:00
impl StdError for Error {}
2021-04-10 15:09:06 +02:00
impl From<String> for Error {
fn from(err: String) -> Self {
Self::Generic(err)
}
}
impl From<globset::Error> for Error {
fn from(err: globset::Error) -> Self {
2019-10-27 12:06:09 +01:00
Self::Glob(err)
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
2020-07-03 14:20:02 +02:00
Self::Io(match err.raw_os_error() {
2021-04-10 19:44:24 +02:00
Some(7) => io::Error::new(io::ErrorKind::Other, "There are so many changed files that the environment variables of the command have been overrun. Try running with --no-meta or --no-environment."),
_ => err,
2020-07-03 14:20:02 +02:00
})
}
}
impl From<notify::Error> for Error {
fn from(err: notify::Error) -> Self {
match err {
2019-10-27 12:06:09 +01:00
notify::Error::Io(err) => Self::Io(err),
other => Self::Notify(other),
}
}
}
impl<'a, T> From<PoisonError<T>> for Error {
fn from(_err: PoisonError<T>) -> Self {
2019-10-27 12:06:09 +01:00
Self::PoisonedLock
}
}
impl From<clearscreen::Error> for Error {
fn from(err: clearscreen::Error) -> Self {
match err {
clearscreen::Error::Io(err) => Self::Io(err),
other => Self::ClearScreen(other),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (error_type, error) = match self {
2019-10-27 12:31:52 +01:00
Self::Canonicalization(path, err) => (
"Path",
format!("couldn't canonicalize '{}':\n{}", path, err),
),
2021-04-10 15:09:06 +02:00
Self::Generic(err) => ("", err.clone()),
2019-10-27 12:06:09 +01:00
Self::Glob(err) => ("Globset", err.to_string()),
Self::Io(err) => ("I/O", err.to_string()),
Self::Notify(err) => ("Notify", err.to_string()),
Self::PoisonedLock => ("Internal", "poisoned lock".to_string()),
Self::ClearScreen(err) => ("ClearScreen", err.to_string()),
};
write!(f, "{} error: {}", error_type, error)
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}