use crate::data::id::ID; use std::{collections::HashMap, fmt, ops::Deref}; #[derive(Clone)] pub enum Field { ID(ID), } impl From for Field { fn from(value: ID) -> Self { Field::ID(value) } } impl fmt::Display for Field { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Field::ID(id) => write!(f, "{}", id), } } } pub struct Record { data: HashMap, } impl Record { pub fn new(data: HashMap) -> Self { Self { data: data } } } impl Deref for Record { type Target = HashMap; fn deref(&self) -> &Self::Target { &self.data } } #[cfg(test)] mod fields { use super::*; #[test] fn from_id() { let id = ID::random(); let field = Field::from(id.clone()); assert_eq!(field.to_string(), id.to_string()); } } #[cfg(test)] mod records { use super::*; #[test] fn new_record() { let data: HashMap = HashMap::new(); let rec = Record::new(data); assert_eq!(rec.len(), 0); } #[test] fn record_iter() { let a = Field::from(ID::random()); let b = Field::from(ID::random()); let c = Field::from(ID::random()); let mut data: HashMap = HashMap::new(); data.insert("a".to_string(), a.clone()); data.insert("b".to_string(), b.clone()); data.insert("c".to_string(), c.clone()); let rec = Record::new(data.clone()); for (key, value) in rec.iter() { assert_eq!(value.to_string(), data.get(key).unwrap().to_string()); } } }