diff --git a/core/src/services/memory/backend.rs b/core/src/services/memory/backend.rs index 6549d0dbe880..03c59ee297dc 100644 --- a/core/src/services/memory/backend.rs +++ b/core/src/services/memory/backend.rs @@ -26,17 +26,30 @@ use async_trait::async_trait; use crate::raw::adapters::typed_kv; use crate::*; +use serde::Deserialize; + +use self::raw::ConfigDeserializer; + +///Config for memory. +#[derive(Default, Deserialize)] +#[serde(default)] +#[non_exhaustive] +pub struct MemoryConfig { + ///root of the backend. + pub root: Option, +} + /// In memory service support. (BTreeMap Based) #[doc = include_str!("docs.md")] #[derive(Default)] pub struct MemoryBuilder { - root: Option, + config: MemoryConfig, } impl MemoryBuilder { /// Set the root for BTreeMap. pub fn root(&mut self, path: &str) -> &mut Self { - self.root = Some(path.into()); + self.config.root = Some(path.into()); self } } @@ -46,11 +59,10 @@ impl Builder for MemoryBuilder { type Accessor = MemoryBackend; fn from_map(map: HashMap) -> Self { - let mut builder = Self::default(); - - map.get("root").map(|v| builder.root(v)); - - builder + MemoryBuilder { + config: MemoryConfig::deserialize(ConfigDeserializer::new(map)) + .expect("config deserialize must succeed"), + } } fn build(&mut self) -> Result { @@ -58,7 +70,7 @@ impl Builder for MemoryBuilder { inner: Arc::new(Mutex::new(BTreeMap::default())), }; - Ok(MemoryBackend::new(adapter).with_root(self.root.as_deref().unwrap_or_default())) + Ok(MemoryBackend::new(adapter).with_root(self.config.root.as_deref().unwrap_or_default())) } } diff --git a/core/src/services/memory/mod.rs b/core/src/services/memory/mod.rs index 2539e665860e..6ba9c2bac330 100644 --- a/core/src/services/memory/mod.rs +++ b/core/src/services/memory/mod.rs @@ -17,3 +17,4 @@ mod backend; pub use backend::MemoryBuilder as Memory; +pub use backend::MemoryConfig; diff --git a/core/src/services/mod.rs b/core/src/services/mod.rs index d9d2917cd297..780dab54f4d0 100644 --- a/core/src/services/mod.rs +++ b/core/src/services/mod.rs @@ -135,7 +135,9 @@ pub use memcached::MemcachedConfig; #[cfg(feature = "services-memory")] mod memory; #[cfg(feature = "services-memory")] -pub use memory::Memory; +pub use self::memory::Memory; +#[cfg(feature = "services-memory")] +pub use self::memory::MemoryConfig; #[cfg(feature = "services-mini-moka")] mod mini_moka;