1use std::io;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Error, Debug)]
11pub enum Error {
12 #[error("I/O error: {0}")]
14 Io(#[from] io::Error),
15
16 #[error("Invalid chunk magic: expected {expected:?}, found {found:?}")]
18 InvalidMagic { expected: [u8; 4], found: [u8; 4] },
19
20 #[error("Invalid chunk size for {chunk}: expected {expected}, found {found}")]
22 InvalidChunkSize {
23 chunk: String,
24 expected: usize,
25 found: usize,
26 },
27
28 #[error("Invalid WDT version: expected 18, found {0}")]
30 InvalidVersion(u32),
31
32 #[error("Missing required chunk: {0}")]
34 MissingChunk(String),
35
36 #[error("Invalid {chunk} data: {message}")]
38 InvalidChunkData { chunk: String, message: String },
39
40 #[error("Cannot convert from {from} to {to}: {reason}")]
42 ConversionError {
43 from: String,
44 to: String,
45 reason: String,
46 },
47
48 #[error("Validation failed: {0}")]
50 ValidationError(String),
51
52 #[error("Feature {feature} is not supported in version {version}")]
54 UnsupportedFeature { feature: String, version: String },
55
56 #[error("Invalid string encoding in {context}: {message}")]
58 StringError { context: String, message: String },
59
60 #[error("File size {size} exceeds limit {limit} for {context}")]
62 SizeLimit {
63 size: usize,
64 limit: usize,
65 context: String,
66 },
67}
68
69impl Error {
70 pub fn invalid_magic_str(_chunk: &str, expected: &[u8; 4], found: &[u8; 4]) -> Self {
72 Error::InvalidMagic {
73 expected: *expected,
74 found: *found,
75 }
76 }
77
78 pub fn invalid_data(chunk: impl Into<String>, message: impl Into<String>) -> Self {
80 Error::InvalidChunkData {
81 chunk: chunk.into(),
82 message: message.into(),
83 }
84 }
85
86 pub fn validation(message: impl Into<String>) -> Self {
88 Error::ValidationError(message.into())
89 }
90}