morethantext/src/data/record.rs
Jeff Baskin e1aec8de28
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
Got tabler holding a record.
2024-11-25 09:12:31 -05:00

79 lines
1.6 KiB
Rust

use crate::data::id::ID;
use std::{collections::HashMap, fmt, ops::Deref};
#[derive(Clone)]
pub enum Field {
ID(ID),
}
impl From<ID> 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<String, Field>,
}
impl Record {
pub fn new(data: HashMap<String, Field>) -> Self {
Self { data: data }
}
}
impl Deref for Record {
type Target = HashMap<String, Field>;
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<String, Field> = 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<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());
let rec = Record::new(data.clone());
for (key, value) in rec.iter() {
assert_eq!(value.to_string(), data.get(key).unwrap().to_string());
}
}
}