Skip to main content

me3_coalesced_parser/
error.rs

1use std::{error::Error, fmt::Display};
2
3#[derive(Debug)]
4pub enum DecodeError {
5    /// Reached the end of the available bytes before
6    /// a value could be obtained
7    UnexpectedEof {
8        /// The current reader cursor position
9        cursor: usize,
10        /// The number of bytes attempted to read
11        wanted: usize,
12        /// The remaining bytes in the reader slice
13        remaining: usize,
14    },
15
16    UnknownFileMagic,
17    StringTableHashMismatch,
18    StringTableSizeMismatch,
19    InvalidNameOffset,
20    UnknownValueType,
21    MalformedDecompressionNodes,
22}
23
24/// Type alias for result which could result in a Coalesced Error
25pub type DecodeResult<T> = Result<T, DecodeError>;
26
27/// Error implementation
28impl Error for DecodeError {}
29
30/// Display formatting implementation
31impl Display for DecodeError {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            DecodeError::UnexpectedEof {
35                cursor,
36                wanted,
37                remaining,
38            } => {
39                write!(
40                    f,
41                    "Unexpected end of file (cursor: {}, wanted: {}, remaining: {})",
42                    cursor, wanted, remaining
43                )
44            }
45            DecodeError::UnknownFileMagic => f.write_str("Unexpected file magic bytes"),
46            DecodeError::StringTableHashMismatch => f.write_str("String table hash didn't match"),
47            DecodeError::StringTableSizeMismatch => f.write_str("String table size didn't match"),
48            DecodeError::InvalidNameOffset => f.write_str("Invalid name offset"),
49            DecodeError::UnknownValueType => f.write_str("Unknown value type"),
50            DecodeError::MalformedDecompressionNodes => {
51                f.write_str("Decompression nodes are malformed")
52            }
53        }
54    }
55}