strands_agents/types/
traces.rs

1//! Tracing type definitions for the SDK.
2
3use std::collections::HashMap;
4
5use serde::{Deserialize, Serialize};
6
7/// Attribute value for tracing.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(untagged)]
10pub enum AttributeValue {
11    String(String),
12    Bool(bool),
13    Float(f64),
14    Int(i64),
15    StringList(Vec<String>),
16    BoolList(Vec<bool>),
17    FloatList(Vec<f64>),
18    IntList(Vec<i64>),
19}
20
21impl From<String> for AttributeValue {
22    fn from(s: String) -> Self {
23        Self::String(s)
24    }
25}
26
27impl From<&str> for AttributeValue {
28    fn from(s: &str) -> Self {
29        Self::String(s.to_string())
30    }
31}
32
33impl From<bool> for AttributeValue {
34    fn from(b: bool) -> Self {
35        Self::Bool(b)
36    }
37}
38
39impl From<f64> for AttributeValue {
40    fn from(f: f64) -> Self {
41        Self::Float(f)
42    }
43}
44
45impl From<i64> for AttributeValue {
46    fn from(i: i64) -> Self {
47        Self::Int(i)
48    }
49}
50
51impl From<i32> for AttributeValue {
52    fn from(i: i32) -> Self {
53        Self::Int(i as i64)
54    }
55}
56
57impl From<Vec<String>> for AttributeValue {
58    fn from(v: Vec<String>) -> Self {
59        Self::StringList(v)
60    }
61}
62
63impl From<Vec<bool>> for AttributeValue {
64    fn from(v: Vec<bool>) -> Self {
65        Self::BoolList(v)
66    }
67}
68
69impl From<Vec<f64>> for AttributeValue {
70    fn from(v: Vec<f64>) -> Self {
71        Self::FloatList(v)
72    }
73}
74
75impl From<Vec<i64>> for AttributeValue {
76    fn from(v: Vec<i64>) -> Self {
77        Self::IntList(v)
78    }
79}
80
81/// Attributes mapping for tracing.
82pub type Attributes = Option<HashMap<String, AttributeValue>>;
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn test_attribute_value_from_string() {
90        let attr: AttributeValue = "test".into();
91        assert!(matches!(attr, AttributeValue::String(_)));
92    }
93
94    #[test]
95    fn test_attribute_value_from_bool() {
96        let attr: AttributeValue = true.into();
97        assert!(matches!(attr, AttributeValue::Bool(true)));
98    }
99
100    #[test]
101    fn test_attribute_value_from_int() {
102        let attr: AttributeValue = 42i64.into();
103        assert!(matches!(attr, AttributeValue::Int(42)));
104    }
105
106    #[test]
107    fn test_attributes_type() {
108        let mut attrs: Attributes = Some(HashMap::new());
109        if let Some(ref mut map) = attrs {
110            map.insert("key".to_string(), "value".into());
111        }
112        assert!(attrs.is_some());
113    }
114}
115