From 878e5277bf4681029716fceaab3b422148a574fb Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 21 Jan 2023 21:46:39 -0500 Subject: [PATCH] Added databases structure. --- src/morethantext/databases.rs | 81 +++++++++++++++++++++++++++++++++++ src/morethantext/mod.rs | 26 +---------- 2 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 src/morethantext/databases.rs diff --git a/src/morethantext/databases.rs b/src/morethantext/databases.rs new file mode 100644 index 0000000..6089d18 --- /dev/null +++ b/src/morethantext/databases.rs @@ -0,0 +1,81 @@ +use std::collections::HashMap; + +#[derive(Clone)] +pub struct Databases { + db_map: HashMap, +} + +impl Databases { + pub fn new() -> Self { + Self { + db_map: HashMap::new(), + } + } + + fn add_database(&mut self, name: &str, id: &str) -> Option { + if self.db_map.contains_key(name) { + None + } else { + self.db_map.insert(name.to_string(), id.to_string()); + Some(id.to_string()) + } + } + + fn get_database(&self, name: &str) -> Option { + self.db_map.get(name).cloned() + } + + fn show(&self) -> Vec { + let mut names: Vec = self.db_map.clone().into_keys().collect(); + names.sort(); + names + } +} + +#[cfg(test)] +mod functions { + use super::*; + + #[test] + fn add_entry() { + let name = "fred"; + let id = "123456"; + let mut dbs = Databases::new(); + let output = dbs.add_database(name, id); + assert_eq!(output, Some(id.to_string())); + let output = dbs.get_database(name); + assert_eq!(output, Some(id.to_string())); + } + + #[test] + fn entry_cannot_be_over_written() { + let name = "barney"; + let id = "abcde"; + let mut dbs = Databases::new(); + dbs.add_database(name, id); + let output = dbs.add_database(name, "09876"); + assert_eq!(output, None); + let output = dbs.get_database(name); + assert_eq!(output, Some(id.to_string())); + } + + #[test] + fn get_bad_database() { + let dbs = Databases::new(); + let output = dbs.get_database("missing"); + assert_eq!(output, None); + } + + #[test] + fn list_databases() { + let mut dbs = Databases::new(); + dbs.add_database("zebra", "a"); + dbs.add_database("alpha", "a"); + dbs.add_database("charlie", "a"); + dbs.add_database("wilma", "a"); + dbs.add_database("frank", "a"); + let expected = ["alpha", "charlie", "frank", "wilma", "zebra"]; + let output = dbs.show(); + assert_eq!(output, expected); + } +} diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 228bdfa..44660f0 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -1,3 +1,4 @@ +mod databases; pub mod error; use async_std::{ @@ -6,6 +7,7 @@ use async_std::{ sync::{Arc, Mutex}, task::{sleep, spawn}, }; +use databases::Databases; use error::DBError; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use std::{ @@ -17,18 +19,6 @@ use std::{ const DATA: &str = "data"; const ENTRY: &str = "databases"; -#[derive(Clone)] -pub struct Databases; - -impl Databases { - pub fn new() -> Self { - Self {} - } - - fn add_database(&self, _name: &str) { - } -} - #[derive(Clone)] pub enum CacheType { Raw(String), @@ -658,18 +648,6 @@ mod cache_entry { } } -#[cfg(test)] -mod databases { - use super::*; - - #[test] - fn add_database() { - let dbs = Databases::new(); - let name = "db1"; - dbs.add_database(name); - } -} - #[cfg(test)] mod enum_ctype { use super::*;