use crate::Config;
use std::error::Error as StdError;
use std::path::PathBuf;
use std::result::Result as StdResult;
use std::{self, fmt, io};
pub type Result<T> = StdResult<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
Generic(String),
Io(io::Error),
PathNotFound,
WatchNotFound,
InvalidConfig(Config),
MaxFilesWatch,
}
#[derive(Debug)]
pub struct Error {
pub kind: ErrorKind,
pub paths: Vec<PathBuf>,
}
impl Error {
pub fn add_path(mut self, path: PathBuf) -> Self {
self.paths.push(path);
self
}
pub fn set_paths(mut self, paths: Vec<PathBuf>) -> Self {
self.paths = paths;
self
}
pub fn new(kind: ErrorKind) -> Self {
Self {
kind,
paths: Vec::new(),
}
}
pub fn generic(msg: &str) -> Self {
Self::new(ErrorKind::Generic(msg.into()))
}
pub fn io(err: io::Error) -> Self {
Self::new(ErrorKind::Io(err))
}
pub fn path_not_found() -> Self {
Self::new(ErrorKind::PathNotFound)
}
pub fn watch_not_found() -> Self {
Self::new(ErrorKind::WatchNotFound)
}
pub fn invalid_config(config: &Config) -> Self {
Self::new(ErrorKind::InvalidConfig(config.clone()))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let error = match self.kind {
ErrorKind::PathNotFound => "No path was found.".into(),
ErrorKind::WatchNotFound => "No watch was found.".into(),
ErrorKind::InvalidConfig(ref config) => format!("Invalid configuration: {:?}", config),
ErrorKind::Generic(ref err) => err.clone(),
ErrorKind::Io(ref err) => err.to_string(),
ErrorKind::MaxFilesWatch => "OS file watch limit reached.".into(),
};
if self.paths.is_empty() {
write!(f, "{}", error)
} else {
write!(f, "{} about {:?}", error, self.paths)
}
}
}
impl StdError for Error {
fn cause(&self) -> Option<&dyn StdError> {
match self.kind {
ErrorKind::Io(ref cause) => Some(cause),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::io(err)
}
}
#[cfg(feature = "crossbeam-channel")]
impl<T> From<crossbeam_channel::SendError<T>> for Error {
fn from(err: crossbeam_channel::SendError<T>) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
#[cfg(not(feature = "crossbeam-channel"))]
impl<T> From<std::sync::mpsc::SendError<T>> for Error {
fn from(err: std::sync::mpsc::SendError<T>) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
#[cfg(feature = "crossbeam-channel")]
impl From<crossbeam_channel::RecvError> for Error {
fn from(err: crossbeam_channel::RecvError) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
#[cfg(not(feature = "crossbeam-channel"))]
impl From<std::sync::mpsc::RecvError> for Error {
fn from(err: std::sync::mpsc::RecvError) -> Self {
Error::generic(&format!("internal channel disconnect: {:?}", err))
}
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(err: std::sync::PoisonError<T>) -> Self {
Error::generic(&format!("internal mutex poisoned: {:?}", err))
}
}
#[test]
fn display_formatted_errors() {
let expected = "Some error";
assert_eq!(expected, format!("{}", Error::generic(expected)));
assert_eq!(
expected,
format!(
"{}",
Error::io(io::Error::new(io::ErrorKind::Other, expected))
)
);
}