Skip to main content

actionqueue_core/ids/
department_id.rs

1use std::fmt::{Display, Formatter};
2
3/// A human-readable identifier for a department or actor group.
4///
5/// Department identifiers use string labels (e.g. `"engineering"`, `"ops"`)
6/// rather than UUIDs, so that task routing rules are human-readable in
7/// configuration and WAL events.
8///
9/// # Invariants
10///
11/// - The identifier string must be non-empty.
12/// - The identifier string must not exceed 128 characters.
13/// - Validated at construction; never stores an invalid value.
14#[derive(Debug, Clone, PartialEq, Eq, Hash)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize))]
16pub struct DepartmentId(String);
17
18/// Error returned when a `DepartmentId` cannot be constructed.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum DepartmentIdError {
21    /// The identifier string was empty.
22    Empty,
23    /// The identifier string exceeded the maximum allowed length.
24    TooLong {
25        /// The length of the rejected string.
26        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    /// Maximum allowed length for a department identifier.
45    pub const MAX_LEN: usize = 128;
46
47    /// Creates a new `DepartmentId` from a string value.
48    ///
49    /// # Errors
50    ///
51    /// Returns [`DepartmentIdError::Empty`] if `value` is empty.
52    /// Returns [`DepartmentIdError::TooLong`] if `value` exceeds 128 characters.
53    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    /// Returns the department identifier string.
65    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}