Skip to main content

made_core/value_objects/ceremony/
max_children.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 = "u16", into = "u16")]
7pub struct MaxChildren(u16);
8
9impl MaxChildren {
10    pub const SERVER_MAX: Self = Self(64);
11
12    pub fn new(value: u16) -> Result<Self, DomainError> {
13        if !(1..=Self::SERVER_MAX.0).contains(&value) {
14            return Err(DomainError::OutOfRange {
15                field: "max_children",
16                value: f64::from(value),
17                min: 1.0,
18                max: f64::from(Self::SERVER_MAX.0),
19            });
20        }
21        Ok(Self(value))
22    }
23
24    #[must_use]
25    pub const fn get(self) -> u16 {
26        self.0
27    }
28}
29
30impl TryFrom<u16> for MaxChildren {
31    type Error = DomainError;
32    fn try_from(value: u16) -> Result<Self, Self::Error> {
33        Self::new(value)
34    }
35}
36impl From<MaxChildren> for u16 {
37    fn from(value: MaxChildren) -> Self {
38        value.0
39    }
40}