made_core/value_objects/ceremony/
guard_name.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
8#[serde(transparent)]
9pub struct GuardName(String);
10
11impl GuardName {
12 pub fn new(raw: impl Into<String>) -> Result<Self, DomainError> {
13 let value = raw.into();
14 let trimmed = value.trim();
15 if trimmed.is_empty() {
16 return Err(DomainError::EmptyField {
17 field: "guard_name",
18 });
19 }
20 if trimmed
21 .chars()
22 .any(|ch| !(ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '_'))
23 {
24 return Err(DomainError::InvalidCharacters {
25 field: "guard_name",
26 });
27 }
28 Ok(Self(trimmed.to_owned()))
29 }
30
31 #[must_use]
32 pub fn as_str(&self) -> &str {
33 &self.0
34 }
35}
36
37impl fmt::Display for GuardName {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.write_str(&self.0)
40 }
41}