opendal/services/etcd/
config.rs1use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use super::backend::EtcdBuilder;
22use serde::Deserialize;
23use serde::Serialize;
24
25#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct EtcdConfig {
30    pub endpoints: Option<String>,
36    pub username: Option<String>,
40    pub password: Option<String>,
44    pub root: Option<String>,
48    pub ca_path: Option<String>,
52    pub cert_path: Option<String>,
56    pub key_path: Option<String>,
60}
61
62impl Debug for EtcdConfig {
63    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64        let mut ds = f.debug_struct("EtcdConfig");
65
66        ds.field("root", &self.root);
67        if let Some(endpoints) = self.endpoints.clone() {
68            ds.field("endpoints", &endpoints);
69        }
70        if let Some(username) = self.username.clone() {
71            ds.field("username", &username);
72        }
73        if self.password.is_some() {
74            ds.field("password", &"<redacted>");
75        }
76        if let Some(ca_path) = self.ca_path.clone() {
77            ds.field("ca_path", &ca_path);
78        }
79        if let Some(cert_path) = self.cert_path.clone() {
80            ds.field("cert_path", &cert_path);
81        }
82        if let Some(key_path) = self.key_path.clone() {
83            ds.field("key_path", &key_path);
84        }
85        ds.finish()
86    }
87}
88
89impl crate::Configurator for EtcdConfig {
90    type Builder = EtcdBuilder;
91    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
92        let mut map = uri.options().clone();
93
94        if let Some(authority) = uri.authority() {
95            map.entry("endpoints".to_string())
96                .or_insert_with(|| format!("http://{authority}"));
97        }
98
99        if let Some(root) = uri.root() {
100            if !root.is_empty() {
101                map.insert("root".to_string(), root.to_string());
102            }
103        }
104
105        Self::from_iter(map)
106    }
107
108    fn into_builder(self) -> Self::Builder {
109        EtcdBuilder { config: self }
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use crate::Configurator;
117    use crate::types::OperatorUri;
118
119    #[test]
120    fn from_uri_sets_endpoints_and_root() {
121        let uri = OperatorUri::new(
122            "etcd://127.0.0.1:2379/app/config",
123            Vec::<(String, String)>::new(),
124        )
125        .unwrap();
126
127        let cfg = EtcdConfig::from_uri(&uri).unwrap();
128        assert_eq!(cfg.endpoints.as_deref(), Some("http://127.0.0.1:2379"));
129        assert_eq!(cfg.root.as_deref(), Some("app/config"));
130    }
131}