Skip to main content

made_core/value_objects/ceremony/
state_iteration.rs

1use serde::{Deserialize, Serialize};
2
3use crate::error::DomainError;
4
5pub const MAX_STATE_ITERATIONS: u32 = 1_000;
6
7/// One complete execution of every step in a ceremony state.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct StateIteration(u32);
11
12impl StateIteration {
13    pub const FIRST: Self = Self(1);
14
15    pub fn new(value: u32) -> Result<Self, DomainError> {
16        if value == 0 {
17            return Err(DomainError::MustBeNonZero {
18                field: "state_iteration",
19            });
20        }
21        if value > MAX_STATE_ITERATIONS {
22            return Err(DomainError::OutOfRange {
23                field: "state_iteration",
24                value: f64::from(value),
25                min: 1.0,
26                max: f64::from(MAX_STATE_ITERATIONS),
27            });
28        }
29        Ok(Self(value))
30    }
31
32    pub fn next(self) -> Result<Self, DomainError> {
33        Self::new(self.0.saturating_add(1))
34    }
35
36    #[must_use]
37    pub const fn get(self) -> u32 {
38        self.0
39    }
40
41    #[must_use]
42    pub const fn is_first(&self) -> bool {
43        self.0 == 1
44    }
45}
46
47impl Default for StateIteration {
48    fn default() -> Self {
49        Self::FIRST
50    }
51}