1pub use crate::parser;
4use nom::error::ErrorKind;
5use nom::error::ParseError;
6use thiserror::Error;
7
8pub type MPQResult<I, O> = Result<(I, O), MPQParserError>;
10
11#[derive(Error, Debug)]
13pub enum MPQParserError {
14 #[error("Missing Archive Header")]
16 MissingArchiveHeader,
17 #[error("Unexpected Section")]
19 UnexpectedSection,
20 #[error("Nom ByteAligned Error {0}")]
22 ByteAligned(String),
23 #[error("IO Error")]
25 IoError(#[from] std::io::Error),
26 #[error("Hash Table Entry not found {0}")]
28 HashTableEntryNotFound(String),
29 #[error("Unable to decrypt data with key: {0}")]
31 DecryptionDataWithKey(String),
32 #[error("Missing bytes")]
34 IncompleteData,
35 #[error("Invalid HashType number: {0}")]
37 InvalidHashType(u32),
38 #[error("Invalid Compression Type: {0}")]
40 UnsupportedCompression(u8),
41 #[error("Encryption Type is not supported.")]
43 UnsupportedEncryptionType,
44 #[error("Invalid UTF-8 Sequence on section {0}")]
46 InvalidUTF8Sequence(String),
47 #[error("Invalid ListFile sector")]
49 InvalidListFileSector,
50 #[error("Encryption table index not found, check error messages")]
52 EncryptionTableIndexNotFound,
53}
54
55impl<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}