agent_stream_kit/
config.rs

1use std::collections::{BTreeMap, HashMap};
2
3use serde::{Deserialize, Serialize};
4
5use super::data::AgentValue;
6
7pub type AgentConfigs = HashMap<String, AgentConfig>;
8
9#[derive(Debug, Default, Serialize, Deserialize, Clone)]
10pub struct AgentConfig(BTreeMap<String, AgentValue>);
11
12impl AgentConfig {
13    pub fn new() -> Self {
14        Self(BTreeMap::new())
15    }
16
17    pub fn set(&mut self, key: String, value: AgentValue) {
18        self.0.insert(key, value);
19    }
20
21    pub fn contains_key(&self, key: &str) -> bool {
22        self.0.contains_key(key)
23    }
24
25    pub fn get(&self, key: &str) -> Option<&AgentValue> {
26        self.0.get(key)
27    }
28
29    pub fn get_bool(&self, key: &str) -> Option<bool> {
30        self.0.get(key).and_then(|v| v.as_bool())
31    }
32
33    pub fn get_bool_or(&self, key: &str, default: bool) -> bool {
34        self.get_bool(key).unwrap_or(default)
35    }
36
37    pub fn get_bool_or_default(&self, key: &str) -> bool {
38        self.get_bool(key).unwrap_or_default()
39    }
40
41    pub fn get_integer(&self, key: &str) -> Option<i64> {
42        self.0.get(key).and_then(|v| v.as_i64())
43    }
44
45    pub fn get_integer_or(&self, key: &str, default: i64) -> i64 {
46        self.get_integer(key).unwrap_or(default)
47    }
48
49    pub fn get_integer_or_default(&self, key: &str) -> i64 {
50        self.get_integer(key).unwrap_or_default()
51    }
52
53    pub fn get_number(&self, key: &str) -> Option<f64> {
54        self.0.get(key).and_then(|v| v.as_f64())
55    }
56
57    pub fn get_number_or(&self, key: &str, default: f64) -> f64 {
58        self.get_number(key).unwrap_or(default)
59    }
60
61    pub fn get_number_or_default(&self, key: &str) -> f64 {
62        self.get_number(key).unwrap_or_default()
63    }
64
65    pub fn get_string(&self, key: &str) -> Option<String> {
66        self.0
67            .get(key)
68            .and_then(|v| v.as_str())
69            .map(|v| v.to_string())
70    }
71
72    pub fn get_string_or(&self, key: &str, default: impl Into<String>) -> String {
73        self.get_string(key).unwrap_or_else(|| default.into())
74    }
75
76    pub fn get_string_or_default(&self, key: &str) -> String {
77        self.get_string(key).unwrap_or_default()
78    }
79
80    pub fn get_array(&self, key: &str) -> Option<&Vec<AgentValue>> {
81        self.0.get(key).and_then(|v| v.as_array())
82    }
83
84    pub fn get_array_or<'a>(
85        &'a self,
86        key: &str,
87        default: &'a Vec<AgentValue>,
88    ) -> &'a Vec<AgentValue> {
89        self.get_array(key).unwrap_or(default)
90    }
91
92    pub fn get_object(&self, key: &str) -> Option<&BTreeMap<String, AgentValue>> {
93        self.0.get(key).and_then(|v| v.as_object())
94    }
95
96    // pub fn get_object_or<'a>(
97    //     &'a self,
98    //     key: &str,
99    //     default: &'a BTreeMap<String, AgentValue>,
100    // ) -> &'a BTreeMap<String, AgentValue> {
101    //     self.get_object(key).unwrap_or(default)
102    // }
103}
104
105impl IntoIterator for AgentConfig {
106    type Item = (String, AgentValue);
107    type IntoIter = std::collections::btree_map::IntoIter<String, AgentValue>;
108
109    fn into_iter(self) -> Self::IntoIter {
110        self.0.into_iter()
111    }
112}
113
114impl<'a> IntoIterator for &'a AgentConfig {
115    type Item = (&'a String, &'a AgentValue);
116    type IntoIter = std::collections::btree_map::Iter<'a, String, AgentValue>;
117
118    fn into_iter(self) -> Self::IntoIter {
119        self.0.iter()
120    }
121}