codeprism_utils/
error.rs

1//! Error types for CodePrism utilities
2
3use thiserror::Error;
4
5/// Result type alias for utility operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Lightweight error types for utility operations
9#[derive(Error, Debug)]
10pub enum Error {
11    /// File watcher errors
12    #[cfg(feature = "file-watcher")]
13    #[error("File watcher error: {0}")]
14    Watcher(String),
15
16    /// I/O errors
17    #[error("I/O error: {0}")]
18    Io(#[from] std::io::Error),
19
20    /// General utility errors
21    #[error("Utility error: {0}")]
22    Utility(String),
23}
24
25impl Error {
26    /// Create a file watcher error
27    #[cfg(feature = "file-watcher")]
28    pub fn watcher<S: Into<String>>(msg: S) -> Self {
29        Self::Watcher(msg.into())
30    }
31
32    /// Create a general utility error
33    pub fn utility<S: Into<String>>(msg: S) -> Self {
34        Self::Utility(msg.into())
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_error_display() {
44        let err = Error::utility("test error");
45        assert_eq!(err.to_string(), "Utility error: test error");
46    }
47
48    #[test]
49    fn test_error_from_io() {
50        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
51        let err = Error::from(io_err);
52        assert!(matches!(err, Error::Io(_)));
53    }
54
55    #[cfg(feature = "file-watcher")]
56    #[test]
57    fn test_watcher_error() {
58        let err = Error::watcher("test watcher error");
59        assert!(matches!(err, Error::Watcher(_)));
60        assert_eq!(err.to_string(), "File watcher error: test watcher error");
61    }
62}