unpackers: factorize parsing error generation

This commit is contained in:
dece 2020-05-09 19:30:57 +02:00
parent c072e9cfa9
commit a6c07e1ae4
4 changed files with 19 additions and 30 deletions

View file

@ -7,7 +7,7 @@ use nom::Err::{Error as NomError, Failure as NomFailure};
use crate::name_hashes; use crate::name_hashes;
use crate::parsers::bhd; 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; use crate::utils::fs as fs_utils;
/// Parse a BHD file and extract its content from sister BDT. /// 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]; let mut bhd_data = vec![0u8; file_len];
bhd_file.read_exact(&mut bhd_data)?; bhd_file.read_exact(&mut bhd_data)?;
let bhd = match bhd::parse(&bhd_data) { let bhd = match bhd::parse(&bhd_data) {
Ok((_, bhd)) => { bhd } Ok((_, bhd)) => bhd,
Err(NomError(e)) | Err(NomFailure(e)) => { Err(NomError(e)) | Err(NomFailure(e)) => return Err(UnpackError::parsing_err("BHD", e.1)),
let reason = unpackers_errors::get_nom_error_reason(e.1); e => return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
return Err(UnpackError::Parsing("BHD parsing failed: ".to_owned() + &reason))
}
e => {
return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
}
}; };
let bdt_path = path::PathBuf::from(bhd_path).with_extension("bdt"); let bdt_path = path::PathBuf::from(bhd_path).with_extension("bdt");

View file

@ -5,7 +5,7 @@ use std::path;
use nom::Err::{Error as NomError, Failure as NomFailure}; use nom::Err::{Error as NomError, Failure as NomFailure};
use crate::parsers::bnd; 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; use crate::utils::fs as fs_utils;
/// Extract BND file contents to disk. /// 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> { pub fn load_bnd(bnd_data: &[u8]) -> Result<bnd::Bnd, UnpackError> {
let (_, bnd) = match bnd::parse(bnd_data) { let (_, bnd) = match bnd::parse(bnd_data) {
Ok(result) => result, Ok(result) => result,
Err(NomError(e)) | Err(NomFailure(e)) => { Err(NomError(e)) | Err(NomFailure(e)) => return Err(UnpackError::parsing_err("BND", e.1)),
let reason = unpackers_errors::get_nom_error_reason(e.1); e => return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e))),
return Err(UnpackError::Parsing("BND parsing failed: ".to_owned() + &reason))
}
e => {
return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
}
}; };
Ok(bnd) Ok(bnd)
} }

View file

@ -5,7 +5,7 @@ use flate2::read::ZlibDecoder;
use nom::Err::{Error as NomError, Failure as NomFailure}; use nom::Err::{Error as NomError, Failure as NomFailure};
use crate::parsers::dcx; use crate::parsers::dcx;
use crate::unpackers::errors::{self as unpackers_errors, UnpackError}; use crate::unpackers::errors::UnpackError;
/// Extract DCX file content to disk. /// Extract DCX file content to disk.
pub fn extract_dcx(dcx_path: &str, output_path: &str) -> Result<(), UnpackError> { 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]; let mut dcx_data = vec![0u8; file_len];
dcx_file.read_exact(&mut dcx_data)?; dcx_file.read_exact(&mut dcx_data)?;
let (data, dcx) = match dcx::parse(&dcx_data) { let (data, dcx) = match dcx::parse(&dcx_data) {
Ok(result) => { result } Ok(result) => result,
Err(NomError(e)) | Err(NomFailure(e)) => { Err(NomError(e)) | Err(NomFailure(e)) => return Err(UnpackError::parsing_err("DCX", e.1)),
let reason = unpackers_errors::get_nom_error_reason(e.1); e => return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e))),
return Err(UnpackError::Parsing("DCX parsing failed: ".to_owned() + &reason))
}
e => {
return Err(UnpackError::Unknown(format!("Unknown error: {:?}", e)))
}
}; };
let decomp_data = decompress_dcx(&dcx, data)?; let decomp_data = decompress_dcx(&dcx, data)?;

View file

@ -9,12 +9,16 @@ pub enum UnpackError {
Unknown(String), 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 { impl From<io::Error> for UnpackError {
fn from(e: io::Error) -> Self { fn from(e: io::Error) -> Self {
UnpackError::Io(e) UnpackError::Io(e)
} }
} }
pub fn get_nom_error_reason(kind: nom::error::ErrorKind) -> String {
format!("{:?} {:?}", kind, kind.description())
}