From d90dc3b9fc39966fda96bc16a1dff32da4659daf Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 23 Jun 2023 08:30:49 -0400 Subject: [PATCH] Can now add database to store. --- src/morethantext/database.rs | 2 +- src/morethantext/mod.rs | 9 ++++++++- src/morethantext/store.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/morethantext/database.rs b/src/morethantext/database.rs index d771296..460be4d 100644 --- a/src/morethantext/database.rs +++ b/src/morethantext/database.rs @@ -2,7 +2,7 @@ pub struct Database; impl Database { - fn new() -> Self { + pub fn new() -> Self { Self {} } } diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index e6f229f..2cefbf9 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -33,7 +33,7 @@ pub enum FromCache { } #[derive(Clone, Debug)] -struct Data { +pub struct Data { id: Option, data: Option, } @@ -48,6 +48,13 @@ impl Data { data: None, } } + + fn from_data(data: D) -> Self { + Self { + id: None, + data: Some(data), + } + } } #[derive(Clone)] diff --git a/src/morethantext/store.rs b/src/morethantext/store.rs index f619f13..25c7cf4 100644 --- a/src/morethantext/store.rs +++ b/src/morethantext/store.rs @@ -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> { + self.data.get(name) + } + pub fn list(&self) -> Vec { Vec::new() } @@ -20,7 +29,7 @@ impl Store { #[cfg(test)] mod storage { - use super::*; + use super::{super::MTTError, *}; #[test] fn create_new() { @@ -28,4 +37,22 @@ mod storage { let expected: Vec = Vec::new(); 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(()), + } + } }