Added databases structure.
This commit is contained in:
parent
cef9843720
commit
878e5277bf
81
src/morethantext/databases.rs
Normal file
81
src/morethantext/databases.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Databases {
|
||||
db_map: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl Databases {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
db_map: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_database(&mut self, name: &str, id: &str) -> Option<String> {
|
||||
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<String> {
|
||||
self.db_map.get(name).cloned()
|
||||
}
|
||||
|
||||
fn show(&self) -> Vec<String> {
|
||||
let mut names: Vec<String> = 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);
|
||||
}
|
||||
}
|
@ -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::*;
|
||||
|
Loading…
Reference in New Issue
Block a user