Skip to main content

made_core/value_objects/
council_journal_position.rs

1use crate::error::DomainError;
2use serde::{Deserialize, Serialize};
3
4/// An ordinal in the council journal. It cannot be a ceremony cursor.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
6#[serde(try_from = "u64", into = "u64")]
7pub struct CouncilJournalPosition(u64);
8
9impl CouncilJournalPosition {
10    pub const FIRST: Self = Self(1);
11    pub fn new(value: u64) -> Result<Self, DomainError> {
12        if value == 0 {
13            return Err(DomainError::MustBeNonZero {
14                field: "council_journal_position",
15            });
16        }
17        Ok(Self(value))
18    }
19    #[must_use]
20    pub fn value(self) -> u64 {
21        self.0
22    }
23    pub fn checked_next(self) -> Result<Self, DomainError> {
24        self.0
25            .checked_add(1)
26            .map(Self)
27            .ok_or(DomainError::InvariantViolated {
28                reason: "council journal position exhausted",
29            })
30    }
31}
32impl TryFrom<u64> for CouncilJournalPosition {
33    type Error = DomainError;
34    fn try_from(value: u64) -> Result<Self, Self::Error> {
35        Self::new(value)
36    }
37}
38impl From<CouncilJournalPosition> for u64 {
39    fn from(value: CouncilJournalPosition) -> Self {
40        value.0
41    }
42}