sorting_race/models/
algorithm.rs1#[derive(Debug, Clone, PartialEq, Default)]
5pub enum AlgorithmState {
6 #[default]
8 Ready,
9 Running,
11 Complete,
13 Error(String),
15}
16
17#[derive(Debug, Default)]
19pub struct Algorithm {
20 pub state: AlgorithmState,
22 pub steps_executed: usize,
24 pub runtime_ms: u64,
26 pub is_paused: bool,
28}
29
30impl Algorithm {
31 pub fn new() -> Self {
33 Self::default()
34 }
35
36 pub fn reset(&mut self) {
38 self.state = AlgorithmState::Ready;
39 self.steps_executed = 0;
40 self.runtime_ms = 0;
41 self.is_paused = false;
42 }
43
44 pub fn can_step(&self) -> bool {
46 matches!(self.state, AlgorithmState::Ready | AlgorithmState::Running) && !self.is_paused
47 }
48
49 pub fn complete(&mut self) {
51 self.state = AlgorithmState::Complete;
52 }
53
54 pub fn start(&mut self) {
56 self.state = AlgorithmState::Running;
57 }
58
59 pub fn set_error(&mut self, error: String) {
61 self.state = AlgorithmState::Error(error);
62 }
63
64 pub fn increment_steps(&mut self) {
66 self.steps_executed += 1;
67 }
68
69 pub fn add_runtime(&mut self, ms: u64) {
71 self.runtime_ms += ms;
72 }
73
74 pub fn toggle_pause(&mut self) {
76 self.is_paused = !self.is_paused;
77 }
78}