Still working on getting database to store session key.
This commit is contained in:
parent
620e5593e0
commit
ca64717cea
50
src/morethantext/fieldtype.rs
Normal file
50
src/morethantext/fieldtype.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum FieldType {
|
||||
StaticString(StaticString),
|
||||
}
|
||||
|
||||
impl fmt::Display for FieldType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
FieldType::StaticString(data) => write!(f, "{}", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct StaticString {
|
||||
data: String,
|
||||
}
|
||||
|
||||
impl StaticString {
|
||||
pub fn new(data: &str) -> FieldType {
|
||||
FieldType::StaticString(Self {
|
||||
data: data.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for StaticString {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", &self.data)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod staticstrings {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn create_static_string() {
|
||||
let data = "fred";
|
||||
let field = StaticString::new(data);
|
||||
assert!(
|
||||
field.to_string() == data,
|
||||
"\n\nGot: {}\nWant: {}\n\n",
|
||||
field.to_string(),
|
||||
data
|
||||
);
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
/*
|
||||
use async_std::sync::{Arc, RwLock};
|
||||
use std::{collections::HashMap, error::Error, fmt, str::FromStr};
|
||||
|
||||
@ -66,7 +67,7 @@ impl Table {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_field(&self, name: &str, ftype: &str) -> Result<(), Box<dyn Error>> {
|
||||
pub async fn update_field(&self, name: &str, ftype: &str) -> Result<(), Box<dyn Error>> {
|
||||
let ftype = match FieldType::from_str(ftype) {
|
||||
Ok(field) => field,
|
||||
Err(err) => {
|
||||
@ -93,6 +94,13 @@ impl Table {
|
||||
fmap.clone()
|
||||
}
|
||||
}
|
||||
*/
|
||||
use async_std::sync::{Arc, RwLock};
|
||||
use std::collections::HashMap;
|
||||
|
||||
mod fieldtype;
|
||||
|
||||
use fieldtype::{FieldType, StaticString};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MoreThanText;
|
||||
@ -102,11 +110,145 @@ impl MoreThanText {
|
||||
Self {}
|
||||
}
|
||||
|
||||
pub async fn add_table(&self, _name: String) -> Table {
|
||||
Table::new().await
|
||||
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.");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[cfg(test)]
|
||||
mod tables {
|
||||
use super::*;
|
||||
@ -117,13 +259,13 @@ mod tables {
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn add_field() {
|
||||
async fn update_field() {
|
||||
let table = Table::new().await;
|
||||
let mut expected: HashMap<String, FieldType> = HashMap::new();
|
||||
expected.insert("stan".to_string(), FieldType::Table);
|
||||
expected.insert("lee".to_string(), FieldType::Table);
|
||||
table.add_field("stan", "table").await.unwrap();
|
||||
table.add_field("lee", "table").await.unwrap();
|
||||
table.update_field("stan", "table").await.unwrap();
|
||||
table.update_field("lee", "table").await.unwrap();
|
||||
let output = table.fields().await;
|
||||
assert!(output == expected, "Table did not get the fields added.");
|
||||
}
|
||||
@ -135,7 +277,7 @@ mod tables {
|
||||
let bad_type = "ljksdbtt";
|
||||
let expected = format!("failed to add field {}", name);
|
||||
let source = format!("field type {} does not exist", bad_type);
|
||||
match table.add_field(name, bad_type).await {
|
||||
match table.update_field(name, bad_type).await {
|
||||
Ok(_) => Err("A bad field type should not return successfully".to_string()),
|
||||
Err(err) => {
|
||||
if format!("{}", err) != expected {
|
||||
@ -158,8 +300,8 @@ mod tables {
|
||||
let table = Table::new().await;
|
||||
let name = "twice";
|
||||
let expected = format!("field {} already exists", name);
|
||||
table.add_field(name, "table").await.unwrap();
|
||||
match table.add_field(name, "table").await {
|
||||
table.update_field(name, "table").await.unwrap();
|
||||
match table.update_field(name, "table").await {
|
||||
Ok(_) => Err(format!("Cannot have two fields with named '{}'", name)),
|
||||
Err(err) => {
|
||||
if format!("{}", err) == expected {
|
||||
@ -237,3 +379,4 @@ mod fieldtypes {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user