2022-07-20 09:31:21 -04:00
|
|
|
/*
|
2022-07-12 16:55:20 -04:00
|
|
|
use async_std::sync::{Arc, RwLock};
|
2022-07-13 00:14:00 -04:00
|
|
|
use std::{collections::HashMap, error::Error, fmt, str::FromStr};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DBError {
|
|
|
|
detail: String,
|
|
|
|
source: Option<Box<DBError>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DBError {
|
|
|
|
fn new(detail: String) -> Self {
|
|
|
|
Self {
|
|
|
|
detail: detail.to_string(),
|
|
|
|
source: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DBError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", &self.detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for DBError {
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
match &self.source {
|
|
|
|
Some(err) => Some(err),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 16:55:20 -04:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq)]
|
2022-07-13 00:14:00 -04:00
|
|
|
pub enum FieldType {
|
2022-07-12 16:55:20 -04:00
|
|
|
Table,
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:14:00 -04:00
|
|
|
impl fmt::Display for FieldType {
|
2022-07-12 16:55:20 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
2022-07-13 00:14:00 -04:00
|
|
|
FieldType::Table => write!(f, "table"),
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:14:00 -04:00
|
|
|
impl FromStr for FieldType {
|
|
|
|
type Err = DBError;
|
|
|
|
|
|
|
|
fn from_str(input: &str) -> Result<FieldType, Self::Err> {
|
2022-07-12 16:55:20 -04:00
|
|
|
match input {
|
2022-07-13 00:14:00 -04:00
|
|
|
"table" => Ok(FieldType::Table),
|
|
|
|
_ => Err(DBError::new(format!("field type {} does not exist", input))),
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Table {
|
2022-07-13 00:14:00 -04:00
|
|
|
fields: Arc<RwLock<HashMap<String, FieldType>>>,
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Table {
|
|
|
|
pub async fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
fields: Arc::new(RwLock::new(HashMap::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:31:21 -04:00
|
|
|
pub async fn update_field(&self, name: &str, ftype: &str) -> Result<(), Box<dyn Error>> {
|
2022-07-13 00:14:00 -04:00
|
|
|
let ftype = match FieldType::from_str(ftype) {
|
|
|
|
Ok(field) => field,
|
|
|
|
Err(err) => {
|
|
|
|
let mut error = DBError::new(format!("failed to add field {}", name));
|
|
|
|
error.source = Some(Box::new(err));
|
|
|
|
return Err(Box::new(error));
|
|
|
|
}
|
|
|
|
};
|
2022-07-12 16:55:20 -04:00
|
|
|
let mut fmap = self.fields.write().await;
|
2022-07-13 00:14:00 -04:00
|
|
|
match fmap.get(name) {
|
|
|
|
Some(_) => Err(Box::new(DBError::new(format!(
|
|
|
|
"field {} already exists",
|
|
|
|
name
|
|
|
|
)))),
|
|
|
|
None => {
|
|
|
|
fmap.insert(name.to_string(), ftype);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
|
2022-07-13 00:14:00 -04:00
|
|
|
pub async fn fields(&self) -> HashMap<String, FieldType> {
|
2022-07-12 16:55:20 -04:00
|
|
|
let fmap = self.fields.read().await;
|
|
|
|
fmap.clone()
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 09:31:21 -04:00
|
|
|
*/
|
|
|
|
use async_std::sync::{Arc, RwLock};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
mod fieldtype;
|
|
|
|
|
|
|
|
use fieldtype::{FieldType, StaticString};
|
2022-07-12 16:55:20 -04:00
|
|
|
|
2022-07-18 17:24:45 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MoreThanText;
|
2022-07-12 16:55:20 -04:00
|
|
|
|
2022-07-18 17:24:45 -04:00
|
|
|
impl MoreThanText {
|
2022-07-12 16:55:20 -04:00
|
|
|
pub async fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:31:21 -04:00
|
|
|
pub async fn add_table(&self, name: &str) -> Table {
|
|
|
|
Table::new()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_table(&self, name: &str) -> Table {
|
|
|
|
Table::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
struct FieldDef;
|
|
|
|
|
|
|
|
pub struct Table {
|
|
|
|
fields: Arc<RwLock<HashMap<String, FieldDef>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Table {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
fields: Arc::new(RwLock::new(HashMap::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn add_field(&self, name: &str) {
|
|
|
|
let mut field_defs = self.fields.write().await;
|
|
|
|
field_defs.insert(name.to_string(), FieldDef {});
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_field(&self, name: &str) -> Option<FieldDef> {
|
|
|
|
let field_defs = self.fields.read().await;
|
|
|
|
match field_defs.get(name) {
|
|
|
|
Some(def) => Some(def.clone()),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Record {
|
|
|
|
data: Arc<RwLock<HashMap<String, FieldType>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Record {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
data: Arc::new(RwLock::new(HashMap::new())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_field(&self, name: String, data: FieldType) {
|
|
|
|
let mut map = self.data.write().await;
|
|
|
|
map.insert(name, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_field(&self, name: &str) -> Option<FieldType> {
|
|
|
|
let map = self.data.read().await;
|
|
|
|
match map.get(name) {
|
|
|
|
Some(field) => Some(field.clone()),
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod databases {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn new_database() {
|
|
|
|
MoreThanText::new().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn add_table() {
|
|
|
|
let db = MoreThanText::new().await;
|
|
|
|
let name = "table";
|
|
|
|
db.add_table(name).await;
|
|
|
|
db.get_table(name).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tables {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_table() {
|
|
|
|
Table::new();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn add_field_definition() {
|
|
|
|
let tbl = Table::new();
|
|
|
|
let name = "field";
|
|
|
|
let expected = FieldDef {};
|
|
|
|
tbl.add_field(name).await;
|
|
|
|
let output = tbl.get_field(name).await.unwrap();
|
|
|
|
assert!(output == expected, "Did not return a field definition.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn missing_field_definition() {
|
|
|
|
let tbl = Table::new();
|
|
|
|
let output = tbl.get_field("missing").await;
|
|
|
|
assert!(
|
|
|
|
output == None,
|
|
|
|
"Should return None if field does not exist."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod records {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn update_fields() {
|
|
|
|
let rec = Record::new();
|
|
|
|
let name = "elephant";
|
|
|
|
let data = "Something to remember.";
|
|
|
|
let sstr = StaticString::new(data);
|
|
|
|
rec.update_field(name.to_string(), sstr).await;
|
|
|
|
let output = rec.get_field(name).await.unwrap();
|
|
|
|
assert!(
|
|
|
|
output.to_string() == data,
|
|
|
|
"\n\nGot: {}\nWant: {}\n\n",
|
|
|
|
output.to_string(),
|
|
|
|
data
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn empty_field() {
|
|
|
|
let rec = Record::new();
|
|
|
|
let name = "mull";
|
|
|
|
let output = rec.get_field(name).await;
|
|
|
|
assert!(output == None, "Should return an option.");
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:31:21 -04:00
|
|
|
/*
|
2022-07-12 16:55:20 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tables {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn new_table() {
|
|
|
|
Table::new().await;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
2022-07-20 09:31:21 -04:00
|
|
|
async fn update_field() {
|
2022-07-12 16:55:20 -04:00
|
|
|
let table = Table::new().await;
|
2022-07-13 00:14:00 -04:00
|
|
|
let mut expected: HashMap<String, FieldType> = HashMap::new();
|
|
|
|
expected.insert("stan".to_string(), FieldType::Table);
|
|
|
|
expected.insert("lee".to_string(), FieldType::Table);
|
2022-07-20 09:31:21 -04:00
|
|
|
table.update_field("stan", "table").await.unwrap();
|
|
|
|
table.update_field("lee", "table").await.unwrap();
|
2022-07-12 16:55:20 -04:00
|
|
|
let output = table.fields().await;
|
|
|
|
assert!(output == expected, "Table did not get the fields added.");
|
|
|
|
}
|
2022-07-13 00:14:00 -04:00
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn add_bad_field() -> Result<(), String> {
|
|
|
|
let table = Table::new().await;
|
|
|
|
let name = "failure";
|
|
|
|
let bad_type = "ljksdbtt";
|
|
|
|
let expected = format!("failed to add field {}", name);
|
|
|
|
let source = format!("field type {} does not exist", bad_type);
|
2022-07-20 09:31:21 -04:00
|
|
|
match table.update_field(name, bad_type).await {
|
2022-07-13 00:14:00 -04:00
|
|
|
Ok(_) => Err("A bad field type should not return successfully".to_string()),
|
|
|
|
Err(err) => {
|
|
|
|
if format!("{}", err) != expected {
|
|
|
|
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
|
|
|
} else if format!("{}", err.source().unwrap()) != source {
|
|
|
|
Err(format!(
|
|
|
|
"Got: '{}' - Want: '{}'",
|
|
|
|
err.source().unwrap(),
|
|
|
|
source
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn add_duplicate_field() -> Result<(), String> {
|
|
|
|
let table = Table::new().await;
|
|
|
|
let name = "twice";
|
|
|
|
let expected = format!("field {} already exists", name);
|
2022-07-20 09:31:21 -04:00
|
|
|
table.update_field(name, "table").await.unwrap();
|
|
|
|
match table.update_field(name, "table").await {
|
2022-07-13 00:14:00 -04:00
|
|
|
Ok(_) => Err(format!("Cannot have two fields with named '{}'", name)),
|
|
|
|
Err(err) => {
|
|
|
|
if format!("{}", err) == expected {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod databases {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn new_database() {
|
2022-07-18 17:24:45 -04:00
|
|
|
MoreThanText::new().await;
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn add_table() {
|
2022-07-18 17:24:45 -04:00
|
|
|
let db = MoreThanText::new().await;
|
2022-07-12 16:55:20 -04:00
|
|
|
db.add_table("fred".to_string()).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2022-07-13 00:14:00 -04:00
|
|
|
mod fieldtypes {
|
2022-07-12 16:55:20 -04:00
|
|
|
use super::*;
|
|
|
|
|
2022-07-13 00:14:00 -04:00
|
|
|
fn get_field_map() -> HashMap<String, FieldType> {
|
|
|
|
let mut fields: HashMap<String, FieldType> = HashMap::new();
|
|
|
|
fields.insert("table".to_string(), FieldType::Table);
|
2022-07-12 16:55:20 -04:00
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn convert_to_string() {
|
|
|
|
for (key, value) in get_field_map().iter() {
|
2022-07-13 00:14:00 -04:00
|
|
|
assert!(
|
|
|
|
key == &value.to_string(),
|
|
|
|
"\n\nGot: {}\nWant: {}\n\n",
|
|
|
|
value.to_string(),
|
|
|
|
key
|
|
|
|
);
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn convert_from_string() {
|
|
|
|
for (key, value) in get_field_map().iter() {
|
2022-07-13 00:14:00 -04:00
|
|
|
assert!(
|
|
|
|
&FieldType::from_str(key).unwrap() == value,
|
|
|
|
"\n\nDid not return a FieldType::{}",
|
|
|
|
key
|
|
|
|
);
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn convert_from_string_error() -> Result<(), String> {
|
2022-07-13 00:14:00 -04:00
|
|
|
let ftype = "lkjsdfh";
|
|
|
|
let expected = format!("field type {} does not exist", ftype);
|
|
|
|
match FieldType::from_str(ftype) {
|
|
|
|
Ok(_) => Err(format!("Found field type {}", ftype)),
|
|
|
|
Err(err) => {
|
|
|
|
if format!("{}", err) == expected {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(format!("Got: '{}' - Want: '{}'", err, expected))
|
|
|
|
}
|
|
|
|
}
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 09:31:21 -04:00
|
|
|
*/
|