morethantext/src/data/mod.rs

68 lines
1.3 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-12 10:26:21 -05:00
use crate::data::record::Record;
use std::collections::HashMap;
2024-11-09 07:56:49 -05:00
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-12 10:26:21 -05:00
struct Table {
fields: HashMap<String, FieldDef>,
}
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(())
},
}
}
}
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-12 10:26:21 -05:00
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(_) => {},
}
2024-11-09 07:56:49 -05:00
}
}