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<Duration>,
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 { config: self }
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn from_uri_sets_endpoint_and_root() -> Result<()> {
94 let uri = OperatorUri::new(
95 "memcached://cache.local:11211/app/session",
96 Vec::<(String, String)>::new(),
97 )?;
98
99 let cfg = MemcachedConfig::from_uri(&uri)?;
100 assert_eq!(cfg.endpoint.as_deref(), Some("tcp://cache.local:11211"));
101 assert_eq!(cfg.root.as_deref(), Some("app/session"));
102 Ok(())
103 }
104}