From 06bd16dfc219fd3108c8588857e84dccf3506965 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Sat, 12 Aug 2023 12:12:32 +0100
Subject: [PATCH] chore: better handling of errors

---
 src/gb.rs | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/src/gb.rs b/src/gb.rs
index 8ee0bc18..4f2d8e07 100644
--- a/src/gb.rs
+++ b/src/gb.rs
@@ -989,32 +989,37 @@ impl GameBoy {
         self.mmu_i().rom_i()
     }
 
-    pub fn load_boot_path(&mut self, path: &str) {
-        let data = read_file(path).unwrap();
+    pub fn load_boot_path(&mut self, path: &str) -> Result<(), String> {
+        let data = read_file(path)?;
         self.load_boot(&data);
+        Ok(())
     }
 
-    pub fn load_boot_file(&mut self, boot_rom: BootRom) {
+    pub fn load_boot_file(&mut self, boot_rom: BootRom) -> Result<(), String> {
         match boot_rom {
-            BootRom::Dmg => self.load_boot_path("./res/boot/dmg_boot.bin"),
-            BootRom::Sgb => self.load_boot_path("./res/boot/sgb_boot.bin"),
-            BootRom::DmgBootix => self.load_boot_path("./res/boot/dmg_bootix.bin"),
-            BootRom::MgbBootix => self.load_boot_path("./res/boot/mgb_bootix.bin"),
-            BootRom::Cgb => self.load_boot_path("./res/boot/cgb_boot.bin"),
+            BootRom::Dmg => self.load_boot_path("./res/boot/dmg_boot.bin")?,
+            BootRom::Sgb => self.load_boot_path("./res/boot/sgb_boot.bin")?,
+            BootRom::DmgBootix => self.load_boot_path("./res/boot/dmg_bootix.bin")?,
+            BootRom::MgbBootix => self.load_boot_path("./res/boot/mgb_bootix.bin")?,
+            BootRom::Cgb => self.load_boot_path("./res/boot/cgb_boot.bin")?,
             BootRom::None => (),
         }
+        Ok(())
     }
 
-    pub fn load_boot_default_f(&mut self) {
-        self.load_boot_dmg_f();
+    pub fn load_boot_default_f(&mut self) -> Result<(), String> {
+        self.load_boot_dmg_f()?;
+        Ok(())
     }
 
-    pub fn load_boot_dmg_f(&mut self) {
-        self.load_boot_file(BootRom::DmgBootix);
+    pub fn load_boot_dmg_f(&mut self) -> Result<(), String> {
+        self.load_boot_file(BootRom::DmgBootix)?;
+        Ok(())
     }
 
-    pub fn load_boot_cgb_f(&mut self) {
-        self.load_boot_file(BootRom::Cgb);
+    pub fn load_boot_cgb_f(&mut self) -> Result<(), String> {
+        self.load_boot_file(BootRom::Cgb)?;
+        Ok(())
     }
 
     pub fn load_cartridge(&mut self, rom: Cartridge) -> &mut Cartridge {
-- 
GitLab