From 60dae69788be4dd4626f933e901d87403adde974 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 4 Feb 2023 14:29:07 -0500 Subject: [PATCH] Began abstracting functions for database sessions. --- src/morethantext/databases.rs | 76 ++++++++++++++++++++++++++++++++++- src/morethantext/mod.rs | 6 +++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/morethantext/databases.rs b/src/morethantext/databases.rs index 5aa4759..1f4f722 100644 --- a/src/morethantext/databases.rs +++ b/src/morethantext/databases.rs @@ -1,4 +1,4 @@ -use super::{DBError, FileData}; +use super::{DBError, FileData, SessionData}; use std::{collections::HashMap, slice, str}; #[derive(Clone)] @@ -13,6 +13,14 @@ impl Databases { } } + fn test_key(key: &str) -> Result<(), DBError> { + match key { + "name" => (), + _ => return Err(DBError::new(format!("databases do not have a {}", key))), + } + Ok(()) + } + fn add_database(&mut self, name: &str, id: &str) -> Result { if name.len() == 0 { return Err(DBError::new("database names cannot be empty")); @@ -91,6 +99,34 @@ impl FileData for Databases { } } +impl SessionData for Databases { + fn add(&mut self, key: &str, value: &str, data: &str) -> Result, DBError> { + match Self::test_key(key) { + Ok(_) => (), + Err(err) => return Err(err), + } + self.db_map.insert(value.to_string(), data.to_string()); + let mut output = Vec::new(); + output.push(data.to_string()); + Ok(output) + } + + fn get(&self, key: &str, value: &str) -> Result, DBError> { + match Self::test_key(key) { + Ok(_) => (), + Err(err) => return Err(err), + } + let mut output = Vec::new(); + let data = self.db_map.get(value).unwrap(); + output.push(data.to_string()); + Ok(output) + } + + fn show(&self, keys: Vec<&str>) -> Result, DBError> { + Ok(Vec::new()) + } +} + #[cfg(test)] mod functions { use super::*; @@ -151,7 +187,7 @@ mod functions { } #[cfg(test)] -mod filedata { +mod file_data { use super::*; use std::error::Error; @@ -236,3 +272,39 @@ mod filedata { } } } + +#[cfg(test)] +mod session_data { + use super::*; + + #[test] + fn add_new() { + let mut dbs = Databases::new(); + let key = "name"; + let value = "marvin"; + let data = "123456"; + assert_eq!(dbs.add(key, value, data).unwrap(), [data]); + let output = dbs.get(key, value).unwrap(); + assert_eq!(output, [data]); + } + + #[test] + fn add_bad_key() { + let mut dbs = Databases::new(); + let key = "sdgfjksg"; + match dbs.add(key, "fred", "barney") { + Ok(_) => assert!(false, "Bad keys should produce an error."), + Err(err) => assert_eq!(err.to_string(), format!("databases do not have a {}", key)), + } + } + + #[test] + fn get_bad_key() { + let dbs = Databases::new(); + let key = "bvdfgert"; + match dbs.get(key, "fred") { + Ok(_) => assert!(false, "Bad keys should produce an error."), + Err(_) => (), + } + } +} diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 1bf1fa2..4206263 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -24,6 +24,12 @@ trait FileData { fn from_bytes(data: &mut slice::Iter) -> Result; } +trait SessionData { + fn add(&mut self, key: &str, value: &str, data: &str) -> Result, DBError>; + fn get(&self, key: &str, value: &str) -> Result, DBError>; + fn show(&self, keys: Vec<&str>) -> Result, DBError>; +} + #[derive(Clone)] pub enum CacheType { Raw(String),