Skip to content

Commit

Permalink
Update backend.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuRage-git authored Mar 12, 2024
1 parent afb05fd commit c6032d6
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions core/src/services/memory/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,32 @@ 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 +61,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

0 comments on commit c6032d6

Please sign in to comment.