From d007201a6d671c3615c33c803d7304d9ecfb9987 Mon Sep 17 00:00:00 2001 From: Tangtang Zhou Date: Sat, 18 May 2024 20:20:35 +0200 Subject: [PATCH] chore: refactor unpack stdout --- src/iro_header.rs | 21 ++++++++++++++++++++- src/main.rs | 28 ++++++++++++++++------------ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/iro_header.rs b/src/iro_header.rs index c7a90cd..42345e5 100644 --- a/src/iro_header.rs +++ b/src/iro_header.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use crate::Error; pub const IRO_SIG: i32 = 0x534f5249; // represents IROS text @@ -55,8 +57,25 @@ impl TryFrom for IroFlags { match value { 0 => Ok(IroFlags::None), 1 => Ok(IroFlags::Patch), - _ => Err(Error::InvalidIroFlags(value)) + _ => Err(Error::InvalidIroFlags(value)), + } + } +} + +impl Display for IroFlags { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + IroFlags::None => f.write_str("Full IRO"), + IroFlags::Patch => f.write_str("Patch IRO"), } } } +impl Display for IroVersion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + IroVersion::Zero => f.write_str("0x10000"), + IroVersion::Two => f.write_str("0x10002"), + } + } +} diff --git a/src/main.rs b/src/main.rs index d9d4f37..130d43c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -220,13 +220,16 @@ fn unpack_archive(iro_path: PathBuf, output_path: Option) -> Result version: {}", iro_header.version); + println!("> type: {}", iro_header.flags); + println!("> number of files: {}", iro_header.num_files); + println!(); let mut iro_entries: Vec = Vec::new(); for _ in 0..iro_header.num_files { let bytes = buf_reader.fill_buf()?; let (rem_bytes, iro_entry) = parse_iro_entry_v2(bytes)?; - println!("{:?}", iro_entry); iro_entries.push(iro_entry); let consumed_bytes_len = bytes.len() - rem_bytes.len(); @@ -234,34 +237,35 @@ fn unpack_archive(iro_path: PathBuf, output_path: Option) -> Result Result { - let bytes_u16 = path_bytes +fn parse_utf16(bytes: &[u8]) -> Result { + let bytes_u16 = bytes .chunks(2) .map(|e| e.try_into().map(u16::from_le_bytes)) .collect::, _>>() .map_err(|_| Error::Utf16Error("uneven bytes".to_owned()))?; - String::from_utf16(&bytes_u16).map_err(|_| { - Error::Utf16Error("path_bytes in u16 cannot be converted to string".to_owned()) - }) + String::from_utf16(&bytes_u16) + .map_err(|_| Error::Utf16Error("bytes in u16 cannot be converted to string".to_owned())) } fn unicode_filepath_bytes(path: &Path, strip_prefix_str: &Path) -> Result, Error> {