Skip to main content

made_core/value_objects/ceremony/
child_depth.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 ChildDepth(u16);
8
9impl ChildDepth {
10    pub const FIRST: Self = Self(1);
11    pub fn new(value: u16) -> Result<Self, DomainError> {
12        if value == 0 {
13            return Err(DomainError::MustBeNonZero {
14                field: "child_depth",
15            });
16        }
17        Ok(Self(value))
18    }
19    pub fn next(self) -> Result<Self, DomainError> {
20        self.0
21            .checked_add(1)
22            .map(Self)
23            .ok_or(DomainError::InvariantViolated {
24                reason: "child ceremony depth exhausted",
25            })
26    }
27    #[must_use]
28    pub const fn get(self) -> u16 {
29        self.0
30    }
31}
32impl TryFrom<u16> for ChildDepth {
33    type Error = DomainError;
34    fn try_from(value: u16) -> Result<Self, Self::Error> {
35        Self::new(value)
36    }
37}
38impl From<ChildDepth> for u16 {
39    fn from(value: ChildDepth) -> Self {
40        value.0
41    }
42}