use std::{
collections::{HashMap, hash_map::Iter},
path::PathBuf,
};
use crate::{DsError, Result, config::SpaceTreeId};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct DataBase {
entries: HashMap<String, Space>,
}
impl DataBase {
pub fn get_space(&self, space: &str) -> Result<&Space> {
self.entries
.get(space)
.ok_or_else(|| DsError::SpaceNotFound(space.to_string()))
}
pub fn get_space_mut(&mut self, space: &str) -> Result<&mut Space> {
self.entries
.get_mut(space)
.ok_or_else(|| DsError::SpaceNotFound(space.to_string()))
}
pub fn insert(&mut self, key: String, space: Space) {
self.entries.insert(key, space);
}
pub fn spaces_iter(&self) -> Iter<'_, String, Space> {
self.entries.iter()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn remove(&mut self, key: &str) {
self.entries.remove(key);
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Space {
pub wdir: PathBuf,
#[serde(rename = "tree")]
pub tree: SpaceTreeId,
}
impl Space {
pub fn new(wdir: PathBuf, tree: SpaceTreeId) -> Space {
Space { wdir, tree }
}
}