made_core/value_objects/ceremony/
max_transitions.rs1use serde::{Deserialize, Serialize};
2
3use crate::error::DomainError;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
7#[serde(try_from = "u32", into = "u32")]
8pub struct MaxTransitions(u32);
9
10impl MaxTransitions {
11 pub fn new(value: u32) -> Result<Self, DomainError> {
12 if value == 0 {
13 return Err(DomainError::MustBeNonZero {
14 field: "max_transitions",
15 });
16 }
17 Ok(Self(value))
18 }
19
20 #[must_use]
21 pub const fn get(self) -> u32 {
22 self.0
23 }
24}
25
26impl TryFrom<u32> for MaxTransitions {
27 type Error = DomainError;
28
29 fn try_from(value: u32) -> Result<Self, Self::Error> {
30 Self::new(value)
31 }
32}
33
34impl From<MaxTransitions> for u32 {
35 fn from(value: MaxTransitions) -> Self {
36 value.get()
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn transition_caps_are_positive() {
46 assert_eq!(MaxTransitions::new(7).unwrap().get(), 7);
47 assert!(matches!(
48 MaxTransitions::new(0),
49 Err(DomainError::MustBeNonZero {
50 field: "max_transitions"
51 })
52 ));
53 }
54}