From dc85231952471a295df08e2ea88a6e624223df5c Mon Sep 17 00:00:00 2001 From: Olle Ronstad Date: Wed, 27 Aug 2025 13:36:53 +0200 Subject: [PATCH 1/3] change component store to return result instead of panic --- src/component_store.rs | 44 +++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/src/component_store.rs b/src/component_store.rs index a34a9291..e5c4b9c5 100644 --- a/src/component_store.rs +++ b/src/component_store.rs @@ -1,20 +1,50 @@ use crate::common::ComponentStore; -use std::{fs::File, io::prelude::*, path::PathBuf}; +use std::{fmt::Display, fs::File, io::prelude::*, path::PathBuf}; use log::*; +#[derive(Debug)] +pub enum ComponentStoreLoadError { + Json(serde_json::Error), + Io(std::io::Error, PathBuf) +} +impl Display for ComponentStoreLoadError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ComponentStoreLoadError::Json(error) => write!(f,"Error while decoding json: {}",error), + ComponentStoreLoadError::Io(error,path) => write!(f,"Error while reading file {:?} : {}",path,error), + } + } +} + +impl std::error::Error for ComponentStoreLoadError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + ComponentStoreLoadError::Json(error) => Some(error), + ComponentStoreLoadError::Io(error, _) => Some(error), + } + } +} + +impl From for ComponentStoreLoadError { + fn from(value: serde_json::Error) -> Self { + Self::Json(value) + } +} + impl ComponentStore { - pub fn load(json: &str) -> Self { - serde_json::from_str(json).unwrap() + pub fn load(json: &str) -> Result { + serde_json::from_str(json) } - pub fn load_file(path: &PathBuf) -> Self { - let mut file = File::open(path).unwrap(); + pub fn load_file(path: &PathBuf) -> Result { + let mut file = File::open(path).map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?; let mut json = String::new(); - file.read_to_string(&mut json).unwrap(); + file.read_to_string(&mut json).map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?; - ComponentStore::load(&json) + let cs = ComponentStore::load(&json)?; + Ok(cs) } pub fn save_file(&self, path: &PathBuf) { From 9887192836d4eedc3d6bbf2521c777b612f31b34 Mon Sep 17 00:00:00 2001 From: Olle Ronstad Date: Wed, 27 Aug 2025 13:37:25 +0200 Subject: [PATCH 2/3] changed t0 use new signature of component store load --- mips-lib/src/main.rs | 2 +- riscv/examples/empty.rs | 2 +- riscv/examples/riscv.rs | 2 +- src/gui_egui/keymap.rs | 8 +++++--- src/gui_egui/menu.rs | 2 +- src/main.rs | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/mips-lib/src/main.rs b/mips-lib/src/main.rs index 99c9cc67..3b352c23 100644 --- a/mips-lib/src/main.rs +++ b/mips-lib/src/main.rs @@ -23,7 +23,7 @@ fn main() { let args = Args::parse(); let path = PathBuf::from(args.model); - let cs = ComponentStore::load_file(&path); + let cs = ComponentStore::load_file(&path).unwrap_or(ComponentStore { store: vec![] }); #[cfg(feature = "gui-egui")] syncrim::gui_egui::gui(cs, &path, Library::default()).ok(); diff --git a/riscv/examples/empty.rs b/riscv/examples/empty.rs index 90f9189e..7f71cccf 100644 --- a/riscv/examples/empty.rs +++ b/riscv/examples/empty.rs @@ -20,7 +20,7 @@ struct Args { fn main() { let path = PathBuf::from("riscv.json"); - let cs = ComponentStore::load_file(&path); + let cs = ComponentStore::load_file(&path).unwrap(); #[cfg(feature = "gui-egui")] { use riscv::components::*; diff --git a/riscv/examples/riscv.rs b/riscv/examples/riscv.rs index 11eb4b06..fef9928f 100644 --- a/riscv/examples/riscv.rs +++ b/riscv/examples/riscv.rs @@ -78,7 +78,7 @@ fn main() { } } let path = PathBuf::from("riscv.json"); - let mut cs = ComponentStore::load_file(&path); + let mut cs = ComponentStore::load_file(&path).unwrap(); let mut i = 0; let mut store = cs.store.clone(); for component in store.clone() { diff --git a/src/gui_egui/keymap.rs b/src/gui_egui/keymap.rs index 8217022b..b773268f 100644 --- a/src/gui_egui/keymap.rs +++ b/src/gui_egui/keymap.rs @@ -1,4 +1,5 @@ use crate::common::{ComponentStore, Simulator}; +use crate::component_store::ComponentStoreLoadError; use crate::gui_egui::editor::{Editor, EditorMode}; use crate::gui_egui::editor_wire_mode::reset_wire_mode; use crate::gui_egui::gui::create_contexts; @@ -179,7 +180,7 @@ impl Shortcuts { file_new_fn(gui); } if ctx.input_mut(|i| i.consume_shortcut(&self.file_open)) { - file_open_fn(gui); + let _ = file_open_fn(gui); } if ctx.input_mut(|i| i.consume_shortcut(&self.file_save)) { file_save_fn(gui); @@ -245,12 +246,12 @@ impl Shortcuts { } pub fn file_new_fn(_gui: &mut Gui) {} -pub fn file_open_fn(gui: &mut Gui) { +pub fn file_open_fn(gui: &mut Gui) -> Result<(), ComponentStoreLoadError> { let files = FileDialog::new().add_filter("json", &["json"]).pick_file(); if let Some(path_buf) = files { gui.path = path_buf; } - let cs = ComponentStore::load_file(&gui.path); + let cs = ComponentStore::load_file(&gui.path)?; let contexts = create_contexts(&cs.store); match gui.editor_use { true => { @@ -276,6 +277,7 @@ pub fn file_open_fn(gui: &mut Gui) { } } } + Ok(()) } pub fn file_save_fn(gui: &mut Gui) { match gui.editor_use { diff --git a/src/gui_egui/menu.rs b/src/gui_egui/menu.rs index d0d8943f..d773729d 100644 --- a/src/gui_egui/menu.rs +++ b/src/gui_egui/menu.rs @@ -145,7 +145,7 @@ fn shared_buttons_file(gui: &mut Gui, ui: &mut Ui) { keymap::file_new_fn(gui); } if btn(ui, "Open", gui.shortcuts.file_open).clicked() { - keymap::file_open_fn(gui); + let _ = keymap::file_open_fn(gui); } ui.menu_button("Open Recent", |_ui| { // Recent here diff --git a/src/main.rs b/src/main.rs index 5b17ae69..1bbce81f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ fn main() { let args = Args::parse(); let path = PathBuf::from(args.model); - let cs = ComponentStore::load_file(&path); + let cs = ComponentStore::load_file(&path).unwrap(); #[cfg(feature = "gui-egui")] syncrim::gui_egui::gui(cs, &path, Library::default()).ok(); From fad245c38938a5a8fb41ac5d8b318aa22f1c02e6 Mon Sep 17 00:00:00 2001 From: Olle Ronstad Date: Wed, 27 Aug 2025 13:48:49 +0200 Subject: [PATCH 3/3] rust fmt --- src/component_store.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/component_store.rs b/src/component_store.rs index e5c4b9c5..58585807 100644 --- a/src/component_store.rs +++ b/src/component_store.rs @@ -7,13 +7,17 @@ use log::*; #[derive(Debug)] pub enum ComponentStoreLoadError { Json(serde_json::Error), - Io(std::io::Error, PathBuf) + Io(std::io::Error, PathBuf), } impl Display for ComponentStoreLoadError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - ComponentStoreLoadError::Json(error) => write!(f,"Error while decoding json: {}",error), - ComponentStoreLoadError::Io(error,path) => write!(f,"Error while reading file {:?} : {}",path,error), + ComponentStoreLoadError::Json(error) => { + write!(f, "Error while decoding json: {}", error) + } + ComponentStoreLoadError::Io(error, path) => { + write!(f, "Error while reading file {:?} : {}", path, error) + } } } } @@ -38,10 +42,12 @@ impl ComponentStore { serde_json::from_str(json) } - pub fn load_file(path: &PathBuf) -> Result { - let mut file = File::open(path).map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?; + pub fn load_file(path: &PathBuf) -> Result { + let mut file = + File::open(path).map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?; let mut json = String::new(); - file.read_to_string(&mut json).map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?; + file.read_to_string(&mut json) + .map_err(|err| ComponentStoreLoadError::Io(err, path.clone()))?; let cs = ComponentStore::load(&json)?; Ok(cs)