nom_mpq/
error.rs

1//! Error handling of MPQ Parsing
2
3pub use crate::parser;
4use nom::error::ErrorKind;
5use nom::error::ParseError;
6use thiserror::Error;
7
8/// Holds the result of parsing progress and the possibly failures
9pub type MPQResult<I, O> = Result<(I, O), MPQParserError>;
10
11/// Error handling for upstream crates to use
12#[derive(Error, Debug)]
13pub enum MPQParserError {
14    /// Mising Archive Header
15    #[error("Missing Archive Header")]
16    MissingArchiveHeader,
17    /// A section magic was Unexpected
18    #[error("Unexpected Section")]
19    UnexpectedSection,
20    /// Unable to parse the byte aligned data types
21    #[error("Nom ByteAligned Error {0}")]
22    ByteAligned(String),
23    /// An I/O Error
24    #[error("IO Error")]
25    IoError(#[from] std::io::Error),
26    /// The Hash Table Entry wasn't found for a filename
27    #[error("Hash Table Entry not found {0}")]
28    HashTableEntryNotFound(String),
29    /// Unable to decrypt mpq data with key
30    #[error("Unable to decrypt data with key: {0}")]
31    DecryptionDataWithKey(String),
32    /// Incoming data, missing bytes
33    #[error("Missing bytes")]
34    IncompleteData,
35    /// Invalid HashType number
36    #[error("Invalid HashType number: {0}")]
37    InvalidHashType(u32),
38    /// Unsupported Compression Type
39    #[error("Invalid Compression Type: {0}")]
40    UnsupportedCompression(u8),
41    /// Encryption Type not suported.
42    #[error("Encryption Type is not supported.")]
43    UnsupportedEncryptionType,
44    /// Invalid UTF-8 Sequence on MPQ listfile sector.
45    #[error("Invalid UTF-8 Sequence on section {0}")]
46    InvalidUTF8Sequence(String),
47    /// Invalid ListFile sector
48    #[error("Invalid ListFile sector")]
49    InvalidListFileSector,
50    /// Encryption table index not found
51    #[error("Encryption table index not found, check error messages")]
52    EncryptionTableIndexNotFound,
53}
54
55/// Conversion of errors from byte aligned parser
56impl<I> From<nom::Err<nom::error::Error<I>>> for MPQParserError
57where
58    I: Clone + std::fmt::Debug,
59{
60    fn from(err: nom::Err<nom::error::Error<I>>) -> Self {
61        match err {
62            nom::Err::Incomplete(_) => {
63                unreachable!("This library is compatible with only complete parsers, not streaming")
64            }
65            nom::Err::Error(e) => MPQParserError::ByteAligned(format!("{:?}", e)),
66            nom::Err::Failure(e) => MPQParserError::ByteAligned(format!("{:?}", e)),
67        }
68    }
69}
70
71impl<I> ParseError<I> for MPQParserError
72where
73    I: Clone,
74{
75    fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
76        MPQParserError::ByteAligned(format!("{:?}", kind))
77    }
78
79    fn append(_input: I, _kind: ErrorKind, other: Self) -> Self {
80        other
81    }
82}