actionqueue_core/ids/
department_id.rs1use std::fmt::{Display, Formatter};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize))]
16pub struct DepartmentId(String);
17
18#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum DepartmentIdError {
21 Empty,
23 TooLong {
25 length: usize,
27 },
28}
29
30impl std::fmt::Display for DepartmentIdError {
31 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32 match self {
33 DepartmentIdError::Empty => write!(f, "department identifier must be non-empty"),
34 DepartmentIdError::TooLong { length } => {
35 write!(f, "department identifier length {length} exceeds maximum 128 characters")
36 }
37 }
38 }
39}
40
41impl std::error::Error for DepartmentIdError {}
42
43impl DepartmentId {
44 pub const MAX_LEN: usize = 128;
46
47 pub fn new(value: impl Into<String>) -> Result<Self, DepartmentIdError> {
54 let value = value.into();
55 if value.is_empty() {
56 return Err(DepartmentIdError::Empty);
57 }
58 if value.len() > Self::MAX_LEN {
59 return Err(DepartmentIdError::TooLong { length: value.len() });
60 }
61 Ok(DepartmentId(value))
62 }
63
64 pub fn as_str(&self) -> &str {
66 &self.0
67 }
68}
69
70impl Display for DepartmentId {
71 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
72 write!(f, "{}", self.0)
73 }
74}
75
76#[cfg(feature = "serde")]
77impl<'de> serde::Deserialize<'de> for DepartmentId {
78 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
79 where
80 D: serde::Deserializer<'de>,
81 {
82 let s = String::deserialize(deserializer)?;
83 DepartmentId::new(s).map_err(serde::de::Error::custom)
84 }
85}