Skip to content

Commit

Permalink
feat : Implementing config for part of services (#4344)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuRage-git authored Mar 13, 2024
1 parent 9086fcc commit 02437f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
28 changes: 20 additions & 8 deletions core/src/services/memory/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

/// In memory service support. (BTreeMap Based)
#[doc = include_str!("docs.md")]
#[derive(Default)]
pub struct MemoryBuilder {
root: Option<String>,
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
}
}
Expand All @@ -46,19 +59,18 @@ impl Builder for MemoryBuilder {
type Accessor = MemoryBackend;

fn from_map(map: HashMap<String, String>) -> 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<Self::Accessor> {
let adapter = Adapter {
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()))
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/services/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

mod backend;
pub use backend::MemoryBuilder as Memory;
pub use backend::MemoryConfig;
4 changes: 3 additions & 1 deletion core/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 02437f8

Please sign in to comment.