Got tabler holding a record.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit

This commit is contained in:
Jeff Baskin 2024-11-25 09:12:31 -05:00
parent 508b8269d0
commit e1aec8de28
4 changed files with 251 additions and 107 deletions

View File

@ -1,4 +1,7 @@
use crate::data::record::{Field, FieldRecord}; use crate::{
data::record::Field,
error::{ErrorType, MTTError},
};
use std::fmt; use std::fmt;
use uuid::Uuid; use uuid::Uuid;
@ -8,20 +11,33 @@ pub struct ID {
} }
impl ID { impl ID {
pub fn new() -> Self { pub fn new(data: Uuid) -> Self {
Self { data: data }
}
pub fn random() -> Self {
Self { Self {
data: Uuid::new_v4(), data: Uuid::new_v4(),
} }
} }
} }
impl FieldRecord for ID { impl TryFrom<Uuid> for ID {
fn new_field() -> Field { type Error = MTTError;
Field::ID(ID::new())
}
fn get_type() -> String { fn try_from(value: Uuid) -> Result<Self, Self::Error> {
"id".to_string() Ok(Self::new(value))
}
}
impl TryFrom<&str> for ID {
type Error = MTTError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match Uuid::try_from(value) {
Ok(id) => Ok(ID::new(id)),
Err(_) => Err(MTTError::new(ErrorType::FieldIDInvalid(value.to_string()))),
}
} }
} }
@ -31,42 +47,52 @@ impl fmt::Display for ID {
} }
} }
#[cfg(test)]
mod fielddata {
use super::*;
#[test]
fn new_ids_are_unique() {
let mut ids: Vec<String> = Vec::new();
for _ in 1..10 {
match ID::new_field() {
Field::ID(id) => {
let id_string = id.to_string();
assert!(!ids.contains(&id_string), "'{}' repeated", id_string);
ids.push(id_string);
}
}
}
}
#[test]
fn get_type() {
assert_eq!(ID::get_type(), "id");
}
}
#[cfg(test)] #[cfg(test)]
mod id { mod id {
use super::*; use super::*;
#[test] #[test]
fn id_are_unique() { fn try_from_uuid() {
let data = Uuid::new_v4();
let id = ID::try_from(data.clone()).unwrap();
assert_eq!(id.to_string(), data.to_string());
}
#[test]
fn try_from_str() {
let holder = Uuid::new_v4().to_string();
let data = holder.as_str();
let id = ID::try_from(data).unwrap();
assert_eq!(id.to_string(), data);
}
#[test]
fn try_from_bad_str() {
let bad = "BadUuid";
match ID::try_from(bad) {
Ok(_) => unreachable!("Should have failed to create an ID"),
Err(err) => match err.get_code() {
ErrorType::FieldIDInvalid(id) => assert_eq!(id, bad),
_ => unreachable!("Returned wrong error"),
},
}
}
#[test]
fn create_new() {
let data = Uuid::new_v4();
let id = ID::new(data.clone());
assert_eq!(id.to_string(), data.to_string());
}
#[test]
fn random_is_unique() {
let mut ids: Vec<String> = Vec::new(); let mut ids: Vec<String> = Vec::new();
for _ in 1..10 { for _ in 0..10 {
let id = ID::new(); let id = ID::random();
let id_string = id.to_string(); let holder = id.to_string();
assert!(!ids.contains(&id_string), "'{}' repeated", id_string); assert!(!ids.contains(&holder), "'{}' is a duplicate entry", id);
ids.push(id_string); ids.push(holder);
} }
} }
} }

View File

