Skip to content
Snippets Groups Projects
Verified Commit 9cbd4b77 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: new error support

parent 4bcd59a3
No related branches found
No related tags found
1 merge request!31System state save
Pipeline #3294 passed
...@@ -395,7 +395,7 @@ impl Emulator { ...@@ -395,7 +395,7 @@ impl Emulator {
// into a *.sav file in the file system // into a *.sav file in the file system
if counter % store_count == 0 && self.system.rom().has_battery() { if counter % store_count == 0 && self.system.rom().has_battery() {
let ram_data = self.system.rom().ram_data(); let ram_data = self.system.rom().ram_data();
write_file(&self.ram_path, ram_data); write_file(&self.ram_path, ram_data).unwrap();
} }
// obtains an event from the SDL sub-system to be // obtains an event from the SDL sub-system to be
......
...@@ -990,7 +990,7 @@ impl GameBoy { ...@@ -990,7 +990,7 @@ impl GameBoy {
} }
pub fn load_boot_path(&mut self, path: &str) { pub fn load_boot_path(&mut self, path: &str) {
let data = read_file(path); let data = read_file(path).unwrap();
self.load_boot(&data); self.load_boot(&data);
} }
...@@ -1031,10 +1031,10 @@ impl GameBoy { ...@@ -1031,10 +1031,10 @@ impl GameBoy {
} }
pub fn load_rom_file(&mut self, path: &str, ram_path: Option<&str>) -> &mut Cartridge { pub fn load_rom_file(&mut self, path: &str, ram_path: Option<&str>) -> &mut Cartridge {
let data = read_file(path); let data = read_file(path).unwrap();
match ram_path { match ram_path {
Some(ram_path) => { Some(ram_path) => {
let ram_data = read_file(ram_path); let ram_data = read_file(ram_path).unwrap();
self.load_rom(&data, Some(&ram_data)) self.load_rom(&data, Some(&ram_data))
} }
None => self.load_rom(&data, None), None => self.load_rom(&data, None),
......
...@@ -335,7 +335,7 @@ impl Cartridge { ...@@ -335,7 +335,7 @@ impl Cartridge {
} }
pub fn from_file(path: &str) -> Self { pub fn from_file(path: &str) -> Self {
let data = read_file(path); let data = read_file(path).unwrap();
Self::from_data(&data) Self::from_data(&data)
} }
......
...@@ -10,23 +10,24 @@ pub type SharedMut<T> = Rc<RefCell<T>>; ...@@ -10,23 +10,24 @@ pub type SharedMut<T> = Rc<RefCell<T>>;
/// Reads the contents of the file at the given path into /// Reads the contents of the file at the given path into
/// a vector of bytes. /// a vector of bytes.
pub fn read_file(path: &str) -> Vec<u8> { pub fn read_file(path: &str) -> Result<Vec<u8>, String> {
let mut file = match File::open(path) { let mut file = match File::open(path) {
Ok(file) => file, Ok(file) => file,
Err(_) => panic!("Failed to open file: {}", path), Err(_) => return Err(format!("Failed to open file: {}", path)),
}; };
let mut data = Vec::new(); let mut data = Vec::new();
file.read_to_end(&mut data).unwrap(); file.read_to_end(&mut data).unwrap();
data Ok(data)
} }
/// Writes the given data to the file at the given path. /// Writes the given data to the file at the given path.
pub fn write_file(path: &str, data: &[u8]) { pub fn write_file(path: &str, data: &[u8]) -> Result<(), String> {
let mut file = match File::create(path) { let mut file = match File::create(path) {
Ok(file) => file, Ok(file) => file,
Err(_) => panic!("Failed to open file: {}", path), Err(_) => return Err(format!("Failed to open file: {}", path)),
}; };
file.write_all(data).unwrap() file.write_all(data).unwrap();
Ok(())
} }
/// Replaces the extension in the given path with the provided extension. /// Replaces the extension in the given path with the provided extension.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment