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