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
806#[cfg(test)]
807mod tests {
808 use super::*;
809 use serde_json::json;
810
811 #[test]
812 fn test_agent_data_new_constructors() {
813 let unit_data = AgentData::unit();
815 assert_eq!(unit_data.kind, "unit");
816 assert_eq!(unit_data.value, AgentValue::Null);
817
818 let bool_data = AgentData::boolean(true);
819 assert_eq!(bool_data.kind, "boolean");
820 assert_eq!(bool_data.value, AgentValue::Boolean(true));
821
822 let int_data = AgentData::integer(42);
823 assert_eq!(int_data.kind, "integer");
824 assert_eq!(int_data.value, AgentValue::Integer(42));
825
826 let num_data = AgentData::number(3.14);
827 assert_eq!(num_data.kind, "number");
828 assert!(matches!(num_data.value, AgentValue::Number(_)));
829 if let AgentValue::Number(num) = num_data.value {
830 assert!((num - 3.14).abs() < f64::EPSILON);
831 }
832
833 let str_data = AgentData::string("hello".to_string());
834 assert_eq!(str_data.kind, "string");
835 assert!(matches!(str_data.value, AgentValue::String(_)));
836 assert_eq!(str_data.as_str().unwrap(), "hello");
837
838 let text_data = AgentData::text("multiline\ntext\n\n".to_string());
839 assert_eq!(text_data.kind, "text");
840 assert!(matches!(text_data.value, AgentValue::String(_)));
841 assert_eq!(text_data.as_str().unwrap(), "multiline\ntext\n\n");
842
843 let obj_val = [
854 ("key1".to_string(), AgentValue::string("string1")),
855 ("key2".to_string(), AgentValue::integer(2)),
856 ];
857 let obj_data = AgentData::object(obj_val.clone().into());
858 assert_eq!(obj_data.kind, "object");
859 assert!(matches!(obj_data.value, AgentValue::Object(_)));
860 assert_eq!(obj_data.as_object().unwrap(), &obj_val.into());
861 }
862
863 #[test]
864 fn test_agent_data_from_kind_value() {
865 let unit_data = AgentData::from_json_with_kind("unit", json!(null)).unwrap();
867 assert_eq!(unit_data.kind, "unit");
868 assert_eq!(unit_data.value, AgentValue::Null);
869
870 let bool_data = AgentData::from_json_with_kind("boolean", json!(true)).unwrap();
871 assert_eq!(bool_data.kind, "boolean");
872 assert_eq!(bool_data.value, AgentValue::Boolean(true));
873
874 let int_data = AgentData::from_json_with_kind("integer", json!(42)).unwrap();
875 assert_eq!(int_data.kind, "integer");
876 assert_eq!(int_data.value, AgentValue::Integer(42));
877
878 let int_data = AgentData::from_json_with_kind("integer", json!(3.14)).unwrap();
879 assert_eq!(int_data.kind, "integer");
880 assert_eq!(int_data.value, AgentValue::Integer(3));
881
882 let num_data = AgentData::from_json_with_kind("number", json!(3.14)).unwrap();
883 assert_eq!(num_data.kind, "number");
884 assert_eq!(num_data.value, AgentValue::number(3.14));
885
886 let num_data = AgentData::from_json_with_kind("number", json!(3)).unwrap();
887 assert_eq!(num_data.kind, "number");
888 assert_eq!(num_data.value, AgentValue::number(3.0));
889
890 let str_data = AgentData::from_json_with_kind("string", json!("hello")).unwrap();
891 assert_eq!(str_data.kind, "string");
892 assert_eq!(str_data.value, AgentValue::string("hello"));
893
894 let str_data = AgentData::from_json_with_kind("string", json!("hello\nworld\n\n")).unwrap();
895 assert_eq!(str_data.kind, "string");
896 assert_eq!(str_data.value, AgentValue::string("hello\nworld\n\n"));
897
898 let text_data = AgentData::from_json_with_kind("text", json!("hello")).unwrap();
899 assert_eq!(text_data.kind, "text");
900 assert_eq!(text_data.value, AgentValue::string("hello"));
901
902 let text_data = AgentData::from_json_with_kind("text", json!("hello\nworld\n\n")).unwrap();
903 assert_eq!(text_data.kind, "text");
904 assert_eq!(text_data.value, AgentValue::string("hello\nworld\n\n"));
905
906 let obj_data =
918 AgentData::from_json_with_kind("object", json!({"key1": "string1", "key2": 2}))
919 .unwrap();
920 assert_eq!(obj_data.kind, "object");
921 assert_eq!(
922 obj_data.value,
923 AgentValue::object(
924 [
925 ("key1".to_string(), AgentValue::string("string1")),
926 ("key2".to_string(), AgentValue::integer(2)),
927 ]
928 .into()
929 )
930 );
931
932 let obj_data = AgentData::from_json_with_kind(
934 "custom_type".to_string(),
935 json!({"foo": "hi", "bar": 3}),
936 )
937 .unwrap();
938 assert_eq!(obj_data.kind, "custom_type");
939 assert_eq!(
940 obj_data.value,
941 AgentValue::object(
942 [
943 ("foo".to_string(), AgentValue::string("hi")),
944 ("bar".to_string(), AgentValue::integer(3)),
945 ]
946 .into()
947 )
948 );
949
950 let array_data = AgentData::from_json_with_kind("unit", json!([null, null])).unwrap();
952 assert_eq!(array_data.kind, "unit");
953 assert_eq!(
954 array_data.value,
955 AgentValue::array(vec![AgentValue::unit(), AgentValue::unit(),])
956 );
957
958 let array_data = AgentData::from_json_with_kind("boolean", json!([true, false])).unwrap();
959 assert_eq!(array_data.kind, "boolean");
960 assert_eq!(
961 array_data.value,
962 AgentValue::array(vec![AgentValue::boolean(true), AgentValue::boolean(false),])
963 );
964
965 let array_data = AgentData::from_json_with_kind("integer", json!([1, 2.1, 3.0])).unwrap();
966 assert_eq!(array_data.kind, "integer");
967 assert_eq!(
968 array_data.value,
969 AgentValue::array(vec![
970 AgentValue::integer(1),
971 AgentValue::integer(2),
972 AgentValue::integer(3),
973 ])
974 );
975
976 let array_data = AgentData::from_json_with_kind("number", json!([1.0, 2.1, 3])).unwrap();
977 assert_eq!(array_data.kind, "number");
978 assert_eq!(
979 array_data.value,
980 AgentValue::array(vec![
981 AgentValue::number(1.0),
982 AgentValue::number(2.1),
983 AgentValue::number(3.0),
984 ])
985 );
986
987 let array_data =
988 AgentData::from_json_with_kind("string", json!(["test", "hello\nworld\n", ""]))
989 .unwrap();
990 assert_eq!(array_data.kind, "string");
991 assert_eq!(
992 array_data.value,
993 AgentValue::array(vec![
994 AgentValue::string("test"),
995 AgentValue::string("hello\nworld\n"),
996 AgentValue::string(""),
997 ])
998 );
999
1000 let array_data =
1001 AgentData::from_json_with_kind("text", json!(["test", "hello\nworld\n", ""])).unwrap();
1002 assert_eq!(array_data.kind, "text");
1003 assert_eq!(
1004 array_data.value,
1005 AgentValue::array(vec![
1006 AgentValue::string("test"),
1007 AgentValue::string("hello\nworld\n"),
1008 AgentValue::string(""),
1009 ])
1010 );
1011
1012 let array_data = AgentData::from_json_with_kind(
1013 "object",
1014 json!([{"key1":"test","key2":1}, {"key1":"test2","key2":"hi"}, {}]),
1015 )
1016 .unwrap();
1017 assert_eq!(array_data.kind, "object");
1018 assert_eq!(
1019 array_data.value,
1020 AgentValue::array(vec![
1021 AgentValue::object(
1022 [
1023 ("key1".to_string(), AgentValue::string("test")),
1024 ("key2".to_string(), AgentValue::integer(1)),
1025 ]
1026 .into()
1027 ),
1028 AgentValue::object(
1029 [
1030 ("key1".to_string(), AgentValue::string("test2")),
1031 ("key2".to_string(), AgentValue::string("hi")),
1032 ]
1033 .into()
1034 ),
1035 AgentValue::object(AgentValueMap::default()),
1036 ])
1037 );
1038
1039 let array_data = AgentData::from_json_with_kind(
1040 "custom",
1041 json!([{"key1":"test","key2":1}, {"key1":"test2","key2":"hi"}, {}]),
1042 )
1043 .unwrap();
1044 assert_eq!(array_data.kind, "custom");
1045 assert_eq!(
1046 array_data.value,
1047 AgentValue::array(vec![
1048 AgentValue::object(
1049 [
1050 ("key1".to_string(), AgentValue::string("test")),
1051 ("key2".to_string(), AgentValue::integer(1)),
1052 ]
1053 .into()
1054 ),
1055 AgentValue::object(
1056 [
1057 ("key1".to_string(), AgentValue::string("test2")),
1058 ("key2".to_string(), AgentValue::string("hi")),
1059 ]
1060 .into()
1061 ),
1062 AgentValue::object(AgentValueMap::default()),
1063 ])
1064 );
1065 }
1066
1067 #[test]
1068 fn test_agent_data_from_json_value() {
1069 let unit_data = AgentData::from_json(json!(null)).unwrap();
1071 assert_eq!(unit_data.kind, "unit");
1072 assert_eq!(unit_data.value, AgentValue::Null);
1073
1074 let bool_data = AgentData::from_json(json!(true)).unwrap();
1075 assert_eq!(bool_data.kind, "boolean");
1076 assert_eq!(bool_data.value, AgentValue::Boolean(true));
1077
1078 let int_data = AgentData::from_json(json!(42)).unwrap();
1079 assert_eq!(int_data.kind, "integer");
1080 assert_eq!(int_data.value, AgentValue::Integer(42));
1081
1082 let num_data = AgentData::from_json(json!(3.14)).unwrap();
1083 assert_eq!(num_data.kind, "number");
1084 assert_eq!(num_data.value, AgentValue::number(3.14));
1085
1086 let str_data = AgentData::from_json(json!("hello")).unwrap();
1087 assert_eq!(str_data.kind, "string");
1088 assert_eq!(str_data.value, AgentValue::string("hello"));
1089
1090 let str_data = AgentData::from_json(json!("hello\nworld\n\n")).unwrap();
1091 assert_eq!(str_data.kind, "string");
1092 assert_eq!(str_data.value, AgentValue::string("hello\nworld\n\n"));
1093
1094 let obj_data = AgentData::from_json(json!({"key1": "string1", "key2": 2})).unwrap();
1108 assert_eq!(obj_data.kind, "object");
1109 assert_eq!(
1110 obj_data.value,
1111 AgentValue::object(
1112 [
1113 ("key1".to_string(), AgentValue::string("string1")),
1114 ("key2".to_string(), AgentValue::integer(2)),
1115 ]
1116 .into()
1117 )
1118 );
1119
1120 let array_data = AgentData::from_json(json!([null, null])).unwrap();
1122 assert_eq!(array_data.kind, "unit");
1123 assert_eq!(
1124 array_data.value,
1125 AgentValue::array(vec![AgentValue::unit(), AgentValue::unit(),])
1126 );
1127
1128 let array_data = AgentData::from_json(json!([true, false])).unwrap();
1129 assert_eq!(array_data.kind, "boolean");
1130 assert_eq!(
1131 array_data.value,
1132 AgentValue::array(vec![AgentValue::boolean(true), AgentValue::boolean(false),])
1133 );
1134
1135 let array_data = AgentData::from_json(json!([1, 2, 3])).unwrap();
1136 assert_eq!(array_data.kind, "integer");
1137 assert_eq!(
1138 array_data.value,
1139 AgentValue::array(vec![
1140 AgentValue::integer(1),
1141 AgentValue::integer(2),
1142 AgentValue::integer(3),
1143 ])
1144 );
1145
1146 let array_data = AgentData::from_json(json!([1.0, 2.1, 3.2])).unwrap();
1147 assert_eq!(array_data.kind, "number");
1148 assert_eq!(
1149 array_data.value,
1150 AgentValue::array(vec![
1151 AgentValue::number(1.0),
1152 AgentValue::number(2.1),
1153 AgentValue::number(3.2),
1154 ])
1155 );
1156
1157 let array_data = AgentData::from_json(json!(["test", "hello\nworld\n", ""])).unwrap();
1158 assert_eq!(array_data.kind, "string");
1159 assert_eq!(
1160 array_data.value,
1161 AgentValue::array(vec![
1162 AgentValue::string("test"),
1163 AgentValue::string("hello\nworld\n"),
1164 AgentValue::string(""),
1165 ])
1166 );
1167
1168 let array_data = AgentData::from_json(
1169 json!([{"key1":"test","key2":1}, {"key1":"test2","key2":"hi"}, {}]),
1170 )
1171 .unwrap();
1172 assert_eq!(array_data.kind, "object");
1173 assert_eq!(
1174 array_data.value,
1175 AgentValue::array(vec![
1176 AgentValue::object(
1177 [
1178 ("key1".to_string(), AgentValue::string("test")),
1179 ("key2".to_string(), AgentValue::integer(1)),
1180 ]
1181 .into()
1182 ),
1183 AgentValue::object(
1184 [
1185 ("key1".to_string(), AgentValue::string("test2")),
1186 ("key2".to_string(), AgentValue::string("hi")),
1187 ]
1188 .into()
1189 ),
1190 AgentValue::object(AgentValueMap::default()),
1191 ])
1192 );
1193 }
1194
1195 #[test]
1196 fn test_agent_data_accessor_methods() {
1197 let str_data = AgentData::string("hello".to_string());
1199 assert_eq!(str_data.as_str().unwrap(), "hello");
1200 assert!(str_data.as_object().is_none());
1201
1202 let obj_val = [
1203 ("key1".to_string(), AgentValue::string("string1")),
1204 ("key2".to_string(), AgentValue::integer(2)),
1205 ];
1206 let obj_data = AgentData::object(obj_val.clone().into());
1207 assert!(obj_data.as_str().is_none());
1208 assert_eq!(obj_data.as_object().unwrap(), &obj_val.into());
1209 }
1210
1211 #[test]
1212 fn test_agent_data_serialization() {
1213 {
1215 let data = AgentData::unit();
1216 assert_eq!(
1217 serde_json::to_string(&data).unwrap(),
1218 r#"{"kind":"unit","value":null}"#
1219 );
1220 }
1221
1222 {
1224 let data = AgentData::boolean(true);
1225 assert_eq!(
1226 serde_json::to_string(&data).unwrap(),
1227 r#"{"kind":"boolean","value":true}"#
1228 );
1229
1230 let data = AgentData::boolean(false);
1231 assert_eq!(
1232 serde_json::to_string(&data).unwrap(),
1233 r#"{"kind":"boolean","value":false}"#
1234 );
1235 }
1236
1237 {
1239 let data = AgentData::integer(42);
1240 assert_eq!(
1241 serde_json::to_string(&data).unwrap(),
1242 r#"{"kind":"integer","value":42}"#
1243 );
1244 }
1245
1246 {
1248 let data = AgentData::number(3.14);
1249 assert_eq!(
1250 serde_json::to_string(&data).unwrap(),
1251 r#"{"kind":"number","value":3.14}"#
1252 );
1253
1254 let data = AgentData::number(3.0);
1255 assert_eq!(
1256 serde_json::to_string(&data).unwrap(),
1257 r#"{"kind":"number","value":3.0}"#
1258 );
1259 }
1260
1261 {
1263 let data = AgentData::string("Hello, world!");
1264 assert_eq!(
1265 serde_json::to_string(&data).unwrap(),
1266 r#"{"kind":"string","value":"Hello, world!"}"#
1267 );
1268
1269 let data = AgentData::string("hello\nworld\n\n");
1270 assert_eq!(
1271 serde_json::to_string(&data).unwrap(),
1272 r#"{"kind":"string","value":"hello\nworld\n\n"}"#
1273 );
1274 }
1275
1276 {
1278 let data = AgentData::text("Hello, world!");
1279 assert_eq!(
1280 serde_json::to_string(&data).unwrap(),
1281 r#"{"kind":"text","value":"Hello, world!"}"#
1282 );
1283
1284 let data = AgentData::text("hello\nworld\n\n");
1285 assert_eq!(
1286 serde_json::to_string(&data).unwrap(),
1287 r#"{"kind":"text","value":"hello\nworld\n\n"}"#
1288 );
1289 }
1290
1291 {
1302 let data = AgentData::object(
1303 [
1304 ("key1".to_string(), AgentValue::string("string1")),
1305 ("key2".to_string(), AgentValue::integer(2)),
1306 ]
1307 .into(),
1308 );
1309 assert_eq!(
1310 serde_json::to_string(&data).unwrap(),
1311 r#"{"kind":"object","value":{"key1":"string1","key2":2}}"#
1312 );
1313 }
1314
1315 {
1317 let data = AgentData::object_with_kind(
1318 "custom",
1319 [
1320 ("key1".to_string(), AgentValue::string("test")),
1321 ("key2".to_string(), AgentValue::integer(3)),
1322 ]
1323 .into(),
1324 );
1325 assert_eq!(
1326 serde_json::to_string(&data).unwrap(),
1327 r#"{"kind":"custom","value":{"key1":"test","key2":3}}"#
1328 );
1329 }
1330
1331 {
1333 let data = AgentData::array("unit", vec![AgentValue::unit(), AgentValue::unit()]);
1334 assert_eq!(
1335 serde_json::to_string(&data).unwrap(),
1336 r#"{"kind":"unit","value":[null,null]}"#
1337 );
1338
1339 let data = AgentData::array(
1340 "boolean",
1341 vec![AgentValue::boolean(false), AgentValue::boolean(true)],
1342 );
1343 assert_eq!(
1344 serde_json::to_string(&data).unwrap(),
1345 r#"{"kind":"boolean","value":[false,true]}"#
1346 );
1347
1348 let data = AgentData::array(
1349 "integer",
1350 vec![
1351 AgentValue::integer(1),
1352 AgentValue::integer(2),
1353 AgentValue::integer(3),
1354 ],
1355 );
1356 assert_eq!(
1357 serde_json::to_string(&data).unwrap(),
1358 r#"{"kind":"integer","value":[1,2,3]}"#
1359 );
1360
1361 let data = AgentData::array(
1362 "number",
1363 vec![
1364 AgentValue::number(1.0),
1365 AgentValue::number(2.1),
1366 AgentValue::number(3.2),
1367 ],
1368 );
1369 assert_eq!(
1370 serde_json::to_string(&data).unwrap(),
1371 r#"{"kind":"number","value":[1.0,2.1,3.2]}"#
1372 );
1373
1374 let data = AgentData::array(
1375 "string",
1376 vec![
1377 AgentValue::string("test"),
1378 AgentValue::string("hello\nworld\n"),
1379 AgentValue::string(""),
1380 ],
1381 );
1382 assert_eq!(
1383 serde_json::to_string(&data).unwrap(),
1384 r#"{"kind":"string","value":["test","hello\nworld\n",""]}"#
1385 );
1386
1387 let data = AgentData::array(
1388 "text",
1389 vec![
1390 AgentValue::string("test"),
1391 AgentValue::string("hello\nworld\n"),
1392 AgentValue::string(""),
1393 ],
1394 );
1395 assert_eq!(
1396 serde_json::to_string(&data).unwrap(),
1397 r#"{"kind":"text","value":["test","hello\nworld\n",""]}"#
1398 );
1399
1400 let data = AgentData::array(
1401 "object",
1402 vec![
1403 AgentValue::object(
1404 [
1405 ("key1".to_string(), AgentValue::string("test")),
1406 ("key2".to_string(), AgentValue::integer(1)),
1407 ]
1408 .into(),
1409 ),
1410 AgentValue::object(
1411 [
1412 ("key1".to_string(), AgentValue::string("test2")),
1413 ("key2".to_string(), AgentValue::string("hi")),
1414 ]
1415 .into(),
1416 ),
1417 AgentValue::object(AgentValueMap::default()),
1418 ],
1419 );
1420 assert_eq!(
1421 serde_json::to_string(&data).unwrap(),
1422 r#"{"kind":"object","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#
1423 );
1424
1425 let data = AgentData::array(
1426 "custom",
1427 vec![
1428 AgentValue::object(
1429 [
1430 ("key1".to_string(), AgentValue::string("test")),
1431 ("key2".to_string(), AgentValue::integer(1)),
1432 ]
1433 .into(),
1434 ),
1435 AgentValue::object(
1436 [
1437 ("key1".to_string(), AgentValue::string("test2")),
1438 ("key2".to_string(), AgentValue::string("hi")),
1439 ]
1440 .into(),
1441 ),
1442 AgentValue::object(AgentValueMap::default()),
1443 ],
1444 );
1445 assert_eq!(
1446 serde_json::to_string(&data).unwrap(),
1447 r#"{"kind":"custom","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#
1448 );
1449 }
1450 }
1451
1452 #[test]
1453 fn test_agent_data_deserialization() {
1454 {
1456 let deserialized: AgentData =
1457 serde_json::from_str(r#"{"kind":"unit","value":null}"#).unwrap();
1458 assert_eq!(deserialized, AgentData::unit());
1459 }
1460
1461 {
1463 let deserialized: AgentData =
1464 serde_json::from_str(r#"{"kind":"boolean","value":false}"#).unwrap();
1465 assert_eq!(deserialized, AgentData::boolean(false));
1466
1467 let deserialized: AgentData =
1468 serde_json::from_str(r#"{"kind":"boolean","value":true}"#).unwrap();
1469 assert_eq!(deserialized, AgentData::boolean(true));
1470 }
1471
1472 {
1474 let deserialized: AgentData =
1475 serde_json::from_str(r#"{"kind":"integer","value":123}"#).unwrap();
1476 assert_eq!(deserialized, AgentData::integer(123));
1477 }
1478
1479 {
1481 let deserialized: AgentData =
1482 serde_json::from_str(r#"{"kind":"number","value":3.14}"#).unwrap();
1483 assert_eq!(deserialized, AgentData::number(3.14));
1484
1485 let deserialized: AgentData =
1486 serde_json::from_str(r#"{"kind":"number","value":3.0}"#).unwrap();
1487 assert_eq!(deserialized, AgentData::number(3.0));
1488 }
1489
1490 {
1492 let deserialized: AgentData =
1493 serde_json::from_str(r#"{"kind":"string","value":"Hello, world!"}"#).unwrap();
1494 assert_eq!(deserialized, AgentData::string("Hello, world!"));
1495
1496 let deserialized: AgentData =
1497 serde_json::from_str(r#"{"kind":"string","value":"hello\nworld\n\n"}"#).unwrap();
1498 assert_eq!(deserialized, AgentData::string("hello\nworld\n\n"));
1499 }
1500
1501 {
1513 let deserialized: AgentData =
1514 serde_json::from_str(r#"{"kind":"object","value":{"key1":"test","key2":3}}"#)
1515 .unwrap();
1516 assert_eq!(
1517 deserialized,
1518 AgentData::object(
1519 [
1520 ("key1".to_string(), AgentValue::string("test")),
1521 ("key2".to_string(), AgentValue::integer(3))
1522 ]
1523 .into()
1524 )
1525 );
1526 }
1527
1528 {
1530 let deserialized: AgentData =
1531 serde_json::from_str(r#"{"kind":"custom","value":{"name":"test","value":3}}"#)
1532 .unwrap();
1533 assert_eq!(
1534 deserialized,
1535 AgentData::object_with_kind(
1536 "custom",
1537 [
1538 ("name".to_string(), AgentValue::string("test")),
1539 ("value".to_string(), AgentValue::integer(3))
1540 ]
1541 .into()
1542 )
1543 );
1544 }
1545
1546 {
1548 let deserialized: AgentData =
1549 serde_json::from_str(r#"{"kind":"unit","value":[null,null]}"#).unwrap();
1550 assert_eq!(
1551 deserialized,
1552 AgentData::array("unit", vec![AgentValue::unit(), AgentValue::unit(),])
1553 );
1554
1555 let deserialized: AgentData =
1556 serde_json::from_str(r#"{"kind":"boolean","value":[true,false]}"#).unwrap();
1557 assert_eq!(
1558 deserialized,
1559 AgentData::array(
1560 "boolean",
1561 vec![AgentValue::boolean(true), AgentValue::boolean(false),]
1562 )
1563 );
1564
1565 let deserialized: AgentData =
1566 serde_json::from_str(r#"{"kind":"integer","value":[1,2,3]}"#).unwrap();
1567 assert_eq!(
1568 deserialized,
1569 AgentData::array(
1570 "integer",
1571 vec![
1572 AgentValue::integer(1),
1573 AgentValue::integer(2),
1574 AgentValue::integer(3),
1575 ]
1576 )
1577 );
1578
1579 let deserialized: AgentData =
1580 serde_json::from_str(r#"{"kind":"number","value":[1.0,2.1,3]}"#).unwrap();
1581 assert_eq!(
1582 deserialized,
1583 AgentData::array(
1584 "number",
1585 vec![
1586 AgentValue::number(1.0),
1587 AgentValue::number(2.1),
1588 AgentValue::number(3.0),
1589 ]
1590 )
1591 );
1592
1593 let deserialized: AgentData =
1594 serde_json::from_str(r#"{"kind":"string","value":["test","hello\nworld\n",""]}"#)
1595 .unwrap();
1596 assert_eq!(
1597 deserialized,
1598 AgentData::array(
1599 "string",
1600 vec![
1601 AgentValue::string("test"),
1602 AgentValue::string("hello\nworld\n"),
1603 AgentValue::string(""),
1604 ]
1605 )
1606 );
1607
1608 let deserialized: AgentData =
1609 serde_json::from_str(r#"{"kind":"text","value":["test","hello\nworld\n",""]}"#)
1610 .unwrap();
1611 assert_eq!(
1612 deserialized,
1613 AgentData::array(
1614 "text",
1615 vec![
1616 AgentValue::string("test"),
1617 AgentValue::string("hello\nworld\n"),
1618 AgentValue::string(""),
1619 ]
1620 )
1621 );
1622
1623 let deserialized: AgentData =
1624 serde_json::from_str(r#"{"kind":"object","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#)
1625 .unwrap();
1626 assert_eq!(
1627 deserialized,
1628 AgentData::array(
1629 "object",
1630 vec![
1631 AgentValue::object(
1632 [
1633 ("key1".to_string(), AgentValue::string("test")),
1634 ("key2".to_string(), AgentValue::integer(1)),
1635 ]
1636 .into()
1637 ),
1638 AgentValue::object(
1639 [
1640 ("key1".to_string(), AgentValue::string("test2")),
1641 ("key2".to_string(), AgentValue::string("hi")),
1642 ]
1643 .into()
1644 ),
1645 AgentValue::object(AgentValueMap::default()),
1646 ]
1647 )
1648 );
1649
1650 let deserialized: AgentData =
1651 serde_json::from_str(r#"{"kind":"custom","value":[{"key1":"test","key2":1},{"key1":"test2","key2":"hi"},{}]}"#)
1652 .unwrap();
1653 assert_eq!(
1654 deserialized,
1655 AgentData::array(
1656 "custom",
1657 vec![
1658 AgentValue::object(
1659 [
1660 ("key1".to_string(), AgentValue::string("test")),
1661 ("key2".to_string(), AgentValue::integer(1)),
1662 ]
1663 .into()
1664 ),
1665 AgentValue::object(
1666 [
1667 ("key1".to_string(), AgentValue::string("test2")),
1668 ("key2".to_string(), AgentValue::string("hi")),
1669 ]
1670 .into()
1671 ),
1672 AgentValue::object(AgentValueMap::default()),
1673 ]
1674 )
1675 );
1676 }
1677 }
1678
1679 #[test]
1680 fn test_agent_value_constructors() {
1681 let unit = AgentValue::unit();
1683 assert_eq!(unit, AgentValue::Null);
1684
1685 let boolean = AgentValue::boolean(true);
1686 assert_eq!(boolean, AgentValue::Boolean(true));
1687
1688 let integer = AgentValue::integer(42);
1689 assert_eq!(integer, AgentValue::Integer(42));
1690
1691 let number = AgentValue::number(3.14);
1692 assert!(matches!(number, AgentValue::Number(_)));
1693 if let AgentValue::Number(num) = number {
1694 assert!((num - 3.14).abs() < f64::EPSILON);
1695 }
1696
1697 let string = AgentValue::string("hello");
1698 assert!(matches!(string, AgentValue::String(_)));
1699 assert_eq!(string.as_str().unwrap(), "hello");
1700
1701 let text = AgentValue::string("multiline\ntext");
1702 assert!(matches!(text, AgentValue::String(_)));
1703 assert_eq!(text.as_str().unwrap(), "multiline\ntext");
1704
1705 let array = AgentValue::array(vec![AgentValue::integer(1), AgentValue::integer(2)]);
1706 assert!(matches!(array, AgentValue::Array(_)));
1707 if let AgentValue::Array(arr) = array {
1708 assert_eq!(arr.len(), 2);
1709 assert_eq!(arr[0].as_i64().unwrap(), 1);
1710 assert_eq!(arr[1].as_i64().unwrap(), 2);
1711 }
1712
1713 let obj = AgentValue::object(
1714 [
1715 ("key1".to_string(), AgentValue::string("string1")),
1716 ("key2".to_string(), AgentValue::integer(2)),
1717 ]
1718 .into(),
1719 );
1720 assert!(matches!(obj, AgentValue::Object(_)));
1721 if let AgentValue::Object(obj) = obj {
1722 assert_eq!(obj.get("key1").and_then(|v| v.as_str()), Some("string1"));
1723 assert_eq!(obj.get("key2").and_then(|v| v.as_i64()), Some(2));
1724 } else {
1725 panic!("Object was not deserialized correctly");
1726 }
1727 }
1728
1729 #[test]
1730 fn test_agent_value_from_json_value() {
1731 let null = AgentValue::from_json(json!(null)).unwrap();
1733 assert_eq!(null, AgentValue::Null);
1734
1735 let boolean = AgentValue::from_json(json!(true)).unwrap();
1736 assert_eq!(boolean, AgentValue::Boolean(true));
1737
1738 let integer = AgentValue::from_json(json!(42)).unwrap();
1739 assert_eq!(integer, AgentValue::Integer(42));
1740
1741 let number = AgentValue::from_json(json!(3.14)).unwrap();
1742 assert!(matches!(number, AgentValue::Number(_)));
1743 if let AgentValue::Number(num) = number {
1744 assert!((num - 3.14).abs() < f64::EPSILON);
1745 }
1746
1747 let string = AgentValue::from_json(json!("hello")).unwrap();
1748 assert!(matches!(string, AgentValue::String(_)));
1749 if let AgentValue::String(s) = string {
1750 assert_eq!(*s, "hello");
1751 } else {
1752 panic!("Expected string value");
1753 }
1754
1755 let array = AgentValue::from_json(json!([1, "test", true])).unwrap();
1756 assert!(matches!(array, AgentValue::Array(_)));
1757 if let AgentValue::Array(arr) = array {
1758 assert_eq!(arr.len(), 3);
1759 assert_eq!(arr[0], AgentValue::Integer(1));
1760 assert!(matches!(&arr[1], AgentValue::String(_)));
1761 if let AgentValue::String(s) = &arr[1] {
1762 assert_eq!(**s, "test");
1763 } else {
1764 panic!("Expected string value");
1765 }
1766 assert_eq!(arr[2], AgentValue::Boolean(true));
1767 }
1768
1769 let object = AgentValue::from_json(json!({"key1": "string1", "key2": 2})).unwrap();
1770 assert!(matches!(object, AgentValue::Object(_)));
1771 if let AgentValue::Object(obj) = object {
1772 assert_eq!(obj.get("key1").and_then(|v| v.as_str()), Some("string1"));
1773 assert_eq!(obj.get("key2").and_then(|v| v.as_i64()), Some(2));
1774 } else {
1775 panic!("Object was not deserialized correctly");
1776 }
1777 }
1778
1779 #[test]
1780 fn test_agent_value_from_kind_value() {
1781 let unit = AgentValue::from_kind_json("unit", json!(null)).unwrap();
1783 assert_eq!(unit, AgentValue::Null);
1784
1785 let boolean = AgentValue::from_kind_json("boolean", json!(true)).unwrap();
1786 assert_eq!(boolean, AgentValue::Boolean(true));
1787
1788 let integer = AgentValue::from_kind_json("integer", json!(42)).unwrap();
1789 assert_eq!(integer, AgentValue::Integer(42));
1790
1791 let integer = AgentValue::from_kind_json("integer", json!(42.0)).unwrap();
1792 assert_eq!(integer, AgentValue::Integer(42));
1793
1794 let number = AgentValue::from_kind_json("number", json!(3.14)).unwrap();
1795 assert!(matches!(number, AgentValue::Number(_)));
1796 if let AgentValue::Number(num) = number {
1797 assert!((num - 3.14).abs() < f64::EPSILON);
1798 }
1799
1800 let number = AgentValue::from_kind_json("number", json!(3)).unwrap();
1801 assert!(matches!(number, AgentValue::Number(_)));
1802 if let AgentValue::Number(num) = number {
1803 assert!((num - 3.0).abs() < f64::EPSILON);
1804 }
1805
1806 let string = AgentValue::from_kind_json("string", json!("hello")).unwrap();
1807 assert!(matches!(string, AgentValue::String(_)));
1808 if let AgentValue::String(s) = string {
1809 assert_eq!(*s, "hello");
1810 } else {
1811 panic!("Expected string value");
1812 }
1813
1814 let text = AgentValue::from_kind_json("text", json!("multiline\ntext")).unwrap();
1815 assert!(matches!(text, AgentValue::String(_)));
1816 if let AgentValue::String(t) = text {
1817 assert_eq!(*t, "multiline\ntext");
1818 } else {
1819 panic!("Expected text value");
1820 }
1821
1822 let array = AgentValue::from_kind_json("array", json!([1, "test", true])).unwrap();
1823 assert!(matches!(array, AgentValue::Array(_)));
1824 if let AgentValue::Array(arr) = array {
1825 assert_eq!(arr.len(), 3);
1826 assert_eq!(arr[0], AgentValue::Integer(1));
1827 assert!(matches!(&arr[1], AgentValue::String(_)));
1828 if let AgentValue::String(s) = &arr[1] {
1829 assert_eq!(**s, "test");
1830 } else {
1831 panic!("Expected string value");
1832 }
1833 assert_eq!(arr[2], AgentValue::Boolean(true));
1834 }
1835
1836 let obj = AgentValue::from_kind_json("object", json!({"key1": "test", "key2": 2})).unwrap();
1837 assert!(matches!(obj, AgentValue::Object(_)));
1838 if let AgentValue::Object(obj) = obj {
1839 assert_eq!(obj.get("key1").and_then(|v| v.as_str()), Some("test"));
1840 assert_eq!(obj.get("key2").and_then(|v| v.as_i64()), Some(2));
1841 } else {
1842 panic!("Object was not deserialized correctly");
1843 }
1844
1845 let unit_array = AgentValue::from_kind_json("unit", json!([null, null])).unwrap();
1847 assert!(matches!(unit_array, AgentValue::Array(_)));
1848 if let AgentValue::Array(arr) = unit_array {
1849 assert_eq!(arr.len(), 2);
1850 for val in arr.iter() {
1851 assert_eq!(*val, AgentValue::Null);
1852 }
1853 }
1854
1855 let bool_array = AgentValue::from_kind_json("boolean", json!([true, false])).unwrap();
1856 assert!(matches!(bool_array, AgentValue::Array(_)));
1857 if let AgentValue::Array(arr) = bool_array {
1858 assert_eq!(arr.len(), 2);
1859 assert_eq!(arr[0], AgentValue::Boolean(true));
1860 assert_eq!(arr[1], AgentValue::Boolean(false));
1861 }
1862
1863 let int_array = AgentValue::from_kind_json("integer", json!([1, 2, 3])).unwrap();
1864 assert!(matches!(int_array, AgentValue::Array(_)));
1865 if let AgentValue::Array(arr) = int_array {
1866 assert_eq!(arr.len(), 3);
1867 assert_eq!(arr[0], AgentValue::Integer(1));
1868 assert_eq!(arr[1], AgentValue::Integer(2));
1869 assert_eq!(arr[2], AgentValue::Integer(3));
1870 }
1871
1872 let num_array = AgentValue::from_kind_json("number", json!([1.1, 2.2, 3.3])).unwrap();
1873 assert!(matches!(num_array, AgentValue::Array(_)));
1874 if let AgentValue::Array(arr) = num_array {
1875 assert_eq!(arr.len(), 3);
1876 assert_eq!(arr[0], AgentValue::Number(1.1));
1877 assert_eq!(arr[1], AgentValue::Number(2.2));
1878 assert_eq!(arr[2], AgentValue::Number(3.3));
1879 }
1880
1881 let string_array = AgentValue::from_kind_json("string", json!(["hello", "world"])).unwrap();
1882 assert!(matches!(string_array, AgentValue::Array(_)));
1883 if let AgentValue::Array(arr) = string_array {
1884 assert_eq!(arr.len(), 2);
1885 assert!(matches!(&arr[0], AgentValue::String(_)));
1886 if let AgentValue::String(s) = &arr[0] {
1887 assert_eq!(**s, "hello".to_string());
1888 }
1889 assert!(matches!(&arr[1], AgentValue::String(_)));
1890 if let AgentValue::String(s) = &arr[1] {
1891 assert_eq!(**s, "world".to_string());
1892 }
1893 }
1894
1895 let text_array = AgentValue::from_kind_json("text", json!(["hello", "world!\n"])).unwrap();
1896 assert!(matches!(text_array, AgentValue::Array(_)));
1897 if let AgentValue::Array(arr) = text_array {
1898 assert_eq!(arr.len(), 2);
1899 assert!(matches!(&arr[0], AgentValue::String(_)));
1900 if let AgentValue::String(s) = &arr[0] {
1901 assert_eq!(**s, "hello".to_string());
1902 }
1903 assert!(matches!(&arr[1], AgentValue::String(_)));
1904 if let AgentValue::String(s) = &arr[1] {
1905 assert_eq!(**s, "world!\n".to_string());
1906 }
1907 }
1908
1909 }
1913
1914 #[test]
1915 fn test_agent_value_test_methods() {
1916 let unit = AgentValue::unit();
1918 assert_eq!(unit.is_unit(), true);
1919 assert_eq!(unit.is_boolean(), false);
1920 assert_eq!(unit.is_integer(), false);
1921 assert_eq!(unit.is_number(), false);
1922 assert_eq!(unit.is_string(), false);
1923 assert_eq!(unit.is_array(), false);
1924 assert_eq!(unit.is_object(), false);
1925
1926 let boolean = AgentValue::boolean(true);
1927 assert_eq!(boolean.is_unit(), false);
1928 assert_eq!(boolean.is_boolean(), true);
1929 assert_eq!(boolean.is_integer(), false);
1930 assert_eq!(boolean.is_number(), false);
1931 assert_eq!(boolean.is_string(), false);
1932 assert_eq!(boolean.is_array(), false);
1933 assert_eq!(boolean.is_object(), false);
1934
1935 let integer = AgentValue::integer(42);
1936 assert_eq!(integer.is_unit(), false);
1937 assert_eq!(integer.is_boolean(), false);
1938 assert_eq!(integer.is_integer(), true);
1939 assert_eq!(integer.is_number(), false);
1940 assert_eq!(integer.is_string(), false);
1941 assert_eq!(integer.is_array(), false);
1942 assert_eq!(integer.is_object(), false);
1943
1944 let number = AgentValue::number(3.14);
1945 assert_eq!(number.is_unit(), false);
1946 assert_eq!(number.is_boolean(), false);
1947 assert_eq!(number.is_integer(), false);
1948 assert_eq!(number.is_number(), true);
1949 assert_eq!(number.is_string(), false);
1950 assert_eq!(number.is_array(), false);
1951 assert_eq!(number.is_object(), false);
1952
1953 let string = AgentValue::string("hello");
1954 assert_eq!(string.is_unit(), false);
1955 assert_eq!(string.is_boolean(), false);
1956 assert_eq!(string.is_integer(), false);
1957 assert_eq!(string.is_number(), false);
1958 assert_eq!(string.is_string(), true);
1959 assert_eq!(string.is_array(), false);
1960 assert_eq!(string.is_object(), false);
1961
1962 let array = AgentValue::array(vec![AgentValue::integer(1), AgentValue::integer(2)]);
1963 assert_eq!(array.is_unit(), false);
1964 assert_eq!(array.is_boolean(), false);
1965 assert_eq!(array.is_integer(), false);
1966 assert_eq!(array.is_number(), false);
1967 assert_eq!(array.is_string(), false);
1968 assert_eq!(array.is_array(), true);
1969 assert_eq!(array.is_object(), false);
1970
1971 let obj = AgentValue::object(
1972 [
1973 ("key1".to_string(), AgentValue::string("string1")),
1974 ("key2".to_string(), AgentValue::integer(2)),
1975 ]
1976 .into(),
1977 );
1978 assert_eq!(obj.is_unit(), false);
1979 assert_eq!(obj.is_boolean(), false);
1980 assert_eq!(obj.is_integer(), false);
1981 assert_eq!(obj.is_number(), false);
1982 assert_eq!(obj.is_string(), false);
1983 assert_eq!(obj.is_array(), false);
1984 assert_eq!(obj.is_object(), true);
1985 }
1986
1987 #[test]
1988 fn test_agent_value_accessor_methods() {
1989 let boolean = AgentValue::boolean(true);
1991 assert_eq!(boolean.as_bool(), Some(true));
1992 assert_eq!(boolean.as_i64(), None);
1993 assert_eq!(boolean.as_f64(), None);
1994 assert_eq!(boolean.as_str(), None);
1995 assert!(boolean.as_array().is_none());
1996 assert_eq!(boolean.as_object(), None);
1997
1998 let integer = AgentValue::integer(42);
1999 assert_eq!(integer.as_bool(), None);
2000 assert_eq!(integer.as_i64(), Some(42));
2001 assert_eq!(integer.as_f64(), Some(42.0));
2002 assert_eq!(integer.as_str(), None);
2003 assert!(integer.as_array().is_none());
2004 assert_eq!(integer.as_object(), None);
2005
2006 let number = AgentValue::number(3.14);
2007 assert_eq!(number.as_bool(), None);
2008 assert_eq!(number.as_i64(), Some(3)); assert_eq!(number.as_f64().unwrap(), 3.14);
2010 assert_eq!(number.as_str(), None);
2011 assert!(number.as_array().is_none());
2012 assert_eq!(number.as_object(), None);
2013
2014 let string = AgentValue::string("hello");
2015 assert_eq!(string.as_bool(), None);
2016 assert_eq!(string.as_i64(), None);
2017 assert_eq!(string.as_f64(), None);
2018 assert_eq!(string.as_str(), Some("hello"));
2019 assert!(string.as_array().is_none());
2020 assert_eq!(string.as_object(), None);
2021
2022 let array = AgentValue::array(vec![AgentValue::integer(1), AgentValue::integer(2)]);
2023 assert_eq!(array.as_bool(), None);
2024 assert_eq!(array.as_i64(), None);
2025 assert_eq!(array.as_f64(), None);
2026 assert_eq!(array.as_str(), None);
2027 assert!(array.as_array().is_some());
2028 if let Some(arr) = array.as_array() {
2029 assert_eq!(arr.len(), 2);
2030 assert_eq!(arr[0].as_i64().unwrap(), 1);
2031 assert_eq!(arr[1].as_i64().unwrap(), 2);
2032 }
2033 assert_eq!(array.as_object(), None);
2034
2035 let obj = AgentValue::object(
2036 [
2037 ("key1".to_string(), AgentValue::string("string1")),
2038 ("key2".to_string(), AgentValue::integer(2)),
2039 ]
2040 .into(),
2041 );
2042 assert_eq!(obj.as_bool(), None);
2043 assert_eq!(obj.as_i64(), None);
2044 assert_eq!(obj.as_f64(), None);
2045 assert_eq!(obj.as_str(), None);
2046 assert!(obj.as_array().is_none());
2047 assert!(obj.as_object().is_some());
2048 if let Some(value) = obj.as_object() {
2049 assert_eq!(value.get("key1").and_then(|v| v.as_str()), Some("string1"));
2050 assert_eq!(value.get("key2").and_then(|v| v.as_i64()), Some(2));
2051 }
2052 }
2053
2054 #[test]
2055 fn test_agent_value_default() {
2056 assert_eq!(AgentValue::default(), AgentValue::Null);
2057 }
2058
2059 #[test]
2060 fn test_agent_value_serialization() {
2061 {
2063 let null = AgentValue::Null;
2064 assert_eq!(serde_json::to_string(&null).unwrap(), "null");
2065 }
2066
2067 {
2069 let boolean_t = AgentValue::boolean(true);
2070 assert_eq!(serde_json::to_string(&boolean_t).unwrap(), "true");
2071
2072 let boolean_f = AgentValue::boolean(false);
2073 assert_eq!(serde_json::to_string(&boolean_f).unwrap(), "false");
2074 }
2075
2076 {
2078 let integer = AgentValue::integer(42);
2079 assert_eq!(serde_json::to_string(&integer).unwrap(), "42");
2080 }
2081
2082 {
2084 let num = AgentValue::number(3.14);
2085 assert_eq!(serde_json::to_string(&num).unwrap(), "3.14");
2086
2087 let num = AgentValue::number(3.0);
2088 assert_eq!(serde_json::to_string(&num).unwrap(), "3.0");
2089 }
2090
2091 {
2093 let s = AgentValue::string("Hello, world!");
2094 assert_eq!(serde_json::to_string(&s).unwrap(), "\"Hello, world!\"");
2095
2096 let s = AgentValue::string("hello\nworld\n\n");
2097 assert_eq!(serde_json::to_string(&s).unwrap(), r#""hello\nworld\n\n""#);
2098 }
2099
2100 {
2111 let array = AgentValue::array(vec![
2112 AgentValue::integer(1),
2113 AgentValue::string("test"),
2114 AgentValue::object(
2115 [
2116 ("key1".to_string(), AgentValue::string("test")),
2117 ("key2".to_string(), AgentValue::integer(2)),
2118 ]
2119 .into(),
2120 ),
2121 ]);
2122 assert_eq!(
2123 serde_json::to_string(&array).unwrap(),
2124 r#"[1,"test",{"key1":"test","key2":2}]"#
2125 );
2126 }
2127
2128 {
2130 let obj = AgentValue::object(
2131 [
2132 ("key1".to_string(), AgentValue::string("test")),
2133 ("key2".to_string(), AgentValue::integer(3)),
2134 ]
2135 .into(),
2136 );
2137 assert_eq!(
2138 serde_json::to_string(&obj).unwrap(),
2139 r#"{"key1":"test","key2":3}"#
2140 );
2141 }
2142 }
2143
2144 #[test]
2145 fn test_agent_value_deserialization() {
2146 {
2148 let deserialized: AgentValue = serde_json::from_str("null").unwrap();
2149 assert_eq!(deserialized, AgentValue::Null);
2150 }
2151
2152 {
2154 let deserialized: AgentValue = serde_json::from_str("false").unwrap();
2155 assert_eq!(deserialized, AgentValue::boolean(false));
2156
2157 let deserialized: AgentValue = serde_json::from_str("true").unwrap();
2158 assert_eq!(deserialized, AgentValue::boolean(true));
2159 }
2160
2161 {
2163 let deserialized: AgentValue = serde_json::from_str("123").unwrap();
2164 assert_eq!(deserialized, AgentValue::integer(123));
2165 }
2166
2167 {
2169 let deserialized: AgentValue = serde_json::from_str("3.14").unwrap();
2170 assert_eq!(deserialized, AgentValue::number(3.14));
2171
2172 let deserialized: AgentValue = serde_json::from_str("3.0").unwrap();
2173 assert_eq!(deserialized, AgentValue::number(3.0));
2174 }
2175
2176 {
2178 let deserialized: AgentValue = serde_json::from_str("\"Hello, world!\"").unwrap();
2179 assert_eq!(deserialized, AgentValue::string("Hello, world!"));
2180
2181 let deserialized: AgentValue = serde_json::from_str(r#""hello\nworld\n\n""#).unwrap();
2182 assert_eq!(deserialized, AgentValue::string("hello\nworld\n\n"));
2183 }
2184
2185 {
2196 let deserialized: AgentValue =
2197 serde_json::from_str(r#"[1,"test",{"key1":"test","key2":2}]"#).unwrap();
2198 assert!(matches!(deserialized, AgentValue::Array(_)));
2199 if let AgentValue::Array(arr) = deserialized {
2200 assert_eq!(arr.len(), 3, "Array length mismatch after serialization");
2201 assert_eq!(arr[0], AgentValue::integer(1));
2202 assert_eq!(arr[1], AgentValue::string("test"));
2203 assert_eq!(
2204 arr[2],
2205 AgentValue::object(
2206 [
2207 ("key1".to_string(), AgentValue::string("test")),
2208 ("key2".to_string(), AgentValue::integer(2)),
2209 ]
2210 .into()
2211 )
2212 );
2213 }
2214 }
2215
2216 {
2218 let deserialized: AgentValue =
2219 serde_json::from_str(r#"{"key1":"test","key2":3}"#).unwrap();
2220 assert_eq!(
2221 deserialized,
2222 AgentValue::object(
2223 [
2224 ("key1".to_string(), AgentValue::string("test")),
2225 ("key2".to_string(), AgentValue::integer(3)),
2226 ]
2227 .into()
2228 )
2229 );
2230 }
2231 }
2232
2233 #[test]
2234 fn test_serialize_deserialize_roundtrip() {
2235 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2236 struct TestStruct {
2237 name: String,
2238 age: i64,
2239 active: bool,
2240 }
2241
2242 let test_data = TestStruct {
2243 name: "Alice".to_string(),
2244 age: 30,
2245 active: true,
2246 };
2247
2248 let agent_data = AgentData::from_serialize(&test_data).unwrap();
2250 assert_eq!(agent_data.kind, "object");
2251 assert_eq!(agent_data.get_str("name"), Some("Alice"));
2252 assert_eq!(agent_data.get_i64("age"), Some(30));
2253 assert_eq!(agent_data.get_bool("active"), Some(true));
2254
2255 let restored: TestStruct = agent_data.to_deserialize().unwrap();
2256 assert_eq!(restored, test_data);
2257
2258 let agent_data_custom = AgentData::from_serialize_with_kind("person", &test_data).unwrap();
2260 assert_eq!(agent_data_custom.kind, "person");
2261 let restored_custom: TestStruct = agent_data_custom.to_deserialize().unwrap();
2262 assert_eq!(restored_custom, test_data);
2263
2264 let agent_value = AgentValue::from_serialize(&test_data).unwrap();
2266 assert!(agent_value.is_object());
2267 assert_eq!(agent_value.get_str("name"), Some("Alice"));
2268
2269 let restored_value: TestStruct = agent_value.to_deserialize().unwrap();
2270 assert_eq!(restored_value, test_data);
2271 }
2272
2273 #[test]
2274 fn test_serialize_deserialize_nested() {
2275 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2276 struct Address {
2277 street: String,
2278 city: String,
2279 zip: String,
2280 }
2281
2282 #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
2283 struct Person {
2284 name: String,
2285 age: i64,
2286 address: Address,
2287 tags: Vec<String>,
2288 }
2289
2290 let person = Person {
2291 name: "Bob".to_string(),
2292 age: 25,
2293 address: Address {
2294 street: "123 Main St".to_string(),
2295 city: "Springfield".to_string(),
2296 zip: "12345".to_string(),
2297 },
2298 tags: vec!["developer".to_string(), "rust".to_string()],
2299 };
2300
2301 let agent_data = AgentData::from_serialize(&person).unwrap();
2303 assert_eq!(agent_data.kind, "object");
2304 assert_eq!(agent_data.get_str("name"), Some("Bob"));
2305
2306 let address = agent_data.get_object("address").unwrap();
2307 assert_eq!(
2308 address.get("city").and_then(|v| v.as_str()),
2309 Some("Springfield")
2310 );
2311
2312 let tags = agent_data.get_array("tags").unwrap();
2313 assert_eq!(tags.len(), 2);
2314 assert_eq!(tags[0].as_str(), Some("developer"));
2315
2316 let restored: Person = agent_data.to_deserialize().unwrap();
2317 assert_eq!(restored, person);
2318 }
2319}