Skip to main content

wow_wdt/
error.rs

1//! Error types for the WDT library
2
3use std::io;
4use thiserror::Error;
5
6/// Result type alias for WDT operations
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors that can occur during WDT operations
10#[derive(Error, Debug)]
11pub enum Error {
12    /// I/O error occurred
13    #[error("I/O error: {0}")]
14    Io(#[from] io::Error),
15
16    /// Invalid magic bytes for a chunk
17    #[error("Invalid chunk magic: expected {expected:?}, found {found:?}")]
18    InvalidMagic { expected: [u8; 4], found: [u8; 4] },
19
20    /// Unexpected chunk size
21    #[error("Invalid chunk size for {chunk}: expected {expected}, found {found}")]
22    InvalidChunkSize {
23        chunk: String,
24        expected: usize,
25        found: usize,
26    },
27
28    /// Invalid WDT version
29    #[error("Invalid WDT version: expected 18, found {0}")]
30    InvalidVersion(u32),
31
32    /// Missing required chunk
33    #[error("Missing required chunk: {0}")]
34    MissingChunk(String),
35
36    /// Invalid data within a chunk
37    #[error("Invalid {chunk} data: {message}")]
38    InvalidChunkData { chunk: String, message: String },
39
40    /// Version conversion error
41    #[error("Cannot convert from {from} to {to}: {reason}")]
42    ConversionError {
43        from: String,
44        to: String,
45        reason: String,
46    },
47
48    /// Validation error
49    #[error("Validation failed: {0}")]
50    ValidationError(String),
51
52    /// Unsupported feature for version
53    #[error("Feature {feature} is not supported in version {version}")]
54    UnsupportedFeature { feature: String, version: String },
55
56    /// String encoding error
57    #[error("Invalid string encoding in {context}: {message}")]
58    StringError { context: String, message: String },
59
60    /// File size exceeded limit
61    #[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    /// Create an invalid magic error with chunk name
71    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    /// Create an invalid chunk data error
79    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    /// Create a validation error
87    pub fn validation(message: impl Into<String>) -> Self {
88        Error::ValidationError(message.into())
89    }
90}