1use std::{collections::BTreeMap, sync::Arc};
2
3use serde::{
5 Deserialize, Deserializer, Serialize, Serializer,
6 ser::{SerializeMap, SerializeSeq},
7};
8
9use super::error::AgentError;
10
11#[derive(Debug, Clone, PartialEq, Serialize)]
14pub struct AgentData {
15 pub kind: String,
16 pub value: AgentValue,
17}
18
19impl AgentData {
20 pub fn unit() -> Self {
21 Self {
22 kind: "unit".to_string(),
23 value: AgentValue::unit(),
24 }
25 }
26
27 pub fn boolean(value: bool) -> Self {
28 AgentData {
29 kind: "boolean".to_string(),
30 value: AgentValue::boolean(value),
31 }
32 }
33
34 pub fn integer(value: i64) -> Self {
35 AgentData {
36 kind: "integer".to_string(),
37 value: AgentValue::integer(value),
38 }
39 }
40
41 pub fn number(value: f64) -> Self {
42 AgentData {
43 kind: "number".to_string(),
44 value: AgentValue::number(value),
45 }
46 }
47
48 pub fn string(value: impl Into<String>) -> Self {
49 AgentData {
50 kind: "string".to_string(),
51 value: AgentValue::string(value.into()),
52 }
53 }
54
55 pub fn text(value: impl Into<String>) -> Self {
56 AgentData {
57 kind: "text".to_string(),
58 value: AgentValue::string(value.into()),
59 }
60 }
61
62 pub fn object(value: AgentValueMap<String, AgentValue>) -> Self {
70 AgentData {
71 kind: "object".to_string(),
72 value: AgentValue::object(value),
73 }
74 }
75
76 pub fn object_with_kind(
77 kind: impl Into<String>,
78 value: AgentValueMap<String, AgentValue>,
79 ) -> Self {
80 AgentData {
81 kind: kind.into(),
82 value: AgentValue::object(value),
83 }
84 }
85
86 pub fn array(kind: impl Into<String>, value: Vec<AgentValue>) -> Self {
87 AgentData {
88 kind: kind.into(),
89 value: AgentValue::array(value),
90 }
91 }
92
93 pub fn from_value(value: AgentValue) -> Self {
94 let kind = value.kind();
95 AgentData { kind, value }
96 }
97
98 pub fn from_json_with_kind(
99 kind: impl Into<String>,
100 value: serde_json::Value,
101 ) -> Result<Self, AgentError> {
102 let kind: String = kind.into();
103 let value = AgentValue::from_kind_json(&kind, value)?;
104 Ok(Self { kind, value })
105 }
106
107 pub fn from_json(json_value: serde_json::Value) -> Result<Self, AgentError> {
108 let value = AgentValue::from_json(json_value)?;
109 Ok(AgentData {
110 kind: value.kind(),
111 value,
112 })
113 }
114
115 pub fn from_serialize<T: Serialize>(value: &T) -> Result<Self, AgentError> {
117 let json_value = serde_json::to_value(value)
118 .map_err(|e| AgentError::InvalidValue(format!("Failed to serialize: {}", e)))?;
119 Self::from_json(json_value)
120 }
121
122 pub fn from_serialize_with_kind<T: Serialize>(
124 kind: impl Into<String>,
125 value: &T,
126 ) -> Result<Self, AgentError> {
127 let json_value = serde_json::to_value(value)
128 .map_err(|e| AgentError::InvalidValue(format!("Failed to serialize: {}", e)))?;
129 Self::from_json_with_kind(kind, json_value)
130 }
131
132 pub fn to_deserialize<T: for<'de> Deserialize<'de>>(&self) -> Result<T, AgentError> {
134 let json_value = self.value.to_json();
135 serde_json::from_value(json_value)
136 .map_err(|e| AgentError::InvalidValue(format!("Failed to deserialize: {}", e)))
137 }
138
139 #[allow(unused)]
140 pub fn is_unit(&self) -> bool {
141 self.kind == "unit"
142 }
143
144 #[allow(unused)]
145 pub fn is_boolean(&self) -> bool {
146 self.kind == "boolean"
147 }
148
149 #[allow(unused)]
150 pub fn is_integer(&self) -> bool {
151 self.kind == "integer"
152 }
153
154 #[allow(unused)]
155 pub fn is_number(&self) -> bool {
156 self.kind == "number"
157 }
158
159 #[allow(unused)]
160 pub fn is_string(&self) -> bool {
161 self.kind == "string"
162 }
163
164 #[allow(unused)]
165 pub fn is_text(&self) -> bool {
166 self.kind == "text"
167 }
168
169 #[allow(unused)]
175 pub fn is_object(&self) -> bool {
176 !self.is_unit()
177 && !self.is_boolean()
178 && !self.is_integer()
179 && !self.is_number()
180 && !self.is_string()
181 && !self.is_text()
182 }
183
184 #[allow(unused)]
185 pub fn is_array(&self) -> bool {
186 if let AgentValue::Array(_) = &self.value {
187 true
188 } else {
189 false
190 }
191 }
192
193 #[allow(unused)]
194 pub fn as_bool(&self) -> Option<bool> {
195 self.value.as_bool()
196 }
197
198 #[allow(unused)]
199 pub fn as_i64(&self) -> Option<i64> {
200 self.value.as_i64()
201 }
202
203 #[allow(unused)]
204 pub fn as_f64(&self) -> Option<f64> {
205 self.value.as_f64()
206 }
207
208 pub fn as_str(&self) -> Option<&str> {
209 self.value.as_str()
210 }
211
212 pub fn as_object(&self) -> Option<&AgentValueMap<String, AgentValue>> {
218 self.value.as_object()
219 }
220
221 #[allow(unused)]
222 pub fn as_array(&self) -> Option<&Vec<AgentValue>> {
223 self.value.as_array()
224 }
225
226 #[allow(unused)]
227 pub fn get(&self, key: &str) -> Option<&AgentValue> {
228 self.value.get(key)
229 }
230
231 #[allow(unused)]
232 pub fn get_bool(&self, key: &str) -> Option<bool> {
233 self.value.get_bool(key)
234 }
235
236 #[allow(unused)]
237 pub fn get_i64(&self, key: &str) -> Option<i64> {
238 self.value.get_i64(key)
239 }
240
241 #[allow(unused)]
242 pub fn get_f64(&self, key: &str) -> Option<f64> {
243 self.value.get_f64(key)
244 }
245
246 #[allow(unused)]
247 pub fn get_str(&self, key: &str) -> Option<&str> {
248 self.value.get_str(key)
249 }
250
251 #[allow(unused)]
257 pub fn get_object(&self, key: &str) -> Option<&AgentValueMap<String, AgentValue>> {
258 self.value.get_object(key)
259 }
260
261 #[allow(unused)]
262 pub fn get_array(&self, key: &str) -> Option<&Vec<AgentValue>> {
263 self.value.get_array(key)
264 }
265}
266
267impl<'de> Deserialize<'de> for AgentData {
268 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
269 where
270 D: Deserializer<'de>,
271 {
272 let json_value = serde_json::Value::deserialize(deserializer)?;
273 let serde_json::Value::Object(obj) = json_value else {
274 return Err(serde::de::Error::custom("not a JSON object"));
275 };
276 let Some(kind) = obj.get("kind").and_then(|k| k.as_str()) else {
277 return Err(serde::de::Error::custom("missing kind"));
278 };
279 let Some(value) = obj.get("value") else {
280 return Err(serde::de::Error::custom("Missing value"));
281 };
282 AgentData::from_json_with_kind(kind, value.to_owned()).map_err(|e| {
283 serde::de::Error::custom(format!("Failed to deserialize AgentData: {}", e))
284 })
285 }
286}
287
288#[derive(Debug, Clone)]
289pub enum AgentValue {
290 Null,
292 Boolean(bool),
293 Integer(i64),
294 Number(f64),
295
296 String(Arc<String>),
298 Array(Arc<Vec<AgentValue>>),
302 Object(Arc<AgentValueMap<String, AgentValue>>),
303}
304
305pub type AgentValueMap<S, T> = BTreeMap<S, T>;
306
307impl AgentValue {
308 pub fn unit() -> Self {
309 AgentValue::Null
310 }
311
312 pub fn boolean(value: bool) -> Self {
313 AgentValue::Boolean(value)
314 }
315
316 pub fn integer(value: i64) -> Self {
317 AgentValue::Integer(value)
318 }
319
320 pub fn number(value: f64) -> Self {
321 AgentValue::Number(value)
322 }
323
324 pub fn string(value: impl Into<String>) -> Self {
325 AgentValue::String(Arc::new(value.into()))
326 }
327
328 pub fn object(value: AgentValueMap<String, AgentValue>) -> Self {
333 AgentValue::Object(Arc::new(value))
334 }
335
336 pub fn array(value: Vec<AgentValue>) -> Self {
337 AgentValue::Array(Arc::new(value))
338 }
339
340 pub fn boolean_default() -> Self {
341 AgentValue::Boolean(false)
342 }
343
344 pub fn integer_default() -> Self {
345 AgentValue::Integer(0)
346 }
347
348 pub fn number_default() -> Self {
349 AgentValue::Number(0.0)
350 }
351
352 pub fn string_default() -> Self {
353 AgentValue::String(Arc::new(String::new()))
354 }
355
356 pub fn array_default() -> Self {
361 AgentValue::Array(Arc::new(Vec::new()))
362 }
363
364 pub fn object_default() -> Self {
365 AgentValue::Object(Arc::new(AgentValueMap::new()))
366 }
367
368 pub fn from_json(value: serde_json::Value) -> Result<Self, AgentError> {
369 match value {
370 serde_json::Value::Null => Ok(AgentValue::Null),
371 serde_json::Value::Bool(b) => Ok(AgentValue::Boolean(b)),
372 serde_json::Value::Number(n) => {
373 if let Some(i) = n.as_i64() {
374 Ok(AgentValue::Integer(i))
375 } else if let Some(f) = n.as_f64() {
376 Ok(AgentValue::Number(f))
377 } else {
378 Ok(AgentValue::Integer(0))
380 }
381 }
382 serde_json::Value::String(s) => {
383 Ok(AgentValue::String(Arc::new(s)))
391 }
392 serde_json::Value::Array(arr) => {
393 let mut agent_arr = Vec::new();
394 for v in arr {
395 agent_arr.push(AgentValue::from_json(v)?);
396 }
397 Ok(AgentValue::array(agent_arr))
398 }
399 serde_json::Value::Object(obj) => {
400 let mut map = AgentValueMap::new();
401 for (k, v) in obj {
402 map.insert(k, AgentValue::from_json(v)?);
403 }
404 Ok(AgentValue::object(map))
405 }
406 }
407 }
408
409 pub fn from_kind_json(kind: &str, value: serde_json::Value) -> Result<Self, AgentError> {
410 match kind {
411 "unit" => {
412 if let serde_json::Value::Array(a) = value {
413 Ok(AgentValue::Array(Arc::new(
414 a.into_iter().map(|_| AgentValue::Null).collect(),
415 )))
416 } else {
417 Ok(AgentValue::Null)
418 }
419 }
420 "boolean" => match value {
421 serde_json::Value::Bool(b) => Ok(AgentValue::Boolean(b)),
422 serde_json::Value::Array(a) => {
423 let mut agent_arr = Vec::new();
424 for v in a {
425 if let serde_json::Value::Bool(b) = v {
426 agent_arr.push(AgentValue::Boolean(b));
427 } else {
428 return Err(AgentError::InvalidArrayValue("boolean".into()));
429 }
430 }
431 Ok(AgentValue::Array(Arc::new(agent_arr)))
432 }
433 _ => Err(AgentError::InvalidValue("boolean".into())),
434 },
435 "integer" => match value {
436 serde_json::Value::Number(n) => {
437 if let Some(i) = n.as_i64() {
438 Ok(AgentValue::Integer(i))
439 } else if let Some(f) = n.as_f64() {
440 Ok(AgentValue::Integer(f as i64))
441 } else {
442 Err(AgentError::InvalidValue("integer".into()))
443 }
444 }
445 serde_json::Value::Array(a) => {
446 let mut agent_arr = Vec::new();
447 for n in a {
448 if let Some(i) = n.as_i64() {
449 agent_arr.push(AgentValue::Integer(i));
450 } else if let Some(f) = n.as_f64() {
451 agent_arr.push(AgentValue::Integer(f as i64));
452 } else {
453 return Err(AgentError::InvalidArrayValue("integer".into()));
454 }
455 }
456 Ok(AgentValue::Array(Arc::new(agent_arr)))
457 }
458 _ => Err(AgentError::InvalidValue("integer".into())),
459 },
460 "number" => match value {
461 serde_json::Value::Number(n) => {
462 if let Some(f) = n.as_f64() {
463 Ok(AgentValue::Number(f))
464 } else if let Some(i) = n.as_i64() {
465 Ok(AgentValue::Number(i as f64))
466 } else {
467 Err(AgentError::InvalidValue("number".into()))
468 }
469 }
470 serde_json::Value::Array(a) => {
471 let mut agent_arr = Vec::new();
472 for n in a {
473 if let Some(f) = n.as_f64() {
474 agent_arr.push(AgentValue::Number(f));
475 } else if let Some(i) = n.as_i64() {
476 agent_arr.push(AgentValue::Number(i as f64));
477 } else {
478 return Err(AgentError::InvalidArrayValue("number".into()));
479 }
480 }
481 Ok(AgentValue::Array(Arc::new(agent_arr)))
482 }
483 _ => Err(AgentError::InvalidValue("number".into())),
484 },
485 "string" | "text" => match value {
486 serde_json::Value::String(s) => Ok(AgentValue::string(s)),
487 serde_json::Value::Array(a) => {
488 let mut agent_arr = Vec::new();
489 for v in a {
490 if let serde_json::Value::String(s) = v {
491 agent_arr.push(AgentValue::string(s));
492 } else {
493 return Err(AgentError::InvalidArrayValue("string".into()));
494 }
495 }
496 Ok(AgentValue::Array(Arc::new(agent_arr)))
497 }
498 _ => Err(AgentError::InvalidValue("string".into())),
499 },
500 _ => match value {
520 serde_json::Value::Null => Ok(AgentValue::Null),
521 serde_json::Value::Bool(b) => Ok(AgentValue::Boolean(b)),
522 serde_json::Value::Number(n) => {
523 if let Some(i) = n.as_i64() {
524 Ok(AgentValue::Integer(i))
525 } else if let Some(f) = n.as_f64() {
526 Ok(AgentValue::Number(f))
527 } else {
528 Err(AgentError::InvalidValue("number".into()))
529 }
530 }
531 serde_json::Value::String(s) => Ok(AgentValue::string(s)),
532 serde_json::Value::Array(a) => {
533 let mut agent_arr = Vec::new();
534 for v in a {
535 let agent_v = AgentValue::from_kind_json(kind, v)?;
536 agent_arr.push(agent_v);
537 }
538 Ok(AgentValue::Array(Arc::new(agent_arr)))
539 }
540 serde_json::Value::Object(obj) => {
541 let mut map = AgentValueMap::new();
542 for (k, v) in obj {
543 map.insert(k.clone(), AgentValue::from_json(v)?);
544 }
545 Ok(AgentValue::object(map))
546 }
547 },
548 }
549 }
550
551 pub fn to_json(&self) -> serde_json::Value {
552 match self {
553 AgentValue::Null => serde_json::Value::Null,
554 AgentValue::Boolean(b) => (*b).into(),
555 AgentValue::Integer(i) => (*i).into(),
556 AgentValue::Number(n) => (*n).into(),
557 AgentValue::String(s) => s.as_str().into(),
558 AgentValue::Object(o) => {
560 let mut map = serde_json::Map::new();
561 for (k, v) in o.iter() {
562 map.insert(k.clone(), v.to_json());
563 }
564 serde_json::Value::Object(map)
565 }
566 AgentValue::Array(a) => {
567 let arr: Vec<serde_json::Value> = a.iter().map(|v| v.to_json()).collect();
568 serde_json::Value::Array(arr)
569 }
570 }
571 }
572
573 pub fn from_serialize<T: Serialize>(value: &T) -> Result<Self, AgentError> {
575 let json_value = serde_json::to_value(value)
576 .map_err(|e| AgentError::InvalidValue(format!("Failed to serialize: {}", e)))?;
577 Self::from_json(json_value)
578 }
579
580 pub fn to_deserialize<T: for<'de> Deserialize<'de>>(&self) -> Result<T, AgentError> {
582 let json_value = self.to_json();
583 serde_json::from_value(json_value)
584 .map_err(|e| AgentError::InvalidValue(format!("Failed to deserialize: {}", e)))
585 }
586
587 #[allow(unused)]
588 pub fn is_unit(&self) -> bool {
589 matches!(self, AgentValue::Null)
590 }
591
592 #[allow(unused)]
593 pub fn is_boolean(&self) -> bool {
594 matches!(self, AgentValue::Boolean(_))
595 }
596
597 #[allow(unused)]
598 pub fn is_integer(&self) -> bool {
599 matches!(self, AgentValue::Integer(_))
600 }
601
602 #[allow(unused)]
603 pub fn is_number(&self) -> bool {
604 matches!(self, AgentValue::Number(_))
605 }
606
607 #[allow(unused)]
608 pub fn is_string(&self) -> bool {
609 matches!(self, AgentValue::String(_))
610 }
611
612 #[allow(unused)]
618 pub fn is_array(&self) -> bool {
619 matches!(self, AgentValue::Array(_))
620 }
621
622 #[allow(unused)]
623 pub fn is_object(&self) -> bool {
624 matches!(self, AgentValue::Object(_))
625 }
626
627 pub fn as_bool(&self) -> Option<bool> {
628 match self {
629 AgentValue::Boolean(b) => Some(*b),
630 _ => None,
631 }
632 }
633
634 pub fn as_i64(&self) -> Option<i64> {
635 match self {
636 AgentValue::Integer(i) => Some(*i),
637 AgentValue::Number(n) => Some(*n as i64),
638 _ => None,
639 }
640 }
641
642 pub fn as_f64(&self) -> Option<f64> {
643 match self {
644 AgentValue::Integer(i) => Some(*i as f64),
645 AgentValue::Number(n) => Some(*n),
646 _ => None,
647 }
648 }
649
650 pub fn as_str(&self) -> Option<&str> {
651 match self {
652 AgentValue::String(s) => Some(s),
653 _ => None,
654 }
655 }
656
657 pub fn as_object(&self) -> Option<&AgentValueMap<String, AgentValue>> {
665 match self {
666 AgentValue::Object(o) => Some(o),
667 _ => None,
668 }
669 }
670
671 pub fn as_array(&self) -> Option<&Vec<AgentValue>> {
672 match self {
673 AgentValue::Array(a) => Some(a),
674 _ => None,
675 }
676 }
677
678 #[allow(unused)]
679 pub fn get(&self, key: &str) -> Option<&AgentValue> {
680 self.as_object().and_then(|o| o.get(key))
681 }
682
683 #[allow(unused)]
684 pub fn get_bool(&self, key: &str) -> Option<bool> {
685 self.get(key).and_then(|v| v.as_bool())
686 }
687
688 #[allow(unused)]
689 pub fn get_i64(&self, key: &str) -> Option<i64> {
690 self.get(key).and_then(|v| v.as_i64())
691 }
692
693 #[allow(unused)]
694 pub fn get_f64(&self, key: &str) -> Option<f64> {
695 self.get(key).and_then(|v| v.as_f64())
696 }
697
698 #[allow(unused)]
699 pub fn get_str(&self, key: &str) -> Option<&str> {
700 self.get(key).and_then(|v| v.as_str())
701 }
702
703 #[allow(unused)]
709 pub fn get_object(&self, key: &str) -> Option<&AgentValueMap<String, AgentValue>> {
710 self.get(key).and_then(|v| v.as_object())
711 }
712
713 #[allow(unused)]
714 pub fn get_array(&self, key: &str) -> Option<&Vec<AgentValue>> {
715 self.get(key).and_then(|v| v.as_array())
716 }
717
718 pub fn kind(&self) -> String {
719 match self {
720 AgentValue::Null => "unit".to_string(),
721 AgentValue::Boolean(_) => "boolean".to_string(),
722 AgentValue::Integer(_) => "integer".to_string(),
723 AgentValue::Number(_) => "number".to_string(),
724 AgentValue::String(_) => "string".to_string(),
725 AgentValue::Object(_) => "object".to_string(),
727 AgentValue::Array(arr) => {
728 if arr.is_empty() {
729 "array".to_string()
730 } else {
731 arr[0].kind()
732 }
733 }
734 }
735 }
736}
737
738impl Default for AgentValue {
739 fn default() -> Self {
740 AgentValue::Null
741 }
742}
743
744impl PartialEq for AgentValue {
745 fn eq(&self, other: &Self) -> bool {
746 match (self, other) {
747 (AgentValue::Null, AgentValue::Null) => true,
748 (AgentValue::Boolean(b1), AgentValue::Boolean(b2)) => b1 == b2,
749 (AgentValue::Integer(i1), AgentValue::Integer(i2)) => i1 == i2,
750 (AgentValue::Number(n1), AgentValue::Number(n2)) => n1 == n2,
751 (AgentValue::String(s1), AgentValue::String(s2)) => s1 == s2,
752 (AgentValue::Object(o1), AgentValue::Object(o2)) => o1 == o2,
758 (AgentValue::Array(a1), AgentValue::Array(a2)) => a1 == a2,
759 _ => false,
760 }
761 }
762}
763
764impl Serialize for AgentValue {
765 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
766 where
767 S: Serializer,
768 {
769 match self {
770 AgentValue::Null => serializer.serialize_none(),
771 AgentValue::Boolean(b) => serializer.serialize_bool(*b),
772 AgentValue::Integer(i) => serializer.serialize_i64(*i),
773 AgentValue::Number(n) => serializer.serialize_f64(*n),
774 AgentValue::String(s) => serializer.serialize_str(s),
775 AgentValue::Object(o) => {
777 let mut map = serializer.serialize_map(Some(o.len()))?;
778 for (k, v) in o.iter() {
779 map.serialize_entry(k, v)?;
780 }
781 map.end()
782 }
783 AgentValue::Array(a) => {
784 let mut seq = serializer.serialize_seq(Some(a.len()))?;
785 for e in a.iter() {
786 seq.serialize_element(e)?;
787 }
788 seq.end()
789 }
790 }
791 }
792}
793
794impl<'de> Deserialize<'de> for AgentValue {
795 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
796 where
797 D: Deserializer<'de>,
798 {
799 let value = serde_json::Value::deserialize(deserializer)?;
800 AgentValue::from_json(value).map_err(|e| {
801 serde::de::Error::custom(format!("Failed to deserialize AgentValue: {}", e))
802 })
803 }
804}
805
806impl From<()> for AgentValue {
807 fn from(_: ()) -> Self {
808 AgentValue::unit()
809 }
810}
811
812impl From<bool> for AgentValue {
813 fn from(value: bool) -> Self {
814 AgentValue::boolean(value)
815 }
816}
817
818impl From<i32> for AgentValue {
819 fn from(value: i32) -> Self {
820 AgentValue::integer(value as i64)
821 }
822}
823
824impl From<i64> for AgentValue {
825 fn from(value: i64) -> Self {
826 AgentValue::integer(value)
827 }
828}
829
830impl From<f64> for AgentValue {
831 fn from(value: f64) -> Self {
832 AgentValue::number(value)
833 }
834}
835
836impl From<String> for AgentValue {
837 fn from(value: String) -> Self {
838 AgentValue::string(value)
839 }
840}
841
842impl From<&str> for AgentValue {
843 fn from(value: &str) -> Self {
844 AgentValue::string(value)
845 }
846}
847
848#[cfg(test)]
849mod tests {
850 use super::*;
851 use serde_json::json;
852
853 #[test]
854 fn test_agent_data_new_constructors() {
855 let unit_data = AgentData::unit();
857 assert_eq!(unit_data.kind, "unit");
858 assert_eq!(unit_data.value, AgentValue::Null);
859
860 let bool_data = AgentData::boolean(true);
861 assert_eq!(bool_data.kind, "boolean");
862 assert_eq!(bool_data.value, AgentValue::Boolean(true));
863
864 let int_data = AgentData::integer(42);
865 assert_eq!(int_data.kind, "integer");
866 assert_eq!(int_data.value, AgentValue::Integer(42));
867
868 let num_data = AgentData::number(3.14);
869 assert_eq!(num_data.kind, "number");
870 assert!(matches!(num_data.value, AgentValue::Number(_)));
871 if let AgentValue::Number(num) = num_data.value {
872 assert!((num - 3.14).abs() < f64::EPSILON);
873 }
874
875 let str_data = AgentData::string("hello".to_string());
876 assert_eq!(str_data.kind, "string");
877 assert!(matches!(str_data.value, AgentValue::String(_)));
878 assert_eq!(str_data.as_str().unwrap(), "hello");
879
880 let text_data = AgentData::text("multiline\ntext\n\n".to_string());
881 assert_eq!(text_data.kind, "text");
882 assert!(matches!(text_data.value, AgentValue::String(_)));
883 assert_eq!(text_data.as_str().unwrap(), "multiline\ntext\n\n");
884
885 let obj_val = [
896 ("key1".to_string(), AgentValue::string("string1")),
897 ("key2".to_string(), AgentValue::integer(2)),
898 ];
899 let obj_data = AgentData::object(obj_val.clone().into());
900 assert_eq!(obj_data.kind, "object");
901 assert!(matches!(obj_data.value, AgentValue::Object(_)));
902 assert_eq!(obj_data.as_object().unwrap(), &obj_val.into());
903 }
904
905 #[test]
906 fn test_agent_data_from_kind_value() {
907 let unit_data = AgentData::from_json_with_kind("unit", json!(null)).unwrap();
909 assert_eq!(unit_data.kind, "unit");
910 assert_eq!(unit_data.value, AgentValue::Null);
911
912 let bool_data = AgentData::from_json_with_kind("boolean", json!(true)).unwrap();
913 assert_eq!(bool_data.kind, "boolean");
914 assert_eq!(bool_data.value, AgentValue::Boolean(true));
915
916 let int_data = AgentData::from_json_with_kind("integer", json!(42)).unwrap();
917 assert_eq!(int_data.kind, "integer");
918 assert_eq!(int_data.value, AgentValue::Integer(42));
919
920 let int_data = AgentData::from_json_with_kind("integer", json!(3.14)).unwrap();
921 assert_eq!(int_data.kind, "integer");
922 assert_eq!(int_data.value, AgentValue::Integer(3));
923
924 let num_data = AgentData::from_json_with_kind("number", json!(3.14)).unwrap();
925 assert_eq!(num_data.kind, "number");
926 assert_eq!(num_data.value, AgentValue::number(3.14));
927
928 let num_data = AgentData::from_json_with_kind("number", json!(3)).unwrap();
929 assert_eq!(num_data.kind, "number");
930 assert_eq!(num_data.value, AgentValue::number(3.0));
931
932 let str_data = AgentData::from_json_with_kind("string", json!("hello")).unwrap();
933 assert_eq!(str_data.kind, "string");
934 assert_eq!(str_data.value, AgentValue::string("hello"));
935
936 let str_data = AgentData::from_json_with_kind("string", json!("hello\nworld\n\n")).unwrap();
937 assert_eq!(str_data.kind, "string");
938 assert_eq!(str_data.value, AgentValue::string("hello\nworld\n\n"));
939
940 let text_data = AgentData::from_json_with_kind("text", json!("hello")).unwrap();
941 assert_eq!(text_data.kind, "text");
942 assert_eq!(text_data.value, AgentValue::string("hello"));
943
944 let text_data = AgentData::from_json_with_kind("text", json!("hello\nworld\n\n")).unwrap();
945 assert_eq!(text_data.kind, "text");
946 assert_eq!(text_data.value, AgentValue::string("hello\nworld\n\n"));
947
948 let obj_data =
960 AgentData::from_json_with_kind("object", json!({"key1": "string1", "key2": 2}))
961 .unwrap();
962 assert_eq!(obj_data.kind, "object");
963 assert_eq!(
964 obj_data.value,
965 AgentValue::object(
966 [
967 ("key1".to_string(), AgentValue::string("string1")),
968 ("key2".to_string(), AgentValue::integer(2)),
969 ]
970 .into()
971 )
972 );
973
974 let obj_data = AgentData::from_json_with_kind(
976 "custom_type".to_string(),
977 json!({"foo": "hi", "bar": 3}),
978 )
979 .unwrap();
980 assert_eq!(obj_data.kind, "custom_type");
981 assert_eq!(
982 obj_data.value,
983 AgentValue::object(
984 [
985 ("foo".to_string(), AgentValue::string("hi")),
986 ("bar".to_string(), AgentValue::integer(3)),
987 ]
988 .into()
989 )
990 );
991
992 let array_data = AgentData::from_json_with_kind("unit", json!([null, null])).unwrap();
994 assert_eq!(array_data.kind, "unit");
995 assert_eq!(
996 array_data.value,
997 AgentValue::array(vec![AgentValue::unit(), AgentValue::unit(),])
998 );
999
1000 let array_data = AgentData::from_json_with_kind("boolean", json!([true, false])).unwrap();
1001 assert_eq!(array_data.kind, "boolean");
1002 assert_eq!(
1003 array_data.value,
1004 AgentValue::array(vec![AgentValue::boolean(true), AgentValue::boolean(false),])
1005 );
1006
1007 let array_data = AgentData::from_json_with_kind("integer", json!([1, 2.1, 3.0])).unwrap();
1008 assert_eq!(array_data.kind, "integer");
1009 assert_eq!(
1010 array_data.value,
1011 AgentValue::array(vec![
1012 AgentValue::integer(1),
1013 AgentValue::integer(2),
1014 AgentValue::integer(3),
1015 ])
1016 );
1017
1018 let array_data = AgentData::from_json_with_kind("number", json!([1.0, 2.1, 3])).unwrap();
1019 assert_eq!(array_data.kind, "number");
1020 assert_eq!(
1021 array_data.value,
1022 AgentValue::array(vec![
1023 AgentValue::number(1.0),
1024 AgentValue::number(2.1),
1025 AgentValue::number(3.0),
1026 ])
1027 );
1028
1029 let array_data =
1030 AgentData::from_json_with_kind("string", json!(["test", "hello\nworld\n", ""]))
1031 .unwrap();
1032 assert_eq!(array_data.kind, "string");
1033 assert_eq!(
1034 array_data.value,
1035 AgentValue::array(vec![
1036 AgentValue::string("test"),
1037 AgentValue::string("hello\nworld\n"),
1038 AgentValue::string(""),
1039 ])
1040 );
1041
1042 let array_data =
1043 AgentData::from_json_with_kind("text", json!(["test", "hello\nworld\n", ""])).unwrap();
1044 assert_eq!(array_data.kind, "text");
1045 assert_eq!(
1046 array_data.value,
1047 AgentValue::array(vec![
1048 AgentValue::string("test"),
1049 AgentValue::string("hello\nworld\n"),
1050 AgentValue::string(""),
1051 ])
1052 );
1053
1054 let array_data = AgentData::from_json_with_kind(
1055 "object",
1056 json!([{"key1":"test","key2":1}, {"key1":"test2","key2":"hi"}, {}]),
1057 )
1058 .unwrap();
1059 assert_eq!(array_data.kind, "object");
1060 assert_eq!(
1061 array_data.value,
1062 AgentValue::array(vec![
1063 AgentValue::object(
1064 [
1065 ("key1".to_string(), AgentValue::string("test")),
1066 ("key2".to_string(), AgentValue::integer(1)),
1067 ]
1068 .into()
1069 ),
1070 AgentValue::object(
1071 [
1072 ("key1".to_string(), AgentValue::string("test2")),
1073 ("key2".to_string(), AgentValue::string("hi")),
1074 ]
1075 .into()
1076 ),
1077 AgentValue::object(AgentValueMap::default()),
1078 ])
1079 );
1080
1081 let array_data = AgentData::from_json_with_kind(
1082 "custom",
1083 json!([{"key1":"test","key2":1}, {"key1":"test2","key2":"hi"}, {}]),
1084 )
1085 .unwrap();
1086 assert_eq!(array_data.kind, "custom");
1087 assert_eq!(
1088 array_data.value,
1089 AgentValue::array(vec![
1090 AgentValue::object(
1091 [
1092 ("key1".to_string(), AgentValue::string("test")),
1093 ("key2".to_string(), AgentValue::integer(1)),
1094 ]
1095 .into()
1096 ),
1097 AgentValue::object(
1098 [
1099 ("key1".to_string(), AgentValue::string("test2")),
1100 ("key2".to_string(), AgentValue::string("hi")),
1101 ]
1102 .into()
1103 ),
1104 AgentValue::object(AgentValueMap::default()),
1105 ])
1106 );
1107 }
1108
1109 #[test]
1110 fn test_agent_data_from_json_value() {
1111 let unit_data = AgentData::from_json(json!(null)).unwrap();
1113 assert_eq!(unit_data.kind, "unit");
1114 assert_eq!(unit_data.value, AgentValue::Null);
1115
1116 let bool_data = AgentData::from_json(json!(true)).unwrap();
1117 assert_eq!(bool_data.kind, "boolean");
1118 assert_eq!(bool_data.value, AgentValue::Boolean(true));
1119
1120 let int_data = AgentData::from_json(json!(42)).unwrap();
1121 assert_eq!(int_data.kind, "integer");
1122 assert_eq!(int_data.value, AgentValue::Integer(42));
1123
1124 let num_data = AgentData::from_json(json!(3.14)).unwrap();
1125 assert_eq!(num_data.kind, "number");
1126 assert_eq!(num_data.value, AgentValue::number(3.14));
1127
1128 let str_data = AgentData::from_json(json!("hello")).unwrap();
1129 assert_eq!(str_data.kind, "string");
1130 assert_eq!(str_data.value, AgentValue::string("hello"));
1131
1132 let str_data = AgentData::from_json(json!("hello\nworld\n\n")).unwrap();
1133 assert_eq!(str_data.kind, "string");
1134 assert_eq!(str_data.value, AgentValue::string("hello\nworld\n\n"));
1135
1136 let obj_data = AgentData::from_json(json!({"key1": "string1", "key2": 2})).unwrap();
1150 assert_eq!(obj_data.kind, "object");
1151 assert_eq!(
1152 obj_data.value,
1153 AgentValue::object(
1154 [
1155 ("key1".to_string(), AgentValue::string("string1")),
1156 ("key2".to_string(), AgentValue::integer(2)),
1157 ]
1158 .into()
1159 )
1160 );
1161
1162 let array_data = AgentData::from_json(json!([null, null])).unwrap();
1164 assert_eq!(array_data.kind, "unit");
1165 assert_eq!(
1166 array_data.value,
1167 AgentValue::array(vec![AgentValue::unit(), AgentValue::unit(),])
1168 );
1169
1170 let array_data = AgentData::from_json(json!([true, false])).unwrap();
1171 assert_eq!(array_data.kind, "boolean");
1172 assert_eq!(
1173 array_data.value,
1174 AgentValue::array(vec![AgentValue::boolean(true), AgentValue::boolean(false),])
1175 );
1176
1177 let array_data = AgentData::from_json(json!([1, 2, 3])).unwrap();
1178 assert_eq!(array_data.kind, "integer");
1179 assert_eq!(
1180 array_data.value,
1181 AgentValue::array(vec![
1182 AgentValue::integer(1),
1183 AgentValue::integer(2),
1184 AgentValue::integer(3),
1185 ])
1186 );
1187
1188 let array_data = AgentData::from_json(json!([1.0, 2.1, 3.2])).unwrap();
1189 assert_eq!(array_data.kind, "number");
1190 assert_eq!(
1191 array_data.value,
1192 AgentValue::array(vec![
1193 AgentValue::number(1.0),
1194 AgentValue::number(2.1),
1195 AgentValue::number(3.2),
1196 ])
1197 );
1198
1199 let array_data = AgentData::from_json(json!(["test", "hello\nworld\n", ""])).unwrap();
1200 assert_eq!(array_data.kind, "string");
1201 assert_eq!(
1202 array_data.value,
1203 AgentValue::array(vec![
1204 AgentValue::string("test"),
1205 AgentValue::string("hello\nworld\n"),
1206 AgentValue::string(""),
1207 ])
1208 );
1209
1210 let array_data = AgentData::from_json(
1211 json!([{"key1":"test","key2":1}, {"key1":"test2","key2":"hi"}, {}]),
1212 )
1213 .unwrap();
1214 assert_eq!(array_data.kind, "object");
1215 assert_eq!(
1216 array_data.value,
1217 AgentValue::array(vec![
1218 AgentValue::object(
1219 [
1220 ("key1".to_string(), AgentValue::string("test")),
1221 ("key2".to_string(), AgentValue::integer(1)),
1222 ]
1223 .into()
1224 ),
1225 AgentValue::object(
1226 [
1227 ("key1".to_string(), AgentValue::string("test2")),
1228 ("key2".to_string(), AgentValue::string("hi")),
1229 ]
1230 .into()
1231 ),
1232 AgentValue::object(AgentValueMap::default()),
1233 ])
1234 );
1235 }
1236
1237 #[test]
1238 fn test_agent_data_accessor_methods() {
1239 let str_data = AgentData::string("hello".to_string());
1241 assert_eq!(str_data.as_str().unwrap(), "hello");
1242 assert!(str_data.as_object().is_none());
1243
1244 let obj_val = [
1245 ("key1".to_string(), AgentValue::string("string1")),
1246 ("key2".to_string(), AgentValue::integer(2)),
1247 ];
1248 let obj_data = AgentData::object(obj_val.clone().into());
1249 assert!(obj_data.as_str().is_none());
1250 assert_eq!(obj_data.as_object().unwrap(), &obj_val.into());
1251 }
1252
1253 #[test]
1254 fn test_agent_data_serialization() {
1255 {
1257 let data = AgentData::unit();
1258 assert_eq!(
1259 serde_json::to_string(&data).unwrap(),
1260 r#"{"kind":"unit","value":null}"#
1261 );
1262 }
1263
1264 {
1266 let data = AgentData::boolean(true);
1267 assert_eq!(
1268 serde_json::to_string(&data).unwrap(),
1269 r#"{"kind":"boolean","value":true}"#
1270 );
1271
1272 let data = AgentData::boolean(false);
1273 assert_eq!(
1274 serde_json::to_string(&data).unwrap(),
1275 r#"{"kind":"boolean","value":false}"#
1276 );
1277 }
1278
1279 {
1281 let data = AgentData::integer(42);
1282 assert_eq!(
1283 serde_json::to_string(&data).unwrap(),
1284 r#"{"kind":"integer","value":42}"#
1285 );
1286 }
1287
1288 {
1290 let data = AgentData::number(3.14);
1291 assert_eq!(
1292 serde_json::to_string(&data).unwrap(),
1293 r#"{"kind":"number","value":3.14}"#
1294 );
1295
1296 let data = AgentData::number(3.0);
1297 assert_eq!(
1298 serde_json::to_string(&data).unwrap(),
1299 r#"{"kind":"number","value":3.0}"#
1300 );
1301 }
1302
1303 {
1305 let data = AgentData::string("Hello, world!");
1306 assert_eq!(
1307 serde_json::to_string(&data).unwrap(),
1308 r#"{"kind":"string","value":"Hello, world!"}"#
1309 );
1310
1311 let data = AgentData::string("hello\nworld\n\n");
1312 assert_eq!(
1313 serde_json::to_string(&data).unwrap(),
1314 r#"{"kind":"string","value":"hello\nworld\n\n"}"#
1315 );
1316 }
1317
1318 {
1320 let data = AgentData::text("Hello, world!");
1321 assert_eq!(
1322 serde_json::to_string(&data).unwrap(),
1323 r#"{"kind":"text","value":"Hello, world!"}"#
1324 );
1325
1326 let data = AgentData::text("hello\nworld\n\n");
1327 assert_eq!(
1328 serde_json::to_string(&data).unwrap(),
1329 r#"{"kind":"text","value":"hello\nworld\n\n"}"#
1330 );
1331 }
1332
1333 {
1344 let data = AgentData::object(
1345 [
1346 ("key1".to_string(), AgentValue::string("string1")),
1347 ("key2".to_string(), AgentValue::integer(2)),
1348 ]
1349 .into(),
1350 );
1351 assert_eq!(
1352 serde_json::to_string(&data).unwrap(),
1353 r#"{"kind":"object","value":{"key1":"string1","key2":2}}"#
1354 );
1355 }
1356
1357 {
1359 let data = AgentData::object_with_kind(
1360 "custom",
1361 [
1362 ("key1".to_string(), AgentValue::string("test")),
1363 ("key2".to_string(), AgentValue::integer(3)),
1364 ]
1365 .into(),
1366 );
1367 assert_eq!(
1368 serde_json::to_string(&data).unwrap(),
1369 r#"{"kind":"custom","value":{"key1":"test","key2":3}}"#
1370 );
1371 }
1372
1373 {
1375 let data = AgentData::array("unit", vec![AgentValue::unit(), AgentValue::unit()]);
1376 assert_eq!(
1377 serde_json::to_string(&data).unwrap(),
1378 r#"{"kind":"unit","value":[null,null]}"#
1379 );
1380
1381 let data = AgentData::array(
1382 "boolean",
1383 vec![AgentValue::boolean(false), AgentValue::boolean(true)],
1384 );
1385 assert_eq!(
1386 serde_json::to_string(&data).unwrap(),
1387 r#"{"kind":"boolean","value":[false,true]}"#
1388 );
1389
1390 let data = AgentData::array(
1391 "integer",
1392 vec![
1393 AgentValue::integer(1),
1394 AgentValue::integer(2),
1395 AgentValue::integer(3),
1396 ],
1397 );
1398 assert_eq!(
1399 serde_json::to_string(&data).unwrap(),
1400 r#"{"kind":"integer","value":[1,2,3]}"#
1401 );
1402
1403 let data = AgentData::array(
1404 "number",
1405 vec![
1406 AgentValue::number(1.0),
1407 AgentValue::number(2.1),
1408 AgentValue::number(3.2),
1409 ],
1410 );
1411 assert_eq!(
1412 serde_json::to_string(&data).unwrap(),
1413 r#"{"kind":"number","value":[1.0,2.1,3.2]}"#
1414 );
1415
1416 let data = AgentData::array(
1417 "string",
1418 vec![
1419 AgentValue::string("test"),
1420 AgentValue::string("hello\nworld\n"),
1421 AgentValue::string(""),
1422 ],
1423 );
1424 assert_eq!(
1425 serde_json::to_string(&data).unwrap(),
1426 r#"{"kind":"string","value":["test","hello\nworld\n",""]}"#
1427 );
1428
1429 let data = AgentData::array(
1430 "text",
1431 vec![
1432 AgentValue::string("test"),
1433 AgentValue::string("hello\nworld\n"),
1434 AgentValue::string(""),
1435 ],
1436 );
1437 assert_eq!(
1438 serde_json::to_string(&data).unwrap(),
1439 r#"{"kind":"text","value":["test","hello\nworld\n",""]}"#
1440 );
1441
1442 let data = AgentData::array(
1443 "object",
1444 vec![
1445 AgentValue::object(
1446 [
1447 ("key1".to_string(), AgentValue::string("test")),
1448 ("key2".to_string(), AgentValue::integer(1)),
1449 ]
1450 .into(),
1451 ),
1452 AgentValue::object(
1453 [
1454 ("key1".to_string(), AgentValue::string("test2")),
1455 ("key2".to_string(), AgentValue::string("hi")),
1456 ]
1457 .into(),
1458 ),
1459 AgentValue::object(AgentValueMap::default()),
1460 ],
1461 );
1462 assert_eq!(
1463 serde_json::to_string(&data).unwrap(),
1464 r#"{"kind":"object","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#
1465 );
1466
1467 let data = AgentData::array(
1468 "custom",
1469 vec![
1470 AgentValue::object(
1471 [
1472 ("key1".to_string(), AgentValue::string("test")),
1473 ("key2".to_string(), AgentValue::integer(1)),
1474 ]
1475 .into(),
1476 ),
1477 AgentValue::object(
1478 [
1479 ("key1".to_string(), AgentValue::string("test2")),
1480 ("key2".to_string(), AgentValue::string("hi")),
1481 ]
1482 .into(),
1483 ),
1484 AgentValue::object(AgentValueMap::default()),
1485 ],
1486 );
1487 assert_eq!(
1488 serde_json::to_string(&data).unwrap(),
1489 r#"{"kind":"custom","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#
1490 );
1491 }
1492 }
1493
1494 #[test]
1495 fn test_agent_data_deserialization() {
1496 {
1498 let deserialized: AgentData =
1499 serde_json::from_str(r#"{"kind":"unit","value":null}"#).unwrap();
1500 assert_eq!(deserialized, AgentData::unit());
1501 }
1502
1503 {
1505 let deserialized: AgentData =
1506 serde_json::from_str(r#"{"kind":"boolean","value":false}"#).unwrap();
1507 assert_eq!(deserialized, AgentData::boolean(false));
1508
1509 let deserialized: AgentData =
1510 serde_json::from_str(r#"{"kind":"boolean","value":true}"#).unwrap();
1511 assert_eq!(deserialized, AgentData::boolean(true));
1512 }
1513
1514 {
1516 let deserialized: AgentData =
1517 serde_json::from_str(r#"{"kind":"integer","value":123}"#).unwrap();
1518 assert_eq!(deserialized, AgentData::integer(123));
1519 }
1520
1521 {
1523 let deserialized: AgentData =
1524 serde_json::from_str(r#"{"kind":"number","value":3.14}"#).unwrap();
1525 assert_eq!(deserialized, AgentData::number(3.14));
1526
1527 let deserialized: AgentData =
1528 serde_json::from_str(r#"{"kind":"number","value":3.0}"#).unwrap();
1529 assert_eq!(deserialized, AgentData::number(3.0));
1530 }
1531
1532 {
1534 let deserialized: AgentData =
1535 serde_json::from_str(r#"{"kind":"string","value":"Hello, world!"}"#).unwrap();
1536 assert_eq!(deserialized, AgentData::string("Hello, world!"));
1537
1538 let deserialized: AgentData =
1539 serde_json::from_str(r#"{"kind":"string","value":"hello\nworld\n\n"}"#).unwrap();
1540 assert_eq!(deserialized, AgentData::string("hello\nworld\n\n"));
1541 }
1542
1543 {
1555 let deserialized: AgentData =
1556 serde_json::from_str(r#"{"kind":"object","value":{"key1":"test","key2":3}}"#)
1557 .unwrap();
1558 assert_eq!(
1559 deserialized,
1560 AgentData::object(
1561 [
1562 ("key1".to_string(), AgentValue::string("test")),
1563 ("key2".to_string(), AgentValue::integer(3))
1564 ]
1565 .into()
1566 )
1567 );
1568 }
1569
1570 {
1572 let deserialized: AgentData =
1573 serde_json::from_str(r#"{"kind":"custom","value":{"name":"test","value":3}}"#)
1574 .unwrap();
1575 assert_eq!(
1576 deserialized,
1577 AgentData::object_with_kind(
1578 "custom",
1579 [
1580 ("name".to_string(), AgentValue::string("test")),
1581 ("value".to_string(), AgentValue::integer(3))
1582 ]
1583 .into()
1584 )
1585 );
1586 }
1587
1588 {
1590 let deserialized: AgentData =
1591 serde_json::from_str(r#"{"kind":"unit","value":[null,null]}"#).unwrap();
1592 assert_eq!(
1593 deserialized,
1594 AgentData::array("unit", vec![AgentValue::unit(), AgentValue::unit(),])
1595 );
1596
1597 let deserialized: AgentData =
1598 serde_json::from_str(r#"{"kind":"boolean","value":[true,false]}"#).unwrap();
1599 assert_eq!(
1600 deserialized,
1601 AgentData::array(
1602 "boolean",
1603 vec![AgentValue::boolean(true), AgentValue::boolean(false),]
1604 )
1605 );
1606
1607 let deserialized: AgentData =
1608 serde_json::from_str(r#"{"kind":"integer","value":[1,2,3]}"#).unwrap();
1609 assert_eq!(
1610 deserialized,
1611 AgentData::array(
1612 "integer",
1613 vec![
1614 AgentValue::integer(1),
1615 AgentValue::integer(2),
1616 AgentValue::integer(3),
1617 ]
1618 )
1619 );
1620
1621 let deserialized: AgentData =
1622 serde_json::from_str(r#"{"kind":"number","value":[1.0,2.1,3]}"#).unwrap();
1623 assert_eq!(
1624 deserialized,
1625 AgentData::array(
1626 "number",
1627 vec![
1628 AgentValue::number(1.0),
1629 AgentValue::number(2.1),
1630 AgentValue::number(3.0),
1631 ]
1632 )
1633 );
1634
1635 let deserialized: AgentData =
1636 serde_json::from_str(r#"{"kind":"string","value":["test","hello\nworld\n",""]}"#)
1637 .unwrap();
1638 assert_eq!(
1639 deserialized,
1640 AgentData::array(
1641 "string",
1642 vec![
1643 AgentValue::string("test"),
1644 AgentValue::string("hello\nworld\n"),
1645 AgentValue::string(""),
1646 ]
1647 )
1648 );
1649
1650 let deserialized: AgentData =
1651 serde_json::from_str(r#"{"kind":"text","value":["test","hello\nworld\n",""]}"#)
1652 .unwrap();
1653 assert_eq!(
1654 deserialized,
1655 AgentData::array(
1656 "text",
1657 vec![
1658 AgentValue::string("test"),
1659 AgentValue::string("hello\nworld\n"),
1660 AgentValue::string(""),
1661 ]
1662 )
1663 );
1664
1665 let deserialized: AgentData =
1666 serde_json::from_str(r#"{"kind":"object","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#)
1667 .unwrap();
1668 assert_eq!(
1669 deserialized,
1670 AgentData::array(
1671 "object",
1672 vec![
1673 AgentValue::object(
1674 [
1675 ("key1".to_string(), AgentValue::string("test")),
1676 ("key2".to_string(), AgentValue::integer(1)),
1677 ]
1678 .into()
1679 ),
1680 AgentValue::object(
1681 [
1682 ("key1".to_string(), AgentValue::string("test2")),
1683 ("key2".to_string(), AgentValue::string("hi")),
1684 ]
1685 .into()
1686 ),
1687 AgentValue::object(AgentValueMap::default()),
1688 ]
1689 )
1690 );
1691
1692 let deserialized: AgentData =
1693 serde_json::from_str(r#"{"kind":"custom","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#)
1694 .unwrap();
1695 assert_eq!(
1696 deserialized,
1697 AgentData::array(
1698 "custom",
1699 vec![
1700 AgentValue::object(
1701 [
1702 ("key1".to_string(), AgentValue::string("test")),
1703 ("key2".to_string(), AgentValue::integer(1)),
1704 ]
1705 .into()
1706 ),
1707 AgentValue::object(
1708 [
1709 ("key1".to_string(), AgentValue::string("test2")),
1710 ("key2".to_string(), AgentValue::string("hi")),
1711 ]
1712 .into()
1713 ),
1714 AgentValue::object(AgentValueMap::default()),
1715 ]
1716 )
1717 );
1718 }
1719 }
1720
1721 #[test]
1722 fn test_agent_value_constructors() {
1723 let unit = AgentValue::unit();
1725 assert_eq!(unit, AgentValue::Null);
1726
1727 let boolean = AgentValue::boolean(true);
1728 assert_eq!(boolean, AgentValue::Boolean(true));
1729
1730 let integer = AgentValue::integer(42);
1731 assert_eq!(integer, AgentValue::Integer(42));
1732
1733 let number = AgentValue::number(3.14);
1734 assert!(matches!(number, AgentValue::Number(_)));
1735 if let AgentValue::Number(num) = number {
1736 assert!((num - 3.14).abs() < f64::EPSILON);
1737 }
1738
1739 let string = AgentValue::string("hello");
1740 assert!(matches!(string, AgentValue::String(_)));
1741 assert_eq!(string.as_str().unwrap(), "hello");
1742
1743 let text = AgentValue::string("multiline\ntext");
1744 assert!(matches!(text, AgentValue::String(_)));
1745 assert_eq!(text.as_str().unwrap(), "multiline\ntext");
1746
1747 let array = AgentValue::array(vec![AgentValue::integer(1), AgentValue::integer(2)]);
1748 assert!(matches!(array, AgentValue::Array(_)));
1749 if let AgentValue::Array(arr) = array {
1750 assert_eq!(arr.len(), 2);
1751 assert_eq!(arr[0].as_i64().unwrap(), 1);
1752 assert_eq!(arr[1].as_i64().unwrap(), 2);
1753 }
1754
1755 let obj = AgentValue::object(
1756 [
1757 ("key1".to_string(), AgentValue::string("string1")),
1758 ("key2".to_string(), AgentValue::integer(2)),
1759 ]
1760 .into(),
1761 );
1762 assert!(matches!(obj, AgentValue::Object(_)));
1763 if let AgentValue::Object(obj) = obj {
1764 assert_eq!(obj.get("key1").and_then(|v| v.as_str()), Some("string1"));
1765 assert_eq!(obj.get("key2").and_then(|v| v.as_i64()), Some(2));
1766 } else {
1767 panic!("Object was not deserialized correctly");
1768 }
1769 }
1770
1771 #[test]
1772 fn test_agent_value_from_json_value() {
1773 let null = AgentValue::from_json(json!(null)).unwrap();
1775 assert_eq!(null, AgentValue::Null);
1776
1777 let boolean = AgentValue::from_json(json!(true)).unwrap();
1778 assert_eq!(boolean, AgentValue::Boolean(true));
1779
1780 let integer = AgentValue::from_json(json!(42)).unwrap();
1781 assert_eq!(integer, AgentValue::Integer(42));
1782
1783 let number = AgentValue::from_json(json!(3.14)).unwrap();
1784 assert!(matches!(number, AgentValue::Number(_)));
1785 if let AgentValue::Number(num) = number {
1786 assert!((num - 3.14).abs() < f64::EPSILON);
1787 }
1788
1789 let string = AgentValue::from_json(json!("hello")).unwrap();
1790 assert!(matches!(string, AgentValue::String(_)));
1791 if let AgentValue::String(s) = string {
1792 assert_eq!(*s, "hello");
1793 } else {
1794 panic!("Expected string value");
1795 }
1796
1797 let array = AgentValue::from_json(json!([1, "test", true])).unwrap();
1798 assert!(matches!(array, AgentValue::Array(_)));
1799 if let AgentValue::Array(arr) = array {
1800 assert_eq!(arr.len(), 3);
1801 assert_eq!(arr[0], AgentValue::Integer(1));
1802 assert!(matches!(&arr[1], AgentValue::String(_)));
1803 if let AgentValue::String(s) = &arr[1] {
1804 assert_eq!(**s, "test");
1805 } else {
1806 panic!("Expected string value");
1807 }
1808 assert_eq!(arr[2], AgentValue::Boolean(true));
1809 }
1810
1811 let object = AgentValue::from_json(json!({"key1": "string1", "key2": 2})).unwrap();
1812 assert!(matches!(object, AgentValue::Object(_)));
1813 if let AgentValue::Object(obj) = object {
1814 assert_eq!(obj.get("key1").and_then(|v| v.as_str()), Some("string1"));
1815 assert_eq!(obj.get("key2").and_then(|v| v.as_i64()), Some(2));
1816 } else {
1817 panic!("Object was not deserialized correctly");
1818 }
1819 }
1820
1821 #[test]
1822 fn test_agent_value_from_kind_value() {
1823 let unit = AgentValue::from_kind_json("unit", json!(null)).unwrap();
1825 assert_eq!(unit, AgentValue::Null);
1826
1827 let boolean = AgentValue::from_kind_json("boolean", json!(true)).unwrap();
1828 assert_eq!(boolean, AgentValue::Boolean(true));
1829
1830 let integer = AgentValue::from_kind_json("integer", json!(42)).unwrap();
1831 assert_eq!(integer, AgentValue::Integer(42));
1832
1833 let integer = AgentValue::from_kind_json("integer", json!(42.0)).unwrap();
1834 assert_eq!(integer, AgentValue::Integer(42));
1835
1836 let number = AgentValue::from_kind_json("number", json!(3.14)).unwrap();
1837 assert!(matches!(number, AgentValue::Number(_)));
1838 if let AgentValue::Number(num) = number {
1839 assert!((num - 3.14).abs() < f64::EPSILON);
1840 }
1841
1842 let number = AgentValue::from_kind_json("number", json!(3)).unwrap();
1843 assert!(matches!(number, AgentValue::Number(_)));
1844 if let AgentValue::Number(num) = number {
1845 assert!((num - 3.0).abs() < f64::EPSILON);
1846 }
1847
1848 let string = AgentValue::from_kind_json("string", json!("hello")).unwrap();
1849 assert!(matches!(string, AgentValue::String(_)));
1850 if let AgentValue::String(s) = string {
1851 assert_eq!(*s, "hello");
1852 } else {
1853 panic!("Expected string value");
1854 }
1855
1856 let text = AgentValue::from_kind_json("text", json!("multiline\ntext")).unwrap();
1857 assert!(matches!(text, AgentValue::String(_)));
1858 if let AgentValue::String(t) = text {
1859 assert_eq!(*t, "multiline\ntext");
1860 } else {
1861 panic!("Expected text value");
1862 }
1863
1864 let array = AgentValue::from_kind_json("array", json!([1, "test", true])).unwrap();
1865 assert!(matches!(array, AgentValue::Array(_)));
1866 if let AgentValue::Array(arr) = array {
1867 assert_eq!(arr.len(), 3);
1868 assert_eq!(arr[0], AgentValue::Integer(1));
1869 assert!(matches!(&arr[1], AgentValue::String(_)));
1870 if let AgentValue::String(s) = &arr[1] {
1871 assert_eq!(**s, "test");
1872 } else {
1873 panic!("Expected string value");
1874 }
1875 assert_eq!(arr[2], AgentValue::Boolean(true));
1876 }
1877
1878 let obj = AgentValue::from_kind_json("object", json!({"key1": "test", "key2": 2})).unwrap();
1879 assert!(matches!(obj, AgentValue::Object(_)));
1880 if let AgentValue::Object(obj) = obj {
1881 assert_eq!(obj.get("key1").and_then(|v| v.as_str()), Some("test"));
1882 assert_eq!(obj.get("key2").and_then(|v| v.as_i64()), Some(2));
1883 } else {
1884 panic!("Object was not deserialized correctly");
1885 }
1886
1887 let unit_array = AgentValue::from_kind_json("unit", json!([null, null])).unwrap();
1889 assert!(matches!(unit_array, AgentValue::Array(_)));
1890 if let AgentValue::Array(arr) = unit_array {
1891 assert_eq!(arr.len(), 2);
1892 for val in arr.iter() {
1893 assert_eq!(*val, AgentValue::Null);
1894 }
1895 }
1896
1897 let bool_array = AgentValue::from_kind_json("boolean", json!([true, false])).unwrap();
1898 assert!(matches!(bool_array, AgentValue::Array(_)));
1899 if let AgentValue::Array(arr) = bool_array {
1900 assert_eq!(arr.len(), 2);
1901 assert_eq!(arr[0], AgentValue::Boolean(true));
1902 assert_eq!(arr[1], AgentValue::Boolean(false));
1903 }
1904
1905 let int_array = AgentValue::from_kind_json("integer", json!([1, 2, 3])).unwrap();
1906 assert!(matches!(int_array, AgentValue::Array(_)));
1907 if let AgentValue::Array(arr) = int_array {
1908 assert_eq!(arr.len(), 3);
1909 assert_eq!(arr[0], AgentValue::Integer(1));
1910 assert_eq!(arr[1], AgentValue::Integer(2));
1911 assert_eq!(arr[2], AgentValue::Integer(3));
1912 }
1913
1914 let num_array = AgentValue::from_kind_json("number", json!([1.1, 2.2, 3.3])).unwrap();
1915 assert!(matches!(num_array, AgentValue::Array(_)));
1916 if let AgentValue::Array(arr) = num_array {
1917 assert_eq!(arr.len(), 3);
1918 assert_eq!(arr[0], AgentValue::Number(1.1));
1919 assert_eq!(arr[1], AgentValue::Number(2.2));
1920 assert_eq!(arr[2], AgentValue::Number(3.3));
1921 }
1922
1923 let string_array = AgentValue::from_kind_json("string", json!(["hello", "world"])).unwrap();
1924 assert!(matches!(string_array, AgentValue::Array(_)));
1925 if let AgentValue::Array(arr) = string_array {
1926 assert_eq!(arr.len(), 2);
1927 assert!(matches!(&arr[0], AgentValue::String(_)));
1928 if let AgentValue::String(s) = &arr[0] {
1929 assert_eq!(**s, "hello".to_string());
1930 }
1931 assert!(matches!(&arr[1], AgentValue::String(_)));
1932 if let AgentValue::String(s) = &arr[1] {
1933 assert_eq!(**s, "world".to_string());
1934 }
1935 }
1936
1937 let text_array = AgentValue::from_kind_json("text", json!(["hello", "world!\n"])).unwrap();
1938 assert!(matches!(text_array, AgentValue::Array(_)));
1939 if let AgentValue::Array(arr) = text_array {
1940 assert_eq!(arr.len(), 2);
1941 assert!(matches!(&arr[0], AgentValue::String(_)));
1942 if let AgentValue::String(s) = &arr[0] {
1943 assert_eq!(**s, "hello".to_string());
1944 }
1945 assert!(matches!(&arr[1], AgentValue::String(_)));
1946 if let AgentValue::String(s) = &arr[1] {
1947 assert_eq!(**s, "world!\n".to_string());
1948 }
1949 }
1950
1951 }
1955
1956 #[test]
1957 fn test_agent_value_test_methods() {
1958 let unit = AgentValue::unit();
1960 assert_eq!(unit.is_unit(), true);
1961 assert_eq!(unit.is_boolean(), false);
1962 assert_eq!(unit.is_integer(), false);
1963 assert_eq!(unit.is_number(), false);
1964 assert_eq!(unit.is_string(), false);
1965 assert_eq!(unit.is_array(), false);
1966 assert_eq!(unit.is_object(), false);
1967
1968 let boolean = AgentValue::boolean(true);
1969 assert_eq!(boolean.is_unit(), false);
1970 assert_eq!(boolean.is_boolean(), true);
1971 assert_eq!(boolean.is_integer(), false);
1972 assert_eq!(boolean.is_number(), false);
1973 assert_eq!(boolean.is_string(), false);
1974 assert_eq!(boolean.is_array(), false);
1975 assert_eq!(boolean.is_object(), false);
1976
1977 let integer = AgentValue::integer(42);
1978 assert_eq!(integer.is_unit(), false);
1979 assert_eq!(integer.is_boolean(), false);
1980 assert_eq!(integer.is_integer(), true);
1981 assert_eq!(integer.is_number(), false);
1982 assert_eq!(integer.is_string(), false);
1983 assert_eq!(integer.is_array(), false);
1984 assert_eq!(integer.is_object(), false);
1985
1986 let number = AgentValue::number(3.14);
1987 assert_eq!(number.is_unit(), false);
1988 assert_eq!(number.is_boolean(), false);
1989 assert_eq!(number.is_integer(), false);
1990 assert_eq!(number.is_number(), true);
1991 assert_eq!(number.is_string(), false);
1992 assert_eq!(number.is_array(), false);
1993 assert_eq!(number.is_object(), false);
1994
1995 let string = AgentValue::string("hello");
1996 assert_eq!(string.is_unit(), false);
1997 assert_eq!(string.is_boolean(), false);
1998 assert_eq!(string.is_integer(), false);
1999 assert_eq!(string.is_number(), false);
2000 assert_eq!(string.is_string(), true);
2001 assert_eq!(string.is_array(), false);
2002 assert_eq!(string.is_object(), false);
2003
2004 let array = AgentValue::array(vec![AgentValue::integer(1), AgentValue::integer(2)]);
2005 assert_eq!(array.is_unit(), false);
2006 assert_eq!(array.is_boolean(), false);
2007 assert_eq!(array.is_integer(), false);
2008 assert_eq!(array.is_number(), false);
2009 assert_eq!(array.is_string(), false);
2010 assert_eq!(array.is_array(), true);
2011 assert_eq!(array.is_object(), false);
2012
2013 let obj = AgentValue::object(
2014 [
2015 ("key1".to_string(), AgentValue::string("string1")),
2016 ("key2".to_string(), AgentValue::integer(2)),
2017 ]
2018 .into(),
2019 );
2020 assert_eq!(obj.is_unit(), false);
2021 assert_eq!(obj.is_boolean(), false);
2022 assert_eq!(obj.is_integer(), false);
2023 assert_eq!(obj.is_number(), false);
2024 assert_eq!(obj.is_string(), false);
2025 assert_eq!(obj.is_array(), false);
2026 assert_eq!(obj.is_object(), true);
2027 }
2028
2029 #[test]
2030 fn test_agent_value_accessor_methods() {
2031 let boolean = AgentValue::boolean(true);
2033 assert_eq!(boolean.as_bool(), Some(true));
2034 assert_eq!(boolean.as_i64(), None);
2035 assert_eq!(boolean.as_f64(), None);
2036 assert_eq!(boolean.as_str(), None);
2037 assert!(boolean.as_array().is_none());
2038 assert_eq!(boolean.as_object(), None);
2039
2040 let integer = AgentValue::integer(42);
2041 assert_eq!(integer.as_bool(), None);
2042 assert_eq!(integer.as_i64(), Some(42));
2043 assert_eq!(integer.as_f64(), Some(42.0));
2044 assert_eq!(integer.as_str(), None);
2045 assert!(integer.as_array().is_none());
2046 assert_eq!(integer.as_object(), None);
2047
2048 let number = AgentValue::number(3.14);
2049 assert_eq!(number.as_bool(), None);
2050 assert_eq!(number.as_i64(), Some(3)); assert_eq!(number.as_f64().unwrap(), 3.14);
2052 assert_eq!(number.as_str(), None);
2053 assert!(number.as_array().is_none());
2054 assert_eq!(number.as_object(), None);
2055
2056 let string = AgentValue::string("hello");
2057 assert_eq!(string.as_bool(), None);
2058 assert_eq!(string.as_i64(), None);
2059 assert_eq!(string.as_f64(), None);
2060 assert_eq!(string.as_str(), Some("hello"));
2061 assert!(string.as_array().is_none());
2062 assert_eq!(string.as_object(), None);
2063
2064 let array = AgentValue::array(vec![AgentValue::integer(1), AgentValue::integer(2)]);
2065 assert_eq!(array.as_bool(), None);
2066 assert_eq!(array.as_i64(), None);
2067 assert_eq!(array.as_f64(), None);
2068 assert_eq!(array.as_str(), None);
2069 assert!(array.as_array().is_some());
2070 if let Some(arr) = array.as_array() {
2071 assert_eq!(arr.len(), 2);
2072 assert_eq!(arr[0].as_i64().unwrap(), 1);
2073 assert_eq!(arr[1].as_i64().unwrap(), 2);
2074 }
2075 assert_eq!(array.as_object(), None);
2076
2077 let obj = AgentValue::object(
2078 [
2079 ("key1".to_string(), AgentValue::string("string1")),
2080 ("key2".to_string(), AgentValue::integer(2)),
2081 ]
2082 .into(),
2083 );
2084 assert_eq!(obj.as_bool(), None);
2085 assert_eq!(obj.as_i64(), None);
2086 assert_eq!(obj.as_f64(), None);
2087 assert_eq!(obj.as_str(), None);
2088 assert!(obj.as_array().is_none());
2089 assert!(obj.as_object().is_some());
2090 if let Some(value) = obj.as_object() {
2091 assert_eq!(value.get("key1").and_then(|v| v.as_str()), Some("string1"));
2092 assert_eq!(value.get("key2").and_then(|v| v.as_i64()), Some(2));
2093 }
2094 }
2095
2096 #[test]
2097 fn test_agent_value_default() {
2098 assert_eq!(AgentValue::default(), AgentValue::Null);
2099 }
2100
2101 #[test]
2102 fn test_agent_value_serialization() {
2103 {
2105 let null = AgentValue::Null;
2106 assert_eq!(serde_json::to_string(&null).unwrap(), "null");
2107 }
2108
2109 {
2111 let boolean_t = AgentValue::boolean(true);
2112 assert_eq!(serde_json::to_string(&boolean_t).unwrap(), "true");
2113
2114 let boolean_f = AgentValue::boolean(false);
2115 assert_eq!(serde_json::to_string(&boolean_f).unwrap(), "false");
2116 }
2117
2118 {
2120 let integer = AgentValue::integer(42);
2121 assert_eq!(serde_json::to_string(&integer).unwrap(), "42");
2122 }
2123
2124 {
2126 let num = AgentValue::number(3.14);
2127 assert_eq!(serde_json::to_string(&num).unwrap(), "3.14");
2128
2129 let num = AgentValue::number(3.0);
2130 assert_eq!(serde_json::to_string(&num).unwrap(), "3.0");
2131 }
2132
2133 {
2135 let s = AgentValue::string("Hello, world!");
2136 assert_eq!(serde_json::to_string(&s).unwrap(), "\"Hello, world!\"");
2137
2138 let s = AgentValue::string("hello\nworld\n\n");
2139 assert_eq!(serde_json::to_string(&s).unwrap(), r#""hello\nworld\n\n""#);
2140 }
2141
2142 {
2153 let array = AgentValue::array(vec![
2154 AgentValue::integer(1),
2155 AgentValue::string("test"),
2156 AgentValue::object(
2157 [
2158 ("key1".to_string(), AgentValue::string("test")),
2159 ("key2".to_string(), AgentValue::integer(2)),
2160 ]
2161 .into(),
2162 ),
2163 ]);
2164 assert_eq!(
2165 serde_json::to_string(&array).unwrap(),
2166 r#"[1,"test",{"key1":"test","key2":2}]"#
2167 );
2168 }
2169
2170 {
2172 let obj = AgentValue::object(
2173 [
2174 ("key1".to_string(), AgentValue::string("test")),
2175 ("key2".to_string(), AgentValue::integer(3)),
2176 ]
2177 .into(),
2178 );
2179 assert_eq!(
2180 serde_json::to_string(&obj).unwrap(),
2181 r#"{"key1":"test","key2":3}"#
2182 );
2183 }
2184 }
2185
2186 #[test]
2187 fn test_agent_value_deserialization() {
2188 {
2190 let deserialized: AgentValue = serde_json::from_str("null").unwrap();
2191 assert_eq!(deserialized, AgentValue::Null);
2192 }
2193
2194 {
2196 let deserialized: AgentValue = serde_json::from_str("false").unwrap();
2197 assert_eq!(deserialized, AgentValue::boolean(false));
2198
2199 let deserialized: AgentValue = serde_json::from_str("true").unwrap();
2200 assert_eq!(deserialized, AgentValue::boolean(true));
2201 }
2202
2203 {
2205 let deserialized: AgentValue = serde_json::from_str("123").unwrap();
2206 assert_eq!(deserialized, AgentValue::integer(123));
2207 }
2208
2209 {
2211 let deserialized: AgentValue = serde_json::from_str("3.14").unwrap();
2212 assert_eq!(deserialized, AgentValue::number(3.14));
2213
2214 let deserialized: AgentValue = serde_json::from_str("3.0").unwrap();
2215 assert_eq!(deserialized, AgentValue::number(3.0));
2216 }
2217
2218 {
2220 let deserialized: AgentValue = serde_json::from_str("\"Hello, world!\"").unwrap();
2221 assert_eq!(deserialized, AgentValue::string("Hello, world!"));
2222
2223 let deserialized: AgentValue = serde_json::from_str(r#""hello\nworld\n\n""#).unwrap();
2224 assert_eq!(deserialized, AgentValue::string("hello\nworld\n\n"));
2225 }
2226
2227 {
2238 let deserialized: AgentValue =
2239 serde_json::from_str(r#"[1,"test",{"key1":"test","key2":2}]"#).unwrap();
2240 assert!(matches!(deserialized, AgentValue::Array(_)));
2241 if let AgentValue::Array(arr) = deserialized {
2242 assert_eq!(arr.len(), 3, "Array length mismatch after serialization");
2243 assert_eq!(arr[0], AgentValue::integer(1));
2244 assert_eq!(arr[1], AgentValue::string("test"));
2245 assert_eq!(
2246 arr[2],
2247 AgentValue::object(
2248 [
2249 ("key1".to_string(), AgentValue::string("test")),
2250 ("key2".to_string(), AgentValue::integer(2)),
2251 ]
2252 .into()
2253 )
2254 );
2255 }
2256 }
2257
2258 {
2260 let deserialized: AgentValue =
2261 serde_json::from_str(r#"{"key1":"test","key2":3}"#).unwrap();
2262 assert_eq!(
2263 deserialized,
2264 AgentValue::object(
2265 [
2266 ("key1".to_string(), AgentValue::string("test")),
2267 ("key2".to_string(), AgentValue::integer(3)),
2268 ]
2269 .into()
2270 )
2271 );
2272 }
2273 }
2274
2275 #[test]
2276 fn test_serialize_deserialize_roundtrip() {
2277 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2278 struct TestStruct {
2279 name: String,
2280 age: i64,
2281 active: bool,
2282 }
2283
2284 let test_data = TestStruct {
2285 name: "Alice".to_string(),
2286 age: 30,
2287 active: true,
2288 };
2289
2290 let agent_data = AgentData::from_serialize(&test_data).unwrap();
2292 assert_eq!(agent_data.kind, "object");
2293 assert_eq!(agent_data.get_str("name"), Some("Alice"));
2294 assert_eq!(agent_data.get_i64("age"), Some(30));
2295 assert_eq!(agent_data.get_bool("active"), Some(true));
2296
2297 let restored: TestStruct = agent_data.to_deserialize().unwrap();
2298 assert_eq!(restored, test_data);
2299
2300 let agent_data_custom = AgentData::from_serialize_with_kind("person", &test_data).unwrap();
2302 assert_eq!(agent_data_custom.kind, "person");
2303 let restored_custom: TestStruct = agent_data_custom.to_deserialize().unwrap();
2304 assert_eq!(restored_custom, test_data);
2305
2306 let agent_value = AgentValue::from_serialize(&test_data).unwrap();
2308 assert!(agent_value.is_object());
2309 assert_eq!(agent_value.get_str("name"), Some("Alice"));
2310
2311 let restored_value: TestStruct = agent_value.to_deserialize().unwrap();
2312 assert_eq!(restored_value, test_data);
2313 }
2314
2315 #[test]
2316 fn test_serialize_deserialize_nested() {
2317 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2318 struct Address {
2319 street: String,
2320 city: String,
2321 zip: String,
2322 }
2323
2324 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2325 struct Person {
2326 name: String,
2327 age: i64,
2328 address: Address,
2329 tags: Vec<String>,
2330 }
2331
2332 let person = Person {
2333 name: "Bob".to_string(),
2334 age: 25,
2335 address: Address {
2336 street: "123 Main St".to_string(),
2337 city: "Springfield".to_string(),
2338 zip: "12345".to_string(),
2339 },
2340 tags: vec!["developer".to_string(), "rust".to_string()],
2341 };
2342
2343 let agent_data = AgentData::from_serialize(&person).unwrap();
2345 assert_eq!(agent_data.kind, "object");
2346 assert_eq!(agent_data.get_str("name"), Some("Bob"));
2347
2348 let address = agent_data.get_object("address").unwrap();
2349 assert_eq!(
2350 address.get("city").and_then(|v| v.as_str()),
2351 Some("Springfield")
2352 );
2353
2354 let tags = agent_data.get_array("tags").unwrap();
2355 assert_eq!(tags.len(), 2);
2356 assert_eq!(tags[0].as_str(), Some("developer"));
2357
2358 let restored: Person = agent_data.to_deserialize().unwrap();
2359 assert_eq!(restored, person);
2360 }
2361}