pub use crate::parser;
use nom::error::ErrorKind;
use nom::error::ParseError;
use thiserror::Error;
pub type MPQResult<I, O> = Result<(I, O), MPQParserError>;
#[derive(Error, Debug)]
pub enum MPQParserError {
#[error("Missing Archive Header")]
MissingArchiveHeader,
#[error("Unexpected Section")]
UnexpectedSection,
#[error("Nom ByteAligned Error {0}")]
ByteAligned(String),
#[error("IO Error")]
IoError(#[from] std::io::Error),
#[error("Hash Table Entry not found {0}")]
HashTableEntryNotFound(String),
#[error("Unable to decrypt data with key: {0}")]
DecryptionDataWithKey(String),
#[error("Missing bytes")]
IncompleteData,
#[error("Invalid HashType number: {0}")]
InvalidHashType(u32),
}
impl<I> From<nom::Err<nom::error::Error<I>>> for MPQParserError
where
I: Clone + std::fmt::Debug,
{
fn from(err: nom::Err<nom::error::Error<I>>) -> Self {
match err {
nom::Err::Incomplete(_) => {
unreachable!("This library is compatible with only complete parsers, not streaming")
}
nom::Err::Error(e) => MPQParserError::ByteAligned(format!("{:?}", e)),
nom::Err::Failure(e) => MPQParserError::ByteAligned(format!("{:?}", e)),
}
}
}
impl<I> ParseError<I> for MPQParserError
where
I: Clone,
{
fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
MPQParserError::ByteAligned(format!("{:?}", kind))
}
fn append(_input: I, _kind: ErrorKind, other: Self) -> Self {
other
}
}