@ -1,15 +1,29 @@
mod id; mod id;
mod record; mod record;
use crate::data::record::Record; use crate::{
data::{
id::ID,
record::{Field, Record},
},
error::{ErrorType, MTTError},
};
use std::collections::HashMap; use std::collections::HashMap;
enum FieldType {
ID,
}
struct FieldDef; struct FieldDef;
impl FieldDef { impl FieldDef {
fn new() -> Self { fn new() -> Self {
Self {} Self {}
} }
fn get_type(&self) -> FieldType {
FieldType::ID
}
} }
struct Table { struct Table {
@ -23,19 +37,35 @@ impl Table {
} }
} }
fn len(&self) -> usize { fn description(&self) -> &HashMap<String, FieldDef> {
self.fields.len() &self.fields
} }
fn add_field(&mut self, name: &str, field_type: &str) -> Result<(), String> { fn add_field(&mut self, name: &str, field_type: FieldType) -> Result<(), MTTError> {
match self.fields.get(name) { match self.fields.get(name) {
Some(_) => Err("duplicate field name".to_string()), Some(_) => {
let err = ErrorType::TableAddFieldDuplicate(name.to_string());
Err(MTTError::new(err))
}
None => { None => {
self.fields.insert(name.to_string(), FieldDef::new()); self.fields.insert(name.to_string(), FieldDef::new());
Ok(()) Ok(())
} }
} }
} }
fn add_record(&mut self, rec: Record) -> Result<Record, MTTError> {
for (key, _) in rec.iter() {
match self.fields.get(key) {
Some(_) => {}
None => {
let err = ErrorType::TableRecordInvalidFieldName(key.to_string());
return Err(MTTError::new(err));
}
}
}
Ok(rec)
}
} }
#[cfg(test)] #[cfg(test)]
@ -45,23 +75,72 @@ mod table {
#[test] #[test]
fn new_table() { fn new_table() {
let tbl = Table::new(); let tbl = Table::new();
assert_eq!(tbl.len(), 0); let data = tbl.description();
assert_eq!(data.len(), 0);
} }
#[test] #[test]
fn add_field() { fn add_field() {
let mut tbl = Table::new(); let mut tbl = Table::new();
tbl.add_field("one", "id"); let field_name = "something ";
assert_eq!(tbl.len(), 1); tbl.add_field(field_name, FieldType::ID);
let data = tbl.description();
assert_eq!(data.len(), 1);
match data.get(field_name) {
Some(field_info) => match field_info.get_type() {
FieldType::ID => {}
_ => unreachable!("incorrect field type"),
},
None => unreachable!("should return field definition"),
}
} }
#[test] #[test]
fn error_on_duplicate_name() { fn error_on_duplicate_name() {
let mut tbl = Table::new(); let mut tbl = Table::new();
tbl.add_field("one", "id"); let name = "one";
match tbl.add_field("one", "id") { tbl.add_field(name, FieldType::ID);
match tbl.add_field(name, FieldType::ID) {
Ok(_) => unreachable!(" Should not duplicates."), Ok(_) => unreachable!(" Should not duplicates."),
Err(_) => {} Err(err) => match err.get_code() {
ErrorType::TableAddFieldDuplicate(result) => assert_eq!(result, name),
_ => unreachable!("should produce a duplicate name error"),
},
}
}
#[test]
fn add_record() {
let mut tbl = Table::new();
let name = "id";
let field_data = ID::random();
tbl.add_field(name, FieldType::ID).unwrap();
let mut data: HashMap<String, Field> = HashMap::new();
data.insert(name.to_string(), field_data.clone().into());
let rec = Record::new(data);
let result = tbl.add_record(rec).unwrap();
assert_eq!(
result.get(name).unwrap().to_string(),
field_data.to_string()
);
}
#[test]
fn add_record_incorrect_field_name() {
let mut tbl = Table::new();
let name = "id";
let field_data = ID::random();
let mut data: HashMap<String, Field> = HashMap::new();
data.insert(name.to_string(), field_data.clone().into());
let rec = Record::new(data);
match tbl.add_record(rec) {
Ok(_) => unreachable!("should have produced an error"),
Err(err) => match err.get_code() {
ErrorType::TableRecordInvalidFieldName(result) => {
assert_eq!(result, name);
}
_ => unreachable!("should have been invalid name error"),
},
} }
} }
} }

View File

