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