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