use thiserror::Error;
#[derive(Debug, Error)]
pub enum CascError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid magic: expected {expected}, found {found}")]
InvalidMagic {
expected: String,
found: String,
},
#[error("Invalid format: {0}")]
InvalidFormat(String),
#[error("{key_type} key not found: {hash}")]
KeyNotFound {
key_type: String,
hash: String,
},
#[error("Unsupported version: {0}")]
UnsupportedVersion(u32),
#[error("Encryption key missing: {0}")]
EncryptionKeyMissing(String),
#[error("Decompression failed: {0}")]
DecompressionFailed(String),
#[error("Checksum mismatch: expected {expected}, actual {actual}")]
ChecksumMismatch {
expected: String,
actual: String,
},
#[error("HTTP error: {0}")]
Http(#[from] reqwest::Error),
}
pub type Result<T> = std::result::Result<T, CascError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display_io() {
let err = CascError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, "test"));
assert!(err.to_string().contains("I/O error"));
}
#[test]
fn error_display_invalid_magic() {
let err = CascError::InvalidMagic {
expected: "BLTE".into(),
found: "XXXX".into(),
};
assert!(err.to_string().contains("BLTE"));
assert!(err.to_string().contains("XXXX"));
}
#[test]
fn error_from_io() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "missing");
let casc_err: CascError = io_err.into();
assert!(matches!(casc_err, CascError::Io(_)));
}
}