1use thiserror::Error;
7
8#[derive(Debug, Error)]
10pub enum CascError {
11 #[error("I/O error: {0}")]
13 Io(#[from] std::io::Error),
14
15 #[error("Invalid magic: expected {expected}, found {found}")]
17 InvalidMagic {
18 expected: String,
20 found: String,
22 },
23
24 #[error("Invalid format: {0}")]
26 InvalidFormat(String),
27
28 #[error("{key_type} key not found: {hash}")]
30 KeyNotFound {
31 key_type: String,
33 hash: String,
35 },
36
37 #[error("Unsupported version: {0}")]
39 UnsupportedVersion(u32),
40
41 #[error("Encryption key missing: {0}")]
43 EncryptionKeyMissing(String),
44
45 #[error("Decompression failed: {0}")]
47 DecompressionFailed(String),
48
49 #[error("Checksum mismatch: expected {expected}, actual {actual}")]
51 ChecksumMismatch {
52 expected: String,
54 actual: String,
56 },
57
58 #[error("HTTP error: {0}")]
60 Http(#[from] reqwest::Error),
61}
62
63pub type Result<T> = std::result::Result<T, CascError>;
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn error_display_io() {
72 let err = CascError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "test"));
73 assert!(err.to_string().contains("I/O error"));
74 }
75
76 #[test]
77 fn error_display_invalid_magic() {
78 let err = CascError::InvalidMagic {
79 expected: "BLTE".into(),
80 found: "XXXX".into(),
81 };
82 assert!(err.to_string().contains("BLTE"));
83 assert!(err.to_string().contains("XXXX"));
84 }
85
86 #[test]
87 fn error_from_io() {
88 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
89 let casc_err: CascError = io_err.into();
90 assert!(matches!(casc_err, CascError::Io(_)));
91 }
92}