a2a_protocol_types/
artifact.rs1use serde::{Deserialize, Serialize};
14
15use crate::message::Part;
16
17#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
23pub struct ArtifactId(pub String);
24
25impl ArtifactId {
26 #[must_use]
28 pub fn new(s: impl Into<String>) -> Self {
29 Self(s.into())
30 }
31}
32
33impl std::fmt::Display for ArtifactId {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 f.write_str(&self.0)
36 }
37}
38
39impl From<String> for ArtifactId {
40 fn from(s: String) -> Self {
41 Self(s)
42 }
43}
44
45impl From<&str> for ArtifactId {
46 fn from(s: &str) -> Self {
47 Self(s.to_owned())
48 }
49}
50
51impl AsRef<str> for ArtifactId {
52 fn as_ref(&self) -> &str {
53 &self.0
54 }
55}
56
57include!("generated/artifact.rs");
61
62impl Artifact {
63 #[must_use]
70 pub fn new(id: impl Into<ArtifactId>, parts: Vec<Part>) -> Self {
71 debug_assert!(
72 !parts.is_empty(),
73 "Artifact parts must not be empty per A2A spec"
74 );
75 Self {
76 id: id.into(),
77 name: None,
78 description: None,
79 parts,
80 extensions: None,
81 metadata: None,
82 }
83 }
84
85 #[must_use]
100 pub fn text(&self) -> Option<&str> {
101 self.parts.iter().find_map(Part::text_content)
102 }
103
104 pub fn texts(&self) -> impl Iterator<Item = &str> {
107 self.parts.iter().filter_map(Part::text_content)
108 }
109
110 pub fn validate(&self) -> Result<(), crate::error::A2aError> {
118 if self.parts.is_empty() {
119 return Err(crate::error::A2aError::invalid_params(
120 "artifact must contain at least one part",
121 ));
122 }
123 Ok(())
124 }
125}
126
127#[cfg(test)]
130mod tests {
131 use super::*;
132 use crate::message::Part;
133
134 #[test]
135 fn text_skips_non_text_parts_but_still_finds_text() {
136 let no_text = Artifact::new("a1", vec![Part::url("https://example.com/f.pdf")]);
138 assert_eq!(no_text.text(), None, "no text part means no text");
139
140 let with_text = Artifact::new(
142 "a2",
143 vec![Part::url("https://example.com/f.pdf"), Part::text("done")],
144 );
145 assert_eq!(
146 with_text.text(),
147 Some("done"),
148 "a non-text part must be skipped, not end the search"
149 );
150 }
151
152 #[test]
153 fn text_returns_the_first_text_part_and_texts_returns_all() {
154 let artifact = Artifact::new(
155 "a3",
156 vec![
157 Part::text("first"),
158 Part::url("https://example.com/f.pdf"),
159 Part::text("second"),
160 ],
161 );
162
163 assert_eq!(artifact.text(), Some("first"));
164 assert_eq!(
165 artifact.texts().collect::<Vec<_>>(),
166 vec!["first", "second"]
167 );
168 }
169
170 #[test]
171 fn artifact_roundtrip() {
172 let artifact = Artifact::new("art-1", vec![Part::text("result content")]);
173 let json = serde_json::to_string(&artifact).expect("serialize");
174 assert!(json.contains("\"artifactId\":\"art-1\""));
175 assert!(json.contains("\"text\":\"result content\""));
176
177 let back: Artifact = serde_json::from_str(&json).expect("deserialize");
178 assert_eq!(back.id, ArtifactId::new("art-1"));
179 assert_eq!(back.parts.len(), 1);
180 }
181
182 #[test]
183 fn optional_fields_omitted() {
184 let artifact = Artifact::new("art-2", vec![Part::text("x")]);
185 let json = serde_json::to_string(&artifact).expect("serialize");
186 assert!(!json.contains("\"name\""), "name should be omitted");
187 assert!(
188 !json.contains("\"description\""),
189 "description should be omitted"
190 );
191 assert!(!json.contains("\"metadata\""), "metadata should be omitted");
192 }
193
194 #[test]
195 fn artifact_id_from_string() {
196 let id: ArtifactId = String::from("art-from-string").into();
197 assert_eq!(id, ArtifactId::new("art-from-string"));
198 }
199
200 #[test]
201 fn artifact_id_from_str() {
202 let id: ArtifactId = "art-from-str".into();
203 assert_eq!(id, ArtifactId::new("art-from-str"));
204 }
205
206 #[test]
207 fn artifact_id_as_ref() {
208 let id = ArtifactId::new("ref-test");
209 assert_eq!(id.as_ref(), "ref-test");
210 }
211
212 #[test]
213 fn artifact_new_optional_fields_are_none() {
214 let a = Artifact::new("id", vec![Part::text("x")]);
215 assert!(a.name.is_none());
216 assert!(a.description.is_none());
217 assert!(a.extensions.is_none());
218 assert!(a.metadata.is_none());
219 }
220
221 #[test]
222 fn artifact_id_display() {
223 let id = ArtifactId::new("my-artifact");
224 assert_eq!(id.to_string(), "my-artifact");
225 }
226
227 #[test]
230 fn validate_non_empty_parts_succeeds() {
231 let a = Artifact::new("art-ok", vec![Part::text("content")]);
232 assert!(a.validate().is_ok(), "artifact with parts should validate");
233 }
234
235 #[test]
236 fn validate_empty_parts_fails() {
237 let a = Artifact {
238 id: ArtifactId::new("art-empty"),
239 name: None,
240 description: None,
241 parts: vec![],
242 extensions: None,
243 metadata: None,
244 };
245 let err = a.validate().unwrap_err();
246 assert_eq!(
247 err.code,
248 crate::error::ErrorCode::InvalidParams,
249 "empty parts should return InvalidParams error"
250 );
251 assert!(
252 err.message.contains("at least one part"),
253 "error message should mention 'at least one part': {}",
254 err.message
255 );
256 }
257}