Added a table field.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit

This commit is contained in:
Jeff Baskin 2024-11-12 10:26:21 -05:00
parent 510a4fa7f8
commit 2bf495b127
3 changed files with 84 additions and 8 deletions

View File

@ -19,6 +19,10 @@ impl FieldRecord for ID {
fn new_field() -> Field { fn new_field() -> Field {
Field::ID(ID::new()) Field::ID(ID::new())
} }
fn get_type() -> String {
"id".to_string()
}
} }
impl fmt::Display for ID { impl fmt::Display for ID {
@ -40,10 +44,15 @@ mod fielddata {
let id_string = id.to_string(); let id_string = id.to_string();
assert!(!ids.contains(&id_string), "'{}' repeated", id_string); assert!(!ids.contains(&id_string), "'{}' repeated", id_string);
ids.push(id_string); ids.push(id_string);
},
} }
} }
} }
}
#[test]
fn get_type() {
assert_eq!(ID::get_type(), "id");
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,11 +1,40 @@
pub mod id; mod id;
pub mod record; mod record;
struct Table; use crate::data::record::Record;
use std::collections::HashMap;
struct FieldDef;
impl FieldDef {
fn new() -> Self {
Self {}
}
}
struct Table {
fields: HashMap<String, FieldDef>,
}
impl Table { impl Table {
fn new() -> Self { fn new() -> Self {
Self {} Self {
fields: HashMap::new(),
}
}
fn len(&self) -> usize {
self.fields.len()
}
fn add_field(&mut self, name: &str, field_type: &str) -> Result<(), String> {
match self.fields.get(name) {
Some(_) => Err("duplicate field name".to_string()),
None => {
self.fields.insert(name.to_string(), FieldDef::new());
Ok(())
},
}
} }
} }
@ -16,5 +45,23 @@ mod table {
#[test] #[test]
fn new_table() { fn new_table() {
let tbl = Table::new(); let tbl = Table::new();
assert_eq!(tbl.len(), 0);
}
#[test]
fn add_field() {
let mut tbl = Table::new();
tbl.add_field("one", "id");
assert_eq!(tbl.len(), 1);
}
#[test]
fn error_on_duplicate_name() {
let mut tbl = Table::new();
tbl.add_field("one", "id");
match tbl.add_field("one", "id") {
Ok(_) => unreachable!(" Should not duplicates."),
Err(_) => {},
}
} }
} }

View File

@ -6,11 +6,18 @@ pub enum Field {
ID(ID), ID(ID),
} }
pub trait FieldRecord { impl Field {
fn new_field() -> Field; fn new(field_type: &str) -> Field {
ID::new_field()
}
} }
struct Record { pub trait FieldRecord {
fn new_field() -> Field;
fn get_type() -> String;
}
pub struct Record {
data: HashMap<String, Field>, data: HashMap<String, Field>,
} }
@ -39,6 +46,19 @@ impl fmt::Display for Field {
} }
} }
#[cfg(test)]
mod fields {
use super::*;
#[test]
fn creaAte_new_id() {
match Field::new("id") {
Field::ID(_) => {}
_ => unreachable!("Fould should be an ID type."),
}
}
}
#[cfg(test)] #[cfg(test)]
mod records { mod records {
use super::*; use super::*;