Began abstracting functions for database sessions.

This commit is contained in:
Jeff Baskin 2023-02-04 14:29:07 -05:00
parent 77c0f9f189
commit 60dae69788
2 changed files with 80 additions and 2 deletions

View File

@ -1,4 +1,4 @@
use super::{DBError, FileData}; use super::{DBError, FileData, SessionData};
use std::{collections::HashMap, slice, str}; use std::{collections::HashMap, slice, str};
#[derive(Clone)] #[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<String, DBError> { fn add_database(&mut self, name: &str, id: &str) -> Result<String, DBError> {
if name.len() == 0 { if name.len() == 0 {
return Err(DBError::new("database names cannot be empty")); return Err(DBError::new("database names cannot be empty"));
@ -91,6 +99,34 @@ impl FileData<Self> for Databases {
} }
} }
impl SessionData for Databases {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, 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<Vec<String>, 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<Vec<String>, DBError> {
Ok(Vec::new())
}
}
#[cfg(test)] #[cfg(test)]
mod functions { mod functions {
use super::*; use super::*;
@ -151,7 +187,7 @@ mod functions {
} }
#[cfg(test)] #[cfg(test)]
mod filedata { mod file_data {
use super::*; use super::*;
use std::error::Error; 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(_) => (),
}
}
}

View File

@ -24,6 +24,12 @@ trait FileData<F> {
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>; fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>;
} }
trait SessionData {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>;
fn get(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>;
fn show(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
}
#[derive(Clone)] #[derive(Clone)]
pub enum CacheType { pub enum CacheType {
Raw(String), Raw(String),