made_core/value_objects/
execution_id.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7const MAX_EXECUTION_ID_LEN: usize = 128;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct ExecutionId(String);
13
14impl ExecutionId {
15 pub fn new(raw: impl Into<String>) -> Result<Self, DomainError> {
16 let raw = raw.into();
17 let value = raw.trim();
18 if value.is_empty() {
19 return Err(DomainError::EmptyField {
20 field: "execution.id",
21 });
22 }
23 if value.len() > MAX_EXECUTION_ID_LEN {
24 return Err(DomainError::FieldTooLong {
25 field: "execution.id",
26 actual: value.len(),
27 max: MAX_EXECUTION_ID_LEN,
28 });
29 }
30 Ok(Self(value.to_owned()))
31 }
32
33 #[must_use]
34 pub fn as_str(&self) -> &str {
35 &self.0
36 }
37}
38
39impl fmt::Display for ExecutionId {
40 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
41 formatter.write_str(self.as_str())
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn trims_and_keeps_a_valid_identity() {
51 assert_eq!(
52 ExecutionId::new(" invocation-1 ").unwrap().as_str(),
53 "invocation-1"
54 );
55 }
56
57 #[test]
58 fn rejects_an_empty_identity() {
59 assert!(matches!(
60 ExecutionId::new(" ").unwrap_err(),
61 DomainError::EmptyField {
62 field: "execution.id"
63 }
64 ));
65 }
66}