Skip to main content

made_core/value_objects/agentic_system/
responsibility.rs

1use std::fmt;
2
3use serde::{Deserialize, Deserializer, Serialize};
4
5use crate::error::DomainError;
6
7use super::slug;
8
9const MAX_LEN: usize = 1000;
10
11/// What one business role is answerable for.
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
13#[serde(transparent)]
14pub struct Responsibility(String);
15
16impl Responsibility {
17    pub fn new(raw: impl AsRef<str>) -> Result<Self, DomainError> {
18        slug::text("responsibility", raw.as_ref(), MAX_LEN).map(Self)
19    }
20
21    #[must_use]
22    pub fn as_str(&self) -> &str {
23        &self.0
24    }
25
26    #[must_use]
27    pub fn into_inner(self) -> String {
28        self.0
29    }
30}
31
32impl fmt::Display for Responsibility {
33    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
34        formatter.write_str(&self.0)
35    }
36}
37
38impl TryFrom<&str> for Responsibility {
39    type Error = DomainError;
40
41    fn try_from(value: &str) -> Result<Self, Self::Error> {
42        Self::new(value)
43    }
44}
45
46impl TryFrom<String> for Responsibility {
47    type Error = DomainError;
48
49    fn try_from(value: String) -> Result<Self, Self::Error> {
50        Self::new(value)
51    }
52}
53
54/// Decoding goes through the constructor.
55///
56/// A derived implementation would accept a stored or transmitted value
57/// this type refuses to be built from, and the invariant would hold
58/// everywhere except where the document came from outside — which is
59/// the only place it was ever at risk.
60impl<'de> Deserialize<'de> for Responsibility {
61    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
62        Self::new(String::deserialize(deserializer)?).map_err(serde::de::Error::custom)
63    }
64}