unpackers: factorize parsing error generation
This commit is contained in:
parent
c072e9cfa9
commit
a6c07e1ae4
|
@ -7,7 +7,7 @@ use nom::Err::{Error as NomError, Failure as NomFailure};
|
|||
|
||||
use crate::name_hashes;
|
||||
use crate::parsers::bhd;
|
||||
use crate::unpackers::errors::{self as unpackers_errors, UnpackError};
|
||||
use crate::unpackers::errors::UnpackError;
|
||||
use crate::utils::fs as fs_utils;
|
||||
|
||||
/// Parse a BHD file and extract its content from sister BDT.
|
||||
|
@ -25,14 +25,9 @@ pub fn extract_bhd(
|
|||
let mut bhd_data = vec![0u8; file_len];
|
||||
bhd_file.read_exact(&mut bhd_data)?;
|
||||
let bhd = match bhd::parse(&bhd_data) {
|
||||
Ok((_, bhd)) => { bhd }
|
||||
Err(NomError(e)) | Err(NomFailure(e)) => {
|
||||
let reason = unpackers_errors::get_nom_error_reason(e.1);
|
||||
return Err(UnpackError::Parsing("BHD parsing failed: ".to_owned() + &reason))
|
||||
}
|
||||
e => {
|
||||
return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
|
||||
}
|
||||
Ok((_, bhd)) => bhd,
|
||||
Err(NomError(e)) | Err(NomFailure(e)) => return Err(UnpackError::parsing_err("BHD", e.1)),
|
||||
e => return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
|
||||
};
|
||||
|
||||
let bdt_path = path::PathBuf::from(bhd_path).with_extension("bdt");
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::path;
|
|||
use nom::Err::{Error as NomError, Failure as NomFailure};
|
||||
|
||||
use crate::parsers::bnd;
|
||||
use crate::unpackers::errors::{self as unpackers_errors, UnpackError};
|
||||
use crate::unpackers::errors::UnpackError;
|
||||
use crate::utils::fs as fs_utils;
|
||||
|
||||
/// Extract BND file contents to disk.
|
||||
|
@ -94,13 +94,8 @@ pub fn load_bnd_file(bnd_path: &str) -> Result<(bnd::Bnd, Vec<u8>), UnpackError>
|
|||
pub fn load_bnd(bnd_data: &[u8]) -> Result<bnd::Bnd, UnpackError> {
|
||||
let (_, bnd) = match bnd::parse(bnd_data) {
|
||||
Ok(result) => result,
|
||||
Err(NomError(e)) | Err(NomFailure(e)) => {
|
||||
let reason = unpackers_errors::get_nom_error_reason(e.1);
|
||||
return Err(UnpackError::Parsing("BND parsing failed: ".to_owned() + &reason))
|
||||
}
|
||||
e => {
|
||||
return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
|
||||
}
|
||||
Err(NomError(e)) | Err(NomFailure(e)) => return Err(UnpackError::parsing_err("BND", e.1)),
|
||||
e => return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e))),
|
||||
};
|
||||
Ok(bnd)
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use flate2::read::ZlibDecoder;
|
|||
use nom::Err::{Error as NomError, Failure as NomFailure};
|
||||
|
||||
use crate::parsers::dcx;
|
||||
use crate::unpackers::errors::{self as unpackers_errors, UnpackError};
|
||||
use crate::unpackers::errors::UnpackError;
|
||||
|
||||
/// Extract DCX file content to disk.
|
||||
pub fn extract_dcx(dcx_path: &str, output_path: &str) -> Result<(), UnpackError> {
|
||||
|
@ -22,14 +22,9 @@ pub fn load_dcx(dcx_path: &str) -> Result<(dcx::Dcx, Vec<u8>), UnpackError> {
|
|||
let mut dcx_data = vec![0u8; file_len];
|
||||
dcx_file.read_exact(&mut dcx_data)?;
|
||||
let (data, dcx) = match dcx::parse(&dcx_data) {
|
||||
Ok(result) => { result }
|
||||
Err(NomError(e)) | Err(NomFailure(e)) => {
|
||||
let reason = unpackers_errors::get_nom_error_reason(e.1);
|
||||
return Err(UnpackError::Parsing("DCX parsing failed: ".to_owned() + &reason))
|
||||
}
|
||||
e => {
|
||||
return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
|
||||
}
|
||||
Ok(result) => result,
|
||||
Err(NomError(e)) | Err(NomFailure(e)) => return Err(UnpackError::parsing_err("DCX", e.1)),
|
||||
e => return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e))),
|
||||
};
|
||||
|
||||
let decomp_data = decompress_dcx(&dcx, data)?;
|
||||
|
|
|
@ -9,12 +9,16 @@ pub enum UnpackError {
|
|||
Unknown(String),
|
||||
}
|
||||
|
||||
impl UnpackError {
|
||||
pub fn parsing_err(filetype: &str, kind: nom::error::ErrorKind) -> UnpackError {
|
||||
let reason = format!("{:?} {:?}", kind, kind.description());
|
||||
let message = format!("{} parsing failed: ", filetype);
|
||||
UnpackError::Parsing(message + &reason)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for UnpackError {
|
||||
fn from(e: io::Error) -> Self {
|
||||
UnpackError::Io(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_nom_error_reason(kind: nom::error::ErrorKind) -> String {
|
||||
format!("{:?} {:?}", kind, kind.description())
|
||||
}
|
||||
|
|
Reference in a new issue