made_core/value_objects/ceremony/
ceremony_name.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7const MAX_NAME_LEN: usize = 128;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10#[serde(transparent)]
11pub struct CeremonyName(String);
12
13impl CeremonyName {
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_name",
20 });
21 }
22 if trimmed.len() > MAX_NAME_LEN {
23 return Err(DomainError::FieldTooLong {
24 field: "ceremony_name",
25 actual: trimmed.len(),
26 max: MAX_NAME_LEN,
27 });
28 }
29 if trimmed
30 .chars()
31 .any(|ch| !(ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '_'))
32 {
33 return Err(DomainError::InvalidCharacters {
34 field: "ceremony_name",
35 });
36 }
37 Ok(Self(trimmed.to_owned()))
38 }
39
40 #[must_use]
41 pub fn as_str(&self) -> &str {
42 &self.0
43 }
44}
45
46impl fmt::Display for CeremonyName {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 f.write_str(&self.0)
49 }
50}