made_core/entities/
context_reference.rs1use serde::{Deserialize, Serialize};
2
3use crate::error::DomainError;
4use crate::value_objects::Attributes;
5
6use super::external_context_validation::{
7 normalize_optional, validate_text, MAX_REFERENCE_ID_LEN, MAX_URI_LEN,
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct ContextReference {
13 reference_id: String,
14 uri: String,
15 title: Option<String>,
16 media_type: Option<String>,
17 #[serde(default)]
18 attributes: Attributes,
19}
20
21impl ContextReference {
22 pub fn new(
23 reference_id: impl Into<String>,
24 uri: impl Into<String>,
25 title: Option<String>,
26 media_type: Option<String>,
27 attributes: Attributes,
28 ) -> Result<Self, DomainError> {
29 let reference_id = reference_id.into();
30 let uri = uri.into();
31 Ok(Self {
32 reference_id: validate_text(
33 &reference_id,
34 "external_context.reference_id",
35 MAX_REFERENCE_ID_LEN,
36 )?,
37 uri: validate_text(&uri, "external_context.reference.uri", MAX_URI_LEN)?,
38 title: normalize_optional(title),
39 media_type: normalize_optional(media_type),
40 attributes,
41 })
42 }
43
44 #[must_use]
45 pub fn reference_id(&self) -> &str {
46 &self.reference_id
47 }
48
49 #[must_use]
50 pub fn uri(&self) -> &str {
51 &self.uri
52 }
53
54 #[must_use]
55 pub fn title(&self) -> Option<&str> {
56 self.title.as_deref()
57 }
58
59 #[must_use]
60 pub fn media_type(&self) -> Option<&str> {
61 self.media_type.as_deref()
62 }
63
64 #[must_use]
65 pub fn attributes(&self) -> &Attributes {
66 &self.attributes
67 }
68}