mod id; mod record; use crate::data::record::Record; use std::collections::HashMap; struct FieldDef; impl FieldDef { fn new() -> Self { Self {} } } struct Table { fields: HashMap, } impl Table { fn new() -> 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(()) } } } } #[cfg(test)] mod table { use super::*; #[test] fn new_table() { 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(_) => {} } } }