Skip to main content

made_core/value_objects/ceremony/
max_parallel.rs

1use serde::{Deserialize, Serialize};
2
3use crate::error::DomainError;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
6#[serde(try_from = "u8", into = "u8")]
7pub struct MaxParallel(u8);
8
9impl MaxParallel {
10    pub const DEFAULT: Self = Self(3);
11    pub const SERVER_MAX: Self = Self(8);
12
13    pub fn new(value: u8) -> Result<Self, DomainError> {
14        if !(1..=8).contains(&value) {
15            return Err(DomainError::OutOfRange {
16                field: "max_parallel",
17                value: f64::from(value),
18                min: 1.0,
19                max: 8.0,
20            });
21        }
22        Ok(Self(value))
23    }
24
25    #[must_use]
26    pub const fn get(self) -> u8 {
27        self.0
28    }
29
30    #[must_use]
31    pub const fn effective_with(self, ceiling: Self) -> Self {
32        if self.0 < ceiling.0 {
33            self
34        } else {
35            ceiling
36        }
37    }
38
39    #[must_use]
40    pub const fn is_default(&self) -> bool {
41        self.0 == Self::DEFAULT.0
42    }
43}
44
45impl Default for MaxParallel {
46    fn default() -> Self {
47        Self::DEFAULT
48    }
49}
50
51impl TryFrom<u8> for MaxParallel {
52    type Error = DomainError;
53    fn try_from(value: u8) -> Result<Self, Self::Error> {
54        Self::new(value)
55    }
56}
57
58impl From<MaxParallel> for u8 {
59    fn from(value: MaxParallel) -> Self {
60        value.get()
61    }
62}