opendal/services/memcached/
config.rs1use std::fmt::Debug;
19use std::time::Duration;
20
21use serde::Deserialize;
22use serde::Serialize;
23
24use super::MEMCACHED_SCHEME;
25use super::backend::MemcachedBuilder;
26
27#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
29#[serde(default)]
30#[non_exhaustive]
31pub struct MemcachedConfig {
32 pub endpoint: Option<String>,
36 pub root: Option<String>,
40 pub username: Option<String>,
42 pub password: Option<String>,
44 pub default_ttl: Option<Duration>,
46 pub connection_pool_max_size: Option<u32>,
50}
51
52impl Debug for MemcachedConfig {
53 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54 f.debug_struct("MemcachedConfig")
55 .field("endpoint", &self.endpoint)
56 .field("root", &self.root)
57 .field("username", &self.username)
58 .field("default_ttl", &self.default_ttl)
59 .finish_non_exhaustive()
60 }
61}
62
63impl crate::Configurator for MemcachedConfig {
64 type Builder = MemcachedBuilder;
65
66 fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
67 let authority = uri.authority().ok_or_else(|| {
68 crate::Error::new(crate::ErrorKind::ConfigInvalid, "uri authority is required")
69 .with_context("service", MEMCACHED_SCHEME)
70 })?;
71
72 let mut map = uri.options().clone();
73 map.insert("endpoint".to_string(), format!("tcp://{authority}"));
74
75 if let Some(root) = uri.root() {
76 if !root.is_empty() {
77 map.insert("root".to_string(), root.to_string());
78 }
79 }
80
81 Self::from_iter(map)
82 }
83
84 fn into_builder(self) -> Self::Builder {
85 MemcachedBuilder { config: self }
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 use crate::Configurator;
93 use crate::types::OperatorUri;
94
95 #[test]
96 fn from_uri_sets_endpoint_and_root() {
97 let uri = OperatorUri::new(
98 "memcached://cache.local:11211/app/session",
99 Vec::<(String, String)>::new(),
100 )
101 .unwrap();
102
103 let cfg = MemcachedConfig::from_uri(&uri).unwrap();
104 assert_eq!(cfg.endpoint.as_deref(), Some("tcp://cache.local:11211"));
105 assert_eq!(cfg.root.as_deref(), Some("app/session"));
106 }
107}