changeset 761:f506ad5c9b17

formats: Replace unwrap() with proper io::Error.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 06 Jan 2021 01:47:06 +0100
parents eba9a3d0c484
children cad9e422b18f
files formats/src/th06/pbg3.rs
diffstat 1 files changed, 4 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/formats/src/th06/pbg3.rs	Tue Jan 05 04:28:45 2021 +0100
+++ b/formats/src/th06/pbg3.rs	Wed Jan 06 01:47:06 2021 +0100
@@ -120,8 +120,7 @@
             let offset = bitstream.read_u32()?;
             let size = bitstream.read_u32()?;
             let name = bitstream.read_string(255)?;
-            // XXX: no unwrap!
-            let name = String::from_utf8(name).unwrap();
+            let name = String::from_utf8(name).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
             entries.insert(name, (unknown_1, unknown_2, checksum, offset, size));
         }
 
@@ -135,8 +134,9 @@
 
     /// Read a single file from this PBG3 archive.
     pub fn get_file(&mut self, filename: &str, check: bool) -> io::Result<Vec<u8>> {
-        // XXX: no unwrap!
-        let (_unknown_1, _unknown_2, checksum, offset, size) = self.entries.get(filename).unwrap();
+        let (_unknown_1, _unknown_2, checksum, offset, size) =
+            self.entries.get(filename)
+                .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, format!("File not found in PBG3: {}", filename)))?;
         self.bitstream.seek(io::SeekFrom::Start(*offset as u64))?;
         let data = lzss::decompress(&mut self.bitstream.bitstream, *size as usize, 0x2000, 13, 4, 3)?;
         if check {