Can now add database to store.

This commit is contained in:
Jeff Baskin 2023-06-23 08:30:49 -04:00
parent a23b5d467e
commit d90dc3b9fc
3 changed files with 37 additions and 3 deletions

View File

@ -2,7 +2,7 @@
pub struct Database; pub struct Database;
impl Database { impl Database {
fn new() -> Self { pub fn new() -> Self {
Self {} Self {}
} }
} }

View File

@ -33,7 +33,7 @@ pub enum FromCache {
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct Data<D> { pub struct Data<D> {
id: Option<String>, id: Option<String>,
data: Option<D>, data: Option<D>,
} }
@ -48,6 +48,13 @@ impl<D> Data<D> {
data: None, data: None,
} }
} }
fn from_data(data: D) -> Self {
Self {
id: None,
data: Some(data),
}
}
} }
#[derive(Clone)] #[derive(Clone)]

View File

@ -13,6 +13,15 @@ impl Store {
} }
} }
pub fn add(&mut self, name: &str) {
let storage = Data::from_data(Database::new());
self.data.insert(name.to_string(), storage);
}
pub fn get(&self, name: &str) -> Option<&Data<Database>> {
self.data.get(name)
}
pub fn list(&self) -> Vec<String> { pub fn list(&self) -> Vec<String> {
Vec::new() Vec::new()
} }
@ -20,7 +29,7 @@ impl Store {
#[cfg(test)] #[cfg(test)]
mod storage { mod storage {
use super::*; use super::{super::MTTError, *};
#[test] #[test]
fn create_new() { fn create_new() {
@ -28,4 +37,22 @@ mod storage {
let expected: Vec<String> = Vec::new(); let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected); assert_eq!(store.list(), expected);
} }
#[test]
fn add_database() {
let mut store = Store::new();
let name = "Melvin";
store.add(name);
let output = store.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn get_bad_database() -> Result<(), MTTError> {
let store = Store::new();
match store.get("missing") {
Some(_) => Err(MTTError::new("Should have returned None.")),
None => Ok(()),
}
}
} }