Skip to main content

made_core/value_objects/ceremony/
ceremony_id.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7const MAX_ID_LEN: usize = 256;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10#[serde(transparent)]
11pub struct CeremonyId(String);
12
13impl CeremonyId {
14    pub fn new(raw: impl Into<String>) -> Result<Self, DomainError> {
15        let value = raw.into();
16        let trimmed = value.trim();
17        if trimmed.is_empty() {
18            return Err(DomainError::EmptyField {
19                field: "ceremony_id",
20            });
21        }
22        if trimmed.len() > MAX_ID_LEN {
23            return Err(DomainError::FieldTooLong {
24                field: "ceremony_id",
25                actual: trimmed.len(),
26                max: MAX_ID_LEN,
27            });
28        }
29        if trimmed.chars().any(char::is_control) {
30            return Err(DomainError::InvalidCharacters {
31                field: "ceremony_id",
32            });
33        }
34        Ok(Self(trimmed.to_owned()))
35    }
36
37    #[must_use]
38    pub fn as_str(&self) -> &str {
39        &self.0
40    }
41}
42
43impl fmt::Display for CeremonyId {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.write_str(&self.0)
46    }
47}