ktx2_reader/
error.rs

1use std::error::Error;
2use std::{fmt, io};
3
4/// Error while reading data.
5#[derive(Debug)]
6pub enum ReadError {
7    IoError(io::Error),
8    ParseError(ParseError),
9}
10
11impl Error for ReadError {}
12
13impl fmt::Display for ReadError {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        match &self {
16            ReadError::IoError(e) => write!(f, "Input error: {}", e),
17            ReadError::ParseError(e) => write!(f, "Parse error: {}", e),
18        }
19    }
20}
21
22impl From<io::Error> for ReadError {
23    fn from(e: io::Error) -> Self {
24        Self::IoError(e)
25    }
26}
27
28impl From<ParseError> for ReadError {
29    fn from(e: ParseError) -> Self {
30        Self::ParseError(e)
31    }
32}
33
34/// Error, that happend when data doesn't satisfy expected parameters.
35#[derive(Debug)]
36pub enum ParseError {
37    /// Unexpected texture identifier.
38    BadIdentifier([u8; 12]),
39    /// Specified format is not supported.
40    BadFormat(u32),
41    /// Type size of texture is zero.
42    ZeroTypeSize,
43    /// Width of texture is zero.
44    ZeroWidth,
45    /// Face count of texture is zero.
46    ZeroFaceCount,
47    /// Found unsupported feature.
48    UnsupportedFeature(&'static str),
49}
50
51impl Error for ParseError {}
52
53impl fmt::Display for ParseError {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        match &self {
56            ParseError::BadIdentifier(id) => write!(f, "Identifier is wrong: {:?}", id),
57            ParseError::BadFormat(i) => write!(f, "Unsoperted format: {:?}", i),
58            ParseError::ZeroTypeSize => write!(f, "Type size is zero"),
59            ParseError::ZeroWidth => write!(f, "Width is zero"),
60            ParseError::ZeroFaceCount => write!(f, "Face count is zero"),
61            ParseError::UnsupportedFeature(name) => write!(f, "Loader doesn't support: {}", name),
62        }
63    }
64}
65
66/// Error, that happened when reader can't read data to client's buffer.
67#[derive(Debug)]
68pub enum ReadToError {
69    ReadError(ReadError),
70    BadBuffer(u64),
71}
72
73impl Error for ReadToError {}
74
75impl fmt::Display for ReadToError {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        match &self {
78            Self::ReadError(e) => e.fmt(f),
79            Self::BadBuffer(expect_size) => {
80                write!(f, "Wrong buffer size. Expected: {:?}", expect_size)
81            }
82        }
83    }
84}
85
86impl From<ReadError> for ReadToError {
87    fn from(e: ReadError) -> Self {
88        Self::ReadError(e)
89    }
90}
91
92impl From<io::Error> for ReadToError {
93    fn from(e: io::Error) -> Self {
94        ReadError::IoError(e).into()
95    }
96}