Added record entry.

This commit is contained in:
Jeff Baskin 2023-01-04 09:56:58 -05:00
parent 58aba1a565
commit 6983ee366d

View File

@ -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<String, String>
}
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");
},
}
}
}