made_core/value_objects/outbox/
outbox_subject.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7const MAX_SUBJECT_LEN: usize = 256;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
15#[serde(transparent)]
16pub struct OutboxSubject(String);
17
18impl OutboxSubject {
19 pub fn new(raw: impl Into<String>) -> Result<Self, DomainError> {
20 let raw = raw.into();
21 let trimmed = raw.trim();
22 if trimmed.is_empty() {
23 return Err(DomainError::EmptyField {
24 field: "outbox_subject",
25 });
26 }
27 if trimmed.len() > MAX_SUBJECT_LEN {
28 return Err(DomainError::FieldTooLong {
29 field: "outbox_subject",
30 actual: trimmed.len(),
31 max: MAX_SUBJECT_LEN,
32 });
33 }
34 if trimmed.chars().any(char::is_whitespace) {
35 return Err(DomainError::InvalidCharacters {
36 field: "outbox_subject",
37 });
38 }
39 Ok(Self(trimmed.to_owned()))
40 }
41
42 #[must_use]
43 pub fn as_str(&self) -> &str {
44 &self.0
45 }
46}
47
48impl fmt::Display for OutboxSubject {
49 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
50 formatter.write_str(&self.0)
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn an_empty_or_spaced_subject_is_rejected() {
60 assert!(OutboxSubject::new(" ").is_err());
61 assert!(OutboxSubject::new("two words").is_err());
62 }
63
64 #[test]
65 fn a_dotted_subject_is_accepted_and_trimmed() {
66 let subject = OutboxSubject::new(" made.ceremony.completed ").unwrap();
67
68 assert_eq!(subject.as_str(), "made.ceremony.completed");
69 }
70}