xvc_walker/
error.rs

1//! Error codes and messages for Xvc Walker
2use log::{debug, error, info, trace, warn};
3use std::hash::Hash;
4use thiserror::Error as ThisError;
5
6#[allow(missing_docs)]
7#[derive(ThisError, Debug)]
8/// Error type for Xvc Walker
9pub enum Error {
10    #[error("General Xvc Walker Error: {source}")]
11    AnyhowError {
12        #[from]
13        source: anyhow::Error,
14    },
15    #[error("Crossbeam Send Error for Type: {t:?} {cause:?}")]
16    CrossbeamSendError { t: String, cause: String },
17
18    #[error("Ignore rules poisoned")]
19    LockPoisonError { t: String, cause: String },
20
21    #[error("File System Notify Error: {source:?}")]
22    NotifyError {
23        #[from]
24        source: notify::Error,
25    },
26
27    #[error("I/O Error: {source}")]
28    IoError {
29        #[from]
30        source: std::io::Error,
31    },
32
33    #[error("Cannot Merge Empty Ignore Rules")]
34    CannotMergeEmptyIgnoreRules,
35}
36
37impl Hash for Error {
38    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
39        format!("{}", self).hash(state);
40    }
41}
42
43impl PartialEq for Error {
44    fn eq(&self, other: &Self) -> bool {
45        format!("{}", self).eq(&format!("{}", other))
46    }
47}
48
49impl<T> From<crossbeam_channel::SendError<T>> for Error
50where
51    T: std::fmt::Debug,
52{
53    fn from(e: crossbeam_channel::SendError<T>) -> Self {
54        Error::CrossbeamSendError {
55            t: format!("{:#?}", e.0),
56            cause: e.to_string(),
57        }
58    }
59}
60impl<T> From<std::sync::PoisonError<T>> for Error
61where
62    T: std::fmt::Debug,
63{
64    fn from(e: std::sync::PoisonError<T>) -> Self {
65        Error::LockPoisonError {
66            t: format!("{:#?}", e),
67            cause: e.to_string(),
68        }
69    }
70}
71
72impl Error {
73    /// print [DEBUG] message for [Error]
74    pub fn debug(self) -> Self {
75        debug!("{}", self);
76        self
77    }
78    /// print [TRACE] message for [Error]
79    pub fn trace(self) -> Self {
80        trace!("{}", self);
81        self
82    }
83    /// print [WARN] message for [Error]
84    pub fn warn(self) -> Self {
85        warn!("{}", self);
86        self
87    }
88    /// print [ERROR] message for [Error]
89    pub fn error(self) -> Self {
90        error!("{}", self);
91        self
92    }
93    /// print [INFO] message for [Error]
94    pub fn info(self) -> Self {
95        info!("{}", self);
96        self
97    }
98    /// print [PANIC] message for [Error] and exit!
99    pub fn panic(self) -> Self {
100        panic!("{}", self);
101    }
102}
103
104/// Result type for xvc-walker that may also return [Error]
105pub type Result<T> = std::result::Result<T, Error>;