From e094742690161a411a1269d4aa0072dae6123756 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Tue, 9 Aug 2022 22:43:04 -0400 Subject: [PATCH] Properly propagate errors up the stack for game_data::extract --- src/gamedata.rs | 14 +++++--------- src/index.rs | 3 +-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/gamedata.rs b/src/gamedata.rs index a08227d..1c09274 100755 --- a/src/gamedata.rs +++ b/src/gamedata.rs @@ -152,8 +152,7 @@ impl GameData { pub fn extract(&self, path: &str) -> Option { let hash = calculate_hash(path); - let index_file = self.get_index_file(path) - .expect("Failed to find index file."); + let index_file = self.get_index_file(path)?; let slice = index_file.entries.iter().find(|s| s.hash == hash); match slice { @@ -162,10 +161,7 @@ impl GameData { dat_file.read_from_offset(entry.bitfield.offset()) } - None => { - println!("Entry not found."); - None - } + None => None } } @@ -188,9 +184,9 @@ impl GameData { } pub fn read_excel_sheet_header(&self, name : &str) -> Option { - let root_exl_file = self.extract("exd/root.exl").unwrap(); + let root_exl_file = self.extract("exd/root.exl")?; - let root_exl = EXL::from_existing(&root_exl_file).unwrap(); + let root_exl = EXL::from_existing(&root_exl_file)?; for (row, _) in root_exl.entries { if row == name { @@ -198,7 +194,7 @@ impl GameData { let path = format!("exd/{new_filename}.exh"); - return EXH::from_existing(&self.extract(&path).unwrap()) + return EXH::from_existing(&self.extract(&path)?) } } diff --git a/src/index.rs b/src/index.rs index 098d49a..1cbfc35 100755 --- a/src/index.rs +++ b/src/index.rs @@ -74,8 +74,7 @@ pub struct IndexFile { impl IndexFile { /// Creates a new reference to an existing index file, this reads both an index and index2 file. pub fn from_existing(path: &str) -> Option { - let mut index_file = std::fs::File::open(path) - .expect("Failed to read index file."); + let mut index_file = std::fs::File::open(path).ok()?; IndexFile::read(&mut index_file).ok() }