morethantext/src/data/mod.rs

147 lines
3.8 KiB
Rust
Raw Normal View History

2024-11-12 10:26:21 -05:00
mod id;
mod record;
2024-11-09 07:56:49 -05:00
2024-11-25 09:12:31 -05:00
use crate::{
data::{
id::ID,
record::{Field, Record},
},
error::{ErrorType, MTTError},
};
2024-11-12 10:26:21 -05:00
use std::collections::HashMap;
2024-11-09 07:56:49 -05:00
2024-11-25 09:12:31 -05:00
enum FieldType {
ID,
}
2024-11-12 10:26:21 -05:00
struct FieldDef;
impl FieldDef {
2024-11-10 11:34:50 -05:00
fn new() -> Self {
Self {}
2024-11-09 07:56:49 -05:00
}
2024-11-25 09:12:31 -05:00
fn get_type(&self) -> FieldType {
FieldType::ID
}
2024-11-09 07:56:49 -05:00
}
2024-11-12 10:26:21 -05:00
struct Table {
fields: HashMap<String, FieldDef>,
}
impl Table {
fn new() -> Self {
Self {
fields: HashMap::new(),
}
}
2024-11-25 09:12:31 -05:00
fn description(&self) -> &HashMap<String, FieldDef> {
&self.fields
2024-11-12 10:26:21 -05:00
}
2024-11-25 09:12:31 -05:00
fn add_field(&mut self, name: &str, field_type: FieldType) -> Result<(), MTTError> {
2024-11-12 10:26:21 -05:00
match self.fields.get(name) {
2024-11-25 09:12:31 -05:00
Some(_) => {
let err = ErrorType::TableAddFieldDuplicate(name.to_string());
Err(MTTError::new(err))
}
2024-11-12 10:26:21 -05:00
None => {
self.fields.insert(name.to_string(), FieldDef::new());
Ok(())
2024-11-15 13:37:11 -05:00
}
2024-11-12 10:26:21 -05:00
}
}
2024-11-25 09:12:31 -05:00
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)
}
2024-11-12 10:26:21 -05:00
}
2024-11-09 07:56:49 -05:00
#[cfg(test)]
2024-11-10 11:34:50 -05:00
mod table {
2024-11-09 07:56:49 -05:00
use super::*;
#[test]
2024-11-10 11:34:50 -05:00
fn new_table() {
let tbl = Table::new();
2024-11-25 09:12:31 -05:00
let data = tbl.description();
assert_eq!(data.len(), 0);
2024-11-12 10:26:21 -05:00
}
#[test]
fn add_field() {
let mut tbl = Table::new();
2024-11-25 09:12:31 -05:00
let field_name = "something ";
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"),
}
2024-11-12 10:26:21 -05:00
}
#[test]
fn error_on_duplicate_name() {
let mut tbl = Table::new();
2024-11-25 09:12:31 -05:00
let name = "one";
tbl.add_field(name, FieldType::ID);
match tbl.add_field(name, FieldType::ID) {
2024-11-12 10:26:21 -05:00
Ok(_) => unreachable!(" Should not duplicates."),
2024-11-25 09:12:31 -05:00
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"),
},
2024-11-12 10:26:21 -05:00
}
2024-11-09 07:56:49 -05:00
}
}