made_core/entities/
context_item.rs1use serde::{Deserialize, Serialize};
2
3use crate::error::DomainError;
4use crate::value_objects::Attributes;
5
6use super::external_context_validation::{
7 validate_text, MAX_ITEM_ID_LEN, MAX_ITEM_KIND_LEN, MAX_ITEM_TITLE_LEN, MAX_REFERENCE_ID_LEN,
8};
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct ContextItem {
13 item_id: String,
14 kind: String,
15 title: String,
16 narrative: Option<String>,
17 #[serde(default)]
18 attributes: Attributes,
19 #[serde(default)]
20 reference_ids: Vec<String>,
21}
22
23impl ContextItem {
24 pub fn new(
25 item_id: impl Into<String>,
26 kind: impl Into<String>,
27 title: impl Into<String>,
28 narrative: Option<String>,
29 attributes: Attributes,
30 reference_ids: Vec<String>,
31 ) -> Result<Self, DomainError> {
32 let reference_ids = reference_ids
33 .into_iter()
34 .map(|reference_id| {
35 validate_text(
36 &reference_id,
37 "external_context.item.reference_id",
38 MAX_REFERENCE_ID_LEN,
39 )
40 })
41 .collect::<Result<Vec<_>, _>>()?;
42 let item_id = item_id.into();
43 let kind = kind.into();
44 let title = title.into();
45
46 Ok(Self {
47 item_id: validate_text(&item_id, "external_context.item_id", MAX_ITEM_ID_LEN)?,
48 kind: validate_text(&kind, "external_context.item.kind", MAX_ITEM_KIND_LEN)?,
49 title: validate_text(&title, "external_context.item.title", MAX_ITEM_TITLE_LEN)?,
50 narrative: narrative.and_then(|text| {
51 let trimmed = text.trim().to_owned();
52 (!trimmed.is_empty()).then_some(trimmed)
53 }),
54 attributes,
55 reference_ids,
56 })
57 }
58
59 #[must_use]
60 pub fn item_id(&self) -> &str {
61 &self.item_id
62 }
63
64 #[must_use]
65 pub fn kind(&self) -> &str {
66 &self.kind
67 }
68
69 #[must_use]
70 pub fn title(&self) -> &str {
71 &self.title
72 }
73
74 #[must_use]
75 pub fn narrative(&self) -> Option<&str> {
76 self.narrative.as_deref()
77 }
78
79 #[must_use]
80 pub fn attributes(&self) -> &Attributes {
81 &self.attributes
82 }
83
84 #[must_use]
85 pub fn reference_ids(&self) -> &[String] {
86 &self.reference_ids
87 }
88}