made_core/value_objects/artifact/
artifact_id.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::DomainError;
6
7const MAX_LENGTH: usize = 256;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
11#[serde(try_from = "String", into = "String")]
12pub struct ArtifactId(String);
13
14impl ArtifactId {
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: "artifact_id",
21 });
22 }
23 if value.len() > MAX_LENGTH {
24 return Err(DomainError::FieldTooLong {
25 field: "artifact_id",
26 actual: value.len(),
27 max: MAX_LENGTH,
28 });
29 }
30 if value.chars().any(char::is_control) {
31 return Err(DomainError::InvalidCharacters {
32 field: "artifact_id",
33 });
34 }
35 Ok(Self(value.to_owned()))
36 }
37
38 #[must_use]
39 pub fn as_str(&self) -> &str {
40 &self.0
41 }
42}
43
44impl TryFrom<String> for ArtifactId {
45 type Error = DomainError;
46
47 fn try_from(value: String) -> Result<Self, Self::Error> {
48 Self::new(value)
49 }
50}
51
52impl From<ArtifactId> for String {
53 fn from(value: ArtifactId) -> Self {
54 value.0
55 }
56}
57
58impl fmt::Display for ArtifactId {
59 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
60 formatter.write_str(&self.0)
61 }
62}