Skip to content

Commit

Permalink
Added SimulatorError
Browse files Browse the repository at this point in the history
  • Loading branch information
Spooky-Firefox committed Jul 10, 2024
1 parent 2028bb1 commit deab78e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Components = Vec<Rc<dyn ViziaComponent>>;
#[cfg(feature = "gui-egui")]
pub type Components = Vec<Rc<dyn EguiComponent>>;

pub enum SimulatorError {
RunningStateIsErr(),
}
#[derive(PartialEq, Clone, Debug)]
pub enum RunningState {
Running,
Expand Down
2 changes: 1 addition & 1 deletion src/gui_egui/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/gui_egui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
21 changes: 13 additions & 8 deletions src/simulator.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit deab78e

Please sign in to comment.