image_blp/parser/
error.rs1use nom::error::{ContextError, ErrorKind, ParseError};
2use std::fmt;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
7pub enum LoadError {
8 #[error("{0}")]
9 Parsing(String),
10 #[error("File system error with file {0}, due: {1}")]
11 FileSystem(std::path::PathBuf, std::io::Error),
12 #[error("Input stream is incomplete, needed: {0:?}")]
13 Incomplete(nom::Needed),
14 #[error("Cannot derive mipmap name for {0}")]
15 InvalidFilename(std::path::PathBuf),
16}
17
18#[derive(Debug, Error)]
20pub enum Error<I: fmt::Debug> {
21 #[error("Unexpected magic value {0}. The file format is not BLP or not supported.")]
22 WrongMagic(String),
23 #[error("Failed to extract external mipmap number {0} with error {1}")]
24 ExternalMipmap(usize, Box<dyn std::error::Error>),
25 #[error("There is no body of image for BLP0 mipmap number {0}")]
26 MissingImage(usize),
27 #[error("Part of image exceeds bounds of file for mipmap number {0}")]
28 OutOfBounds(usize),
29 #[error("BLP2 doesn't support external mipmaps")]
30 Blp2NoExternalMips,
31 #[error("Library doesn't support compression tag: {0}")]
32 Blp2UnknownCompression(u8),
33 #[error("Library doesn't support alpha type: {0}")]
34 Blp2UnknownAlphaType(u8),
35 #[error("Impossible branch, JPEG compression but direct content type")]
36 Blp2UnexpectedJpegCompression,
37 #[error("Error {1:?} at: {0:?}")]
38 Nom(I, ErrorKind),
39 #[error("Context: {0}. Error: {1}")]
40 Context(String, Box<Self>),
41}
42
43impl<'a> From<(&'a [u8], ErrorKind)> for Error<&'a [u8]> {
44 fn from((input, kind): (&'a [u8], ErrorKind)) -> Self {
45 Error::Nom(input, kind)
46 }
47}
48
49impl<'a> ParseError<&'a [u8]> for Error<&'a [u8]> {
50 fn from_error_kind(input: &'a [u8], kind: ErrorKind) -> Self {
51 Error::Nom(input, kind)
52 }
53
54 fn append(_: &[u8], _: ErrorKind, other: Self) -> Self {
55 other
56 }
57}
58
59impl<'a> ContextError<&'a [u8]> for Error<&'a [u8]> {
60 fn add_context(_input: &'a [u8], ctx: &'static str, other: Self) -> Self {
61 Error::Context(ctx.to_owned(), Box::new(other))
62 }
63}