From deab78e39dbc29fdcac85c4d24756642049f690f Mon Sep 17 00:00:00 2001 From: Olle Ronstad Date: Tue, 9 Jul 2024 14:08:57 +0200 Subject: [PATCH] Added SimulatorError --- src/common.rs | 3 +++ src/gui_egui/keymap.rs | 2 +- src/gui_egui/menu.rs | 2 +- src/simulator.rs | 21 +++++++++++++-------- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/common.rs b/src/common.rs index 9ce785e4..da7c4430 100644 --- a/src/common.rs +++ b/src/common.rs @@ -23,6 +23,9 @@ type Components = Vec>; #[cfg(feature = "gui-egui")] pub type Components = Vec>; +pub enum SimulatorError { + RunningStateIsErr(), +} #[derive(PartialEq, Clone, Debug)] pub enum RunningState { Running, diff --git a/src/gui_egui/keymap.rs b/src/gui_egui/keymap.rs index ca797bd8..8217022b 100644 --- a/src/gui_egui/keymap.rs +++ b/src/gui_egui/keymap.rs @@ -399,7 +399,7 @@ pub fn control_play_fn(gui: &mut Gui) { pub fn control_pause_fn(gui: &mut Gui) { if !gui.editor_use { gui.pause = true; - gui.simulator.as_mut().unwrap().stop(); + let _ = gui.simulator.as_mut().unwrap().stop(); } } pub fn control_reset_fn(gui: &mut Gui) { diff --git a/src/gui_egui/menu.rs b/src/gui_egui/menu.rs index 0e5f7b04..47c7ae99 100644 --- a/src/gui_egui/menu.rs +++ b/src/gui_egui/menu.rs @@ -42,7 +42,7 @@ impl Menu { if ui.button("⟳").clicked() { // TODO dont have simulator here add keymap if let Some(s) = gui.simulator.as_mut() { - s.set_step_to(s.cycle + gui.step_amount); + let _ = s.set_step_to(s.cycle + gui.step_amount); } } diff --git a/src/simulator.rs b/src/simulator.rs index 16c57bdd..6e122f10 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -1,6 +1,6 @@ use crate::common::{ Component, ComponentStore, Condition, Id, Input, OutputType, RunningState, Signal, SignalFmt, - SignalValue, Simulator, + SignalValue, Simulator, SimulatorError, }; use log::*; use petgraph::{ @@ -318,8 +318,13 @@ impl Simulator { pub fn run_threaded(&mut self) {} /// stop the simulator from gui or other external reason - pub fn stop(&mut self) { - self.running_state = RunningState::Stopped; + pub fn stop(&mut self) -> Result<(), SimulatorError> { + if self.running_state != RunningState::Err { + self.running_state = RunningState::Stopped; + Ok(()) + } else { + Err(SimulatorError::RunningStateIsErr()) + } } /// reverse simulation using history if clock > 1 @@ -343,7 +348,7 @@ impl Simulator { self.history = vec![]; self.cycle = 0; self.sim_state.iter_mut().for_each(|val| *val = 0.into()); - self.stop(); + self.running_state = RunningState::Stopped; self.clock(); // TODO probably needed to reset component_condition, maybe is handeld correctly by clock who knows? for component in self.ordered_components.clone() { @@ -368,22 +373,22 @@ impl Simulator { } // TODO return error if simulator running state is Err - pub fn set_running(&mut self) -> Result<(), ()> { + pub fn set_running(&mut self) -> Result<(), SimulatorError> { if self.running_state != RunningState::Err { self.running_state = RunningState::Running; Ok(()) } else { - Err(()) + Err(SimulatorError::RunningStateIsErr()) } } // TODO return error if simulator running state is Err - pub fn set_step_to(&mut self, target_cycle: usize) -> Result<(), ()> { + pub fn set_step_to(&mut self, target_cycle: usize) -> Result<(), SimulatorError> { if self.running_state != RunningState::Err { self.running_state = RunningState::StepTo(target_cycle); Ok(()) } else { - Err(()) + Err(SimulatorError::RunningStateIsErr()) } } // This function returns Some() if there are any components