1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug)]
10#[non_exhaustive]
11pub enum Error {
12 #[error("Invalid input: {0}")]
14 InvalidInput(String),
15
16 #[error("IO error: {0}")]
18 Io(#[from] std::io::Error),
19
20 #[error("Parse error: {0}")]
22 Parse(String),
23
24 #[error("Corpus error: {0}")]
26 Corpus(String),
27
28 #[error("Track reference error: {0}")]
30 TrackRef(String),
31}
32
33impl Error {
34 #[must_use]
36 pub fn track_ref(msg: impl Into<String>) -> Self {
37 Self::TrackRef(msg.into())
38 }
39
40 #[must_use]
42 pub fn corpus(msg: impl Into<String>) -> Self {
43 Self::Corpus(msg.into())
44 }
45
46 #[must_use]
48 pub fn invalid_input(msg: impl Into<String>) -> Self {
49 Self::InvalidInput(msg.into())
50 }
51
52 #[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}