Skip to main content

source_vmt/
proxies.rs

1use indexmap::IndexMap;
2use crate::vmt::Vmt;
3use source_kv::Value;
4
5#[derive(Debug, Clone)]
6pub struct Proxy {
7    pub name: String,
8    pub params: IndexMap<String, String>,
9}
10
11impl Proxy {
12    /// Get a proxy parameter by key, case-insensitively.
13    pub fn get_param(&self, key: &str) -> Option<&String> {
14        let lower = key.to_lowercase();
15        self.params.get(lower.as_str())
16    }
17}
18
19impl Vmt {
20    /// Extracts and flattens all proxies into an ordered list.
21    pub fn proxies(&self) -> Vec<Proxy> {
22        let mut result = Vec::new();
23
24        if let Some(Value::Obj(proxies_map)) = self.get_raw("proxies") {
25            for (proxy_name, instances) in proxies_map {
26                for instance in instances {
27                    if let Value::Obj(params_map) = instance {
28                        let mut params = IndexMap::new();
29                        for (k, v) in params_map {
30                            if let Some(Value::Str(s)) = v.first() {
31                                params.insert(k.to_lowercase(), s.clone());
32                            }
33                        }
34                        result.push(Proxy {
35                            name: proxy_name.clone(),
36                            params
37                        });
38                    }
39                }
40            }
41        }
42        result
43    }
44}