agent_stream_kit/
config.rs

1use std::collections::{BTreeMap, HashMap};
2
3use serde::{Deserialize, Serialize};
4
5use crate::data::AgentValue;
6use crate::error::AgentError;
7
8pub type AgentConfigsMap = HashMap<String, AgentConfigs>;
9
10#[derive(Debug, Default, Serialize, Deserialize, Clone)]
11pub struct AgentConfigs(BTreeMap<String, AgentValue>);
12
13impl AgentConfigs {
14    pub fn new() -> Self {
15        Self(BTreeMap::new())
16    }
17
18    pub fn set(&mut self, key: String, value: AgentValue) {
19        self.0.insert(key, value);
20    }
21
22    pub fn contains_key(&self, key: &str) -> bool {
23        self.0.contains_key(key)
24    }
25
26    pub fn get(&self, key: &str) -> Result<&AgentValue, AgentError> {
27        self.0
28            .get(key)
29            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
30    }
31
32    pub fn get_bool(&self, key: &str) -> Result<bool, AgentError> {
33        self.0
34            .get(key)
35            .and_then(|v| v.as_bool())
36            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
37    }
38
39    pub fn get_bool_or(&self, key: &str, default: bool) -> bool {
40        self.get_bool(key).unwrap_or(default)
41    }
42
43    pub fn get_bool_or_default(&self, key: &str) -> bool {
44        self.get_bool(key).unwrap_or_default()
45    }
46
47    pub fn get_integer(&self, key: &str) -> Result<i64, AgentError> {
48        self.0
49            .get(key)
50            .and_then(|v| v.as_i64())
51            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
52    }
53
54    pub fn get_integer_or(&self, key: &str, default: i64) -> i64 {
55        self.get_integer(key).unwrap_or(default)
56    }
57
58    pub fn get_integer_or_default(&self, key: &str) -> i64 {
59        self.get_integer(key).unwrap_or_default()
60    }
61
62    pub fn get_number(&self, key: &str) -> Result<f64, AgentError> {
63        self.0
64            .get(key)
65            .and_then(|v| v.as_f64())
66            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
67    }
68
69    pub fn get_number_or(&self, key: &str, default: f64) -> f64 {
70        self.get_number(key).unwrap_or(default)
71    }
72
73    pub fn get_number_or_default(&self, key: &str) -> f64 {
74        self.get_number(key).unwrap_or_default()
75    }
76
77    pub fn get_string(&self, key: &str) -> Result<String, AgentError> {
78        self.0
79            .get(key)
80            .and_then(|v| v.as_str())
81            .map(|v| v.to_string())
82            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
83    }
84
85    pub fn get_string_or(&self, key: &str, default: impl Into<String>) -> String {
86        self.0
87            .get(key)
88            .and_then(|v| v.as_str())
89            .map(|v| v.to_string())
90            .unwrap_or(default.into())
91    }
92
93    pub fn get_string_or_default(&self, key: &str) -> String {
94        self.0
95            .get(key)
96            .and_then(|v| v.as_str())
97            .map(|v| v.to_string())
98            .unwrap_or_default()
99    }
100
101    pub fn get_array(&self, key: &str) -> Result<&Vec<AgentValue>, AgentError> {
102        self.0
103            .get(key)
104            .and_then(|v| v.as_array())
105            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
106    }
107
108    pub fn get_array_or<'a>(
109        &'a self,
110        key: &str,
111        default: &'a Vec<AgentValue>,
112    ) -> &'a Vec<AgentValue> {
113        self.0
114            .get(key)
115            .and_then(|v| v.as_array())
116            .unwrap_or(default)
117    }
118
119    pub fn get_array_or_default(&self, key: &str) -> Vec<AgentValue> {
120        self.0
121            .get(key)
122            .and_then(|v| v.as_array())
123            .cloned()
124            .unwrap_or_default()
125    }
126
127    pub fn get_object(&self, key: &str) -> Result<&BTreeMap<String, AgentValue>, AgentError> {
128        self.0
129            .get(key)
130            .and_then(|v| v.as_object())
131            .ok_or_else(|| AgentError::UnknownConfig(key.to_string()))
132    }
133
134    pub fn get_object_or<'a>(
135        &'a self,
136        key: &str,
137        default: &'a BTreeMap<String, AgentValue>,
138    ) -> &'a BTreeMap<String, AgentValue> {
139        self.0
140            .get(key)
141            .and_then(|v| v.as_object())
142            .unwrap_or(default)
143    }
144
145    pub fn get_object_or_default(&self, key: &str) -> BTreeMap<String, AgentValue> {
146        self.0
147            .get(key)
148            .and_then(|v| v.as_object())
149            .cloned()
150            .unwrap_or_default()
151    }
152}
153
154impl IntoIterator for AgentConfigs {
155    type Item = (String, AgentValue);
156    type IntoIter = std::collections::btree_map::IntoIter<String, AgentValue>;
157
158    fn into_iter(self) -> Self::IntoIter {
159        self.0.into_iter()
160    }
161}
162
163impl<'a> IntoIterator for &'a AgentConfigs {
164    type Item = (&'a String, &'a AgentValue);
165    type IntoIter = std::collections::btree_map::Iter<'a, String, AgentValue>;
166
167    fn into_iter(self) -> Self::IntoIter {
168        self.0.iter()
169    }
170}