diff --git a/src/morethantext/cachetype.rs b/src/morethantext/cachetype.rs index a0d7b0f..f84d81c 100644 --- a/src/morethantext/cachetype.rs +++ b/src/morethantext/cachetype.rs @@ -1,4 +1,5 @@ -use std::{fmt, str}; +use super::DBError; +use std::{collections::HashMap, fmt, str}; #[derive(Clone)] pub enum CacheType { @@ -7,11 +8,25 @@ pub enum CacheType { } #[derive(Clone)] -pub struct Record; +pub struct Record { + data: HashMap +} impl Record { fn new() -> Self { - Self {} + Self { + data: HashMap::new(), + } + } + + fn add_field(&mut self, col: &str, data: &str) -> Result<(), DBError> { + match self.data.get(col) { + Some(_) => Err(DBError::new("duplicate field")), + None => { + self.data.insert(col.to_string(), data.to_string()); + Ok(()) + }, + } } } @@ -97,3 +112,26 @@ mod enum_ctype { assert_eq!(holder.entry_type(), "Record"); } } + +#[cfg(test)] +mod record { + use super::*; + + #[test] + fn store() { + let mut rec = Record::new(); + rec.add_field("column", "data").unwrap(); + } + + #[test] + fn no_duplicate_fields() { + let mut rec = Record::new(); + rec.add_field("dup", "datai1").unwrap(); + match rec.add_field("dup", "datai2") { + Ok(_) => assert!(false, "Should have raised an error."), + Err(err) => { + assert_eq!(err.to_string(), "duplicate field"); + }, + } + } +}