Skip to main content

anno_core/core/
error.rs

1//! Error types for `anno::core`.
2
3use thiserror::Error;
4
5/// Result type for `anno::core` operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type for `anno::core` operations.
9#[derive(Error, Debug)]
10#[non_exhaustive]
11pub enum Error {
12    /// Invalid input provided.
13    #[error("Invalid input: {0}")]
14    InvalidInput(String),
15
16    /// IO error.
17    #[error("IO error: {0}")]
18    Io(#[from] std::io::Error),
19
20    /// Parse error.
21    #[error("Parse error: {0}")]
22    Parse(String),
23
24    /// Corpus operation error.
25    #[error("Corpus error: {0}")]
26    Corpus(String),
27
28    /// Track reference error.
29    #[error("Track reference error: {0}")]
30    TrackRef(String),
31}
32
33impl Error {
34    /// Create a track reference error.
35    #[must_use]
36    pub fn track_ref(msg: impl Into<String>) -> Self {
37        Self::TrackRef(msg.into())
38    }
39
40    /// Create a corpus error.
41    #[must_use]
42    pub fn corpus(msg: impl Into<String>) -> Self {
43        Self::Corpus(msg.into())
44    }
45
46    /// Create an invalid input error.
47    #[must_use]
48    pub fn invalid_input(msg: impl Into<String>) -> Self {
49        Self::InvalidInput(msg.into())
50    }
51
52    /// Create a parse error.
53    #[must_use]
54    pub fn parse(msg: impl Into<String>) -> Self {
55        Self::Parse(msg.into())
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_error_display() {
65        let e = Error::InvalidInput("bad data".into());
66        assert_eq!(e.to_string(), "Invalid input: bad data");
67
68        let e = Error::parse("unexpected token");
69        assert_eq!(e.to_string(), "Parse error: unexpected token");
70
71        let e = Error::corpus("document not found");
72        assert_eq!(e.to_string(), "Corpus error: document not found");
73
74        let e = Error::track_ref("invalid track ID");
75        assert_eq!(e.to_string(), "Track reference error: invalid track ID");
76    }
77
78    #[test]
79    fn test_error_constructors() {
80        let e = Error::invalid_input("test");
81        assert!(matches!(e, Error::InvalidInput(_)));
82
83        let e = Error::parse("test");
84        assert!(matches!(e, Error::Parse(_)));
85
86        let e = Error::corpus("test");
87        assert!(matches!(e, Error::Corpus(_)));
88
89        let e = Error::track_ref("test");
90        assert!(matches!(e, Error::TrackRef(_)));
91    }
92
93    #[test]
94    fn test_io_error_conversion() {
95        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file missing");
96        let err: Error = io_err.into();
97        assert!(matches!(err, Error::Io(_)));
98        assert!(err.to_string().contains("file missing"));
99    }
100}