Skip to main content

wow_blp/parser/
error.rs

1use thiserror::Error;
2
3/// Errors that appears when loading from filesystem
4#[derive(Debug, Error)]
5pub enum LoadError {
6    /// Generic parsing error with description
7    #[error("{0}")]
8    Parsing(String),
9    /// File system error when reading BLP or mipmap files
10    #[error("File system error with file {0}, due: {1}")]
11    FileSystem(std::path::PathBuf, std::io::Error),
12    /// Invalid or malformed BLP filename
13    #[error("Cannot derive mipmap name for {0}")]
14    InvalidFilename(std::path::PathBuf),
15}
16
17/// Errors that BLP parser can produce
18#[derive(Debug, Error)]
19pub enum Error {
20    /// Invalid magic bytes in BLP header
21    #[error("Unexpected magic value {0}. The file format is not BLP or not supported.")]
22    WrongMagic(String),
23    /// Failed to load external mipmap file
24    #[error("Failed to extract external mipmap number {0} with error {1}")]
25    ExternalMipmap(usize, Box<dyn std::error::Error>),
26    /// Missing image data for the specified mipmap level
27    #[error("There is no body of image for BLP0 mipmap number {0}")]
28    MissingImage(usize),
29    /// Image data extends beyond file boundaries
30    #[error("Part of image exceeds bounds of file at offset {offset} with size {size}")]
31    OutOfBounds {
32        /// Offset where the out of bounds access occurred
33        offset: usize,
34        /// Size of data that was attempted to be read
35        size: usize,
36    },
37    /// BLP2 format does not support external mipmap files
38    #[error("BLP2 doesn't support external mipmaps")]
39    Blp2NoExternalMips,
40    /// Unsupported compression type in BLP2 header
41    #[error("Library doesn't support compression tag: {0}")]
42    Blp2UnknownCompression(u8),
43    /// Unsupported alpha channel type in BLP2 header
44    #[error("Library doesn't support alpha type: {0}")]
45    Blp2UnknownAlphaType(u8),
46    /// Unknown alpha type value during parsing
47    #[error("Unknown alpha type value: {0}")]
48    UnknownAlphaType(u8),
49    /// Invalid combination of JPEG compression with direct content
50    #[error("Impossible branch, JPEG compression but direct content type")]
51    Blp2UnexpectedJpegCompression,
52    /// Unexpected end of file while parsing
53    #[error("Unexpected end of file")]
54    UnexpectedEof,
55    /// Parser error with context information
56    #[error("Context: {0}. Error: {1}")]
57    Context(String, Box<Self>),
58}
59
60impl Error {
61    /// Add context information to an error
62    pub fn with_context(self, context: &str) -> Self {
63        Error::Context(context.to_owned(), Box::new(self))
64    }
65}