@ -1,40 +1,14 @@
use crate::data::id::ID; use crate::data::id::ID;
use std::{collections::HashMap, fmt}; use std::{collections::HashMap, fmt, ops::Deref};
#[derive(Clone)] #[derive(Clone)]
pub enum Field { pub enum Field {
ID(ID), ID(ID),
} }
impl Field { impl From<ID> for Field {
fn new(field_type: &str) -> Field { fn from(value: ID) -> Self {
ID::new_field() Field::ID(value)
}
}
pub trait FieldRecord {
fn new_field() -> Field;
fn get_type() -> String;
}
pub struct Record {
data: HashMap<String, Field>,
}
impl Record {
fn new(data: HashMap<String, Field>) -> Self {
Self { data: data }
}
fn len(&self) -> usize {
self.data.len()
}
fn get(&self, name: &str) -> Field {
match self.data.get(name) {
Some(data) => data.clone(),
None => unreachable!(),
}
} }
} }
@ -46,16 +20,33 @@ impl fmt::Display for Field {
} }
} }
pub struct Record {
data: HashMap<String, Field>,
}
impl Record {
pub fn new(data: HashMap<String, Field>) -> Self {
Self { data: data }
}
}
impl Deref for Record {
type Target = HashMap<String, Field>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
#[cfg(test)] #[cfg(test)]
mod fields { mod fields {
use super::*; use super::*;
#[test] #[test]
fn creaAte_new_id() { fn from_id() {
match Field::new("id") { let id = ID::random();
Field::ID(_) => {} let field = Field::from(id.clone());
_ => unreachable!("Fould should be an ID type."), assert_eq!(field.to_string(), id.to_string());
}
} }
} }
@ -71,18 +62,17 @@ mod records {
} }
#[test] #[test]
fn new_record_more_data() { fn record_iter() {
let a = ID::new_field(); let a = Field::from(ID::random());
let b = ID::new_field(); let b = Field::from(ID::random());
let c = ID::new_field(); let c = Field::from(ID::random());
let mut data: HashMap<String, Field> = HashMap::new(); let mut data: HashMap<String, Field> = HashMap::new();
data.insert("a".to_string(), a.clone()); data.insert("a".to_string(), a.clone());
data.insert("b".to_string(), b.clone()); data.insert("b".to_string(), b.clone());
data.insert("c".to_string(), c.clone()); data.insert("c".to_string(), c.clone());
let rec = Record::new(data); let rec = Record::new(data.clone());
assert_eq!(rec.len(), 3); for (key, value) in rec.iter() {
assert_eq!(rec.get("a").to_string(), a.to_string(), "record a"); assert_eq!(value.to_string(), data.get(key).unwrap().to_string());
assert_eq!(rec.get("b").to_string(), b.to_string(), "record b"); }
assert_eq!(rec.get("c").to_string(), c.to_string(), "record c");
} }
} }

View File

@ -1,28 +1,42 @@
use std::{error::Error, fmt}; use std::{error::Error, fmt};
#[derive(Debug)] #[derive(Debug)]
enum ErrorType { pub enum ErrorType {
FieldIDInvalid(String),
TableAddFieldDuplicate(String), TableAddFieldDuplicate(String),
TableRecordInvalidFieldName(String),
} }
impl fmt::Display for ErrorType { impl fmt::Display for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
ErrorType::FieldIDInvalid(data) => write!(f, "'{}' is not a valid uuid", data),
ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data), ErrorType::TableAddFieldDuplicate(data) => write!(f, "field '{}' already exists", data),
ErrorType::TableRecordInvalidFieldName(data) => {
write!(f, "invalid field name '{}'", data)
}
} }
} }
} }
#[derive(Debug)] #[derive(Debug)]
struct MTTError { pub struct MTTError {
err: ErrorType, err: ErrorType,
} }
impl From<ErrorType> for MTTError {
fn from(value: ErrorType) -> Self {
MTTError::new(value)
}
}
impl MTTError { impl MTTError {
fn new(err: ErrorType) -> Self { pub fn new(err: ErrorType) -> Self {
Self { Self { err: err }
err: err, }
}
pub fn get_code(&self) -> &ErrorType {
&self.err
} }
} }
@ -37,15 +51,50 @@ impl fmt::Display for MTTError {
#[cfg(test)] #[cfg(test)]
mod errors { mod errors {
use super::*; use super::*;
use rand::{distributions::Alphanumeric, Rng};
fn rand_str() -> String {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(5)
.map(char::from)
.collect()
}
#[test] #[test]
fn get_error() { fn from_invalid_id() {
let err = MTTError::new(ErrorType::TableAddFieldDuplicate("tester".to_string())); let data = rand_str();
assert_eq!(err.to_string(), "field 'tester' already exists"); let etype = ErrorType::FieldIDInvalid(data.clone());
assert!(err.source().is_none()); let result = MTTError::from(etype);
let err = MTTError::new(ErrorType::TableAddFieldDuplicate("other".to_string())); match result.get_code() {
assert_eq!(err.to_string(), "field 'other' already exists"); ErrorType::FieldIDInvalid(txt) => assert_eq!(txt, &data),
assert!(err.source().is_none()); _ => unreachable!("should have been ErrorType::FieldIDInvalid"),
}
assert_eq!(
result.to_string(),
format!("'{}' is not a valid uuid", data)
);
}
#[test]
fn from_duplicate_table_name() {
let data = rand_str();
let etype = ErrorType::TableAddFieldDuplicate(data.clone());
let result = MTTError::from(etype);
match result.get_code() {
ErrorType::TableAddFieldDuplicate(txt) => assert_eq!(txt, &data),
_ => unreachable!("should have been ErrorType::FieldIDInvalid"),
}
assert_eq!(
result.to_string(),
format!("field '{}' already exists", data)
);
}
#[test]
fn error_type_strings() {
let data = rand_str();
let etype = ErrorType::TableRecordInvalidFieldName(data.clone());
assert_eq!(etype.to_string(), format!("invalid field name '{}'", data));
} }
} }