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