From 2aecc2685b0f672470c1466ce3a3d6a7db306dcb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= <joamag@gmail.com>
Date: Tue, 8 Aug 2023 18:38:44 +0100
Subject: [PATCH] chore: better state manager naming

---
 frontends/sdl/src/main.rs |  6 ++--
 src/state.rs              | 70 +++++++++++++++++++++------------------
 2 files changed, 40 insertions(+), 36 deletions(-)

diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs
index 5b7c56df..8e45d890 100644
--- a/frontends/sdl/src/main.rs
+++ b/frontends/sdl/src/main.rs
@@ -14,7 +14,7 @@ use boytacean::{
     ppu::PaletteInfo,
     rom::Cartridge,
     serial::{NullDevice, SerialDevice},
-    state::{load_state_file, save_state_file},
+    state::StateManager,
     util::{replace_ext, write_file},
 };
 use chrono::Utc;
@@ -286,7 +286,7 @@ impl Emulator {
     }
 
     fn save_state(&mut self, file_path: &str) {
-        if let Err(message) = save_state_file(file_path, &mut self.system) {
+        if let Err(message) = StateManager::save_file(file_path, &mut self.system) {
             println!("Error saving state: {}", message)
         } else {
             println!("Saved state into: {}", file_path)
@@ -294,7 +294,7 @@ impl Emulator {
     }
 
     fn load_state(&mut self, file_path: &str) {
-        if let Err(message) = load_state_file(file_path, &mut self.system) {
+        if let Err(message) = StateManager::load_file(file_path, &mut self.system) {
             println!("Error loading state: {}", message)
         } else {
             println!("Loaded state from: {}", file_path)
diff --git a/src/state.rs b/src/state.rs
index e671bd80..df9717e4 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -966,37 +966,41 @@ impl Default for BeesMbc {
     }
 }
 
-pub fn save_state_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> {
-    let mut file = match File::create(file_path) {
-        Ok(file) => file,
-        Err(_) => return Err(format!("Failed to open file: {}", file_path)),
-    };
-    let data = save_state(gb)?;
-    file.write_all(&data).unwrap();
-    Ok(())
-}
-
-pub fn save_state(gb: &mut GameBoy) -> Result<Vec<u8>, String> {
-    let mut data: Vec<u8> = vec![];
-    BeesState::from_gb(gb).write(&mut data);
-    Ok(data)
-}
-
-pub fn load_state_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> {
-    let mut file = match File::open(file_path) {
-        Ok(file) => file,
-        Err(_) => return Err(format!("Failed to open file: {}", file_path)),
-    };
-    let mut data = vec![];
-    file.read_to_end(&mut data).unwrap();
-    load_state(&data, gb)?;
-    Ok(())
-}
-
-pub fn load_state(data: &[u8], gb: &mut GameBoy) -> Result<(), String> {
-    let mut state = BeesState::default();
-    state.read(&mut Cursor::new(data.to_vec()));
-    state.to_gb(gb)?;
-    print!("{}", state);
-    Ok(())
+pub struct StateManager;
+
+impl StateManager {
+    pub fn save_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> {
+        let mut file = match File::create(file_path) {
+            Ok(file) => file,
+            Err(_) => return Err(format!("Failed to open file: {}", file_path)),
+        };
+        let data = Self::save(gb)?;
+        file.write_all(&data).unwrap();
+        Ok(())
+    }
+
+    pub fn save(gb: &mut GameBoy) -> Result<Vec<u8>, String> {
+        let mut data: Vec<u8> = vec![];
+        BeesState::from_gb(gb).write(&mut data);
+        Ok(data)
+    }
+
+    pub fn load_file(file_path: &str, gb: &mut GameBoy) -> Result<(), String> {
+        let mut file = match File::open(file_path) {
+            Ok(file) => file,
+            Err(_) => return Err(format!("Failed to open file: {}", file_path)),
+        };
+        let mut data = vec![];
+        file.read_to_end(&mut data).unwrap();
+        Self::load(&data, gb)?;
+        Ok(())
+    }
+
+    pub fn load(data: &[u8], gb: &mut GameBoy) -> Result<(), String> {
+        let mut state = BeesState::default();
+        state.read(&mut Cursor::new(data.to_vec()));
+        state.to_gb(gb)?;
+        print!("{}", state);
+        Ok(())
+    }
 }
-- 
GitLab