diff --git a/src/unpackers/bhd.rs b/src/unpackers/bhd.rs index 58e1ab5..e0f7666 100644 --- a/src/unpackers/bhd.rs +++ b/src/unpackers/bhd.rs @@ -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"); diff --git a/src/unpackers/bnd.rs b/src/unpackers/bnd.rs index 8c2b4c4..63b160a 100644 --- a/src/unpackers/bnd.rs +++ b/src/unpackers/bnd.rs @@ -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), UnpackError> pub fn load_bnd(bnd_data: &[u8]) -> Result { 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) } diff --git a/src/unpackers/dcx.rs b/src/unpackers/dcx.rs index 39af7a8..7957c88 100644 --- a/src/unpackers/dcx.rs +++ b/src/unpackers/dcx.rs @@ -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), 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)?; diff --git a/src/unpackers/errors.rs b/src/unpackers/errors.rs index 17aaa1e..57b0c2f 100644 --- a/src/unpackers/errors.rs +++ b/src/unpackers/errors.rs @@ -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 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()) -}