opendal_service_memcached/
config.rs1use std::fmt::Debug;
19
20use opendal_core::Configurator;
21use opendal_core::OperatorUri;
22use opendal_core::Result;
23use opendal_core::raw::*;
24use serde::Deserialize;
25use serde::Serialize;
26
27use super::backend::MemcachedBuilder;
28
29#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
31#[serde(default)]
32#[non_exhaustive]
33pub struct MemcachedConfig {
34 pub endpoint: Option<String>,
38 pub root: Option<String>,
42 pub username: Option<String>,
44 pub password: Option<String>,
46 pub default_ttl: Option<SignedDuration>,
48 pub connection_pool_max_size: Option<usize>,
52}
53
54impl Debug for MemcachedConfig {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56 f.debug_struct("MemcachedConfig")
57 .field("endpoint", &self.endpoint)
58 .field("root", &self.root)
59 .field("username", &self.username)
60 .field("default_ttl", &self.default_ttl)
61 .finish_non_exhaustive()
62 }
63}
64
65impl Configurator for MemcachedConfig {
66 type Builder = MemcachedBuilder;
67
68 fn from_uri(uri: &OperatorUri) -> Result<Self> {
69 let mut map = uri.options().clone();
70 if let Some(authority) = uri.authority() {
71 map.insert("endpoint".to_string(), format!("tcp://{authority}"));
72 }
73
74 if let Some(root) = uri.root()
75 && !root.is_empty()
76 {
77 map.insert("root".to_string(), root.to_string());
78 }
79
80 Self::from_iter(map)
81 }
82
83 fn into_builder(self) -> Self::Builder {
84 MemcachedBuilder {
85 config: self,
86 default_ttl: None,
87 }
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn from_uri_sets_endpoint_and_root() -> Result<()> {
97 let uri = OperatorUri::new(
98 "memcached://cache.local:11211/app/session",
99 Vec::<(String, String)>::new(),
100 )?;
101
102 let cfg = MemcachedConfig::from_uri(&uri)?;
103 assert_eq!(cfg.endpoint.as_deref(), Some("tcp://cache.local:11211"));
104 assert_eq!(cfg.root.as_deref(), Some("app/session"));
105 Ok(())
106 }
107
108 #[test]
109 fn from_iter_parses_default_ttl() -> Result<()> {
110 let cfg = MemcachedConfig::from_iter([("default_ttl".to_string(), "1500ms".to_string())])?;
111
112 assert_eq!(cfg.default_ttl, Some(SignedDuration::from_millis(1500)));
113 Ok(())
114 }
115}