use std::io;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Invalid chunk magic: expected {expected:?}, found {found:?}")]
InvalidMagic { expected: [u8; 4], found: [u8; 4] },
#[error("Invalid chunk size for {chunk}: expected {expected}, found {found}")]
InvalidChunkSize {
chunk: String,
expected: usize,
found: usize,
},
#[error("Invalid WDT version: expected 18, found {0}")]
InvalidVersion(u32),
#[error("Missing required chunk: {0}")]
MissingChunk(String),
#[error("Invalid {chunk} data: {message}")]
InvalidChunkData { chunk: String, message: String },
#[error("Cannot convert from {from} to {to}: {reason}")]
ConversionError {
from: String,
to: String,
reason: String,
},
#[error("Validation failed: {0}")]
ValidationError(String),
#[error("Feature {feature} is not supported in version {version}")]
UnsupportedFeature { feature: String, version: String },
#[error("Invalid string encoding in {context}: {message}")]
StringError { context: String, message: String },
#[error("File size {size} exceeds limit {limit} for {context}")]
SizeLimit {
size: usize,
limit: usize,
context: String,
},
}
impl Error {
pub fn invalid_magic_str(_chunk: &str, expected: &[u8; 4], found: &[u8; 4]) -> Self {
Error::InvalidMagic {
expected: *expected,
found: *found,
}
}
pub fn invalid_data(chunk: impl Into<String>, message: impl Into<String>) -> Self {
Error::InvalidChunkData {
chunk: chunk.into(),
message: message.into(),
}
}
pub fn validation(message: impl Into<String>) -> Self {
Error::ValidationError(message.into())
}
}