69 lines
1.6 KiB
Rust
69 lines
1.6 KiB
Rust
|
use crate::data::id::ID;
|
||
|
use std::{collections::HashMap, fmt};
|
||
|
|
||
|
#[derive(Clone)]
|
||
|
pub enum Field {
|
||
|
ID(ID),
|
||
|
}
|
||
|
|
||
|
pub trait FieldRecord {
|
||
|
fn new_field() -> Field;
|
||
|
}
|
||
|
|
||
|
struct Record {
|
||
|
data: HashMap<String, Field>,
|
||
|
}
|
||
|
|
||
|
impl Record {
|
||
|
fn new(data: HashMap<String, Field>) -> Self {
|
||
|
Self { data: data }
|
||
|
}
|
||
|
|
||
|
fn len(&self) -> usize {
|
||
|
self.data.len()
|
||
|
}
|
||
|
|
||
|
fn get(&self, name: &str) -> Field {
|
||
|
match self.data.get(name) {
|
||
|
Some(data) => data.clone(),
|
||
|
None => unreachable!(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Field {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
|
match self {
|
||
|
Field::ID(id) => write!(f, "{}", id),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[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 new_record_more_data() {
|
||
|
let a = ID::new_field();
|
||
|
let b = ID::new_field();
|
||
|
let c = ID::new_field();
|
||
|
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);
|
||
|
assert_eq!(rec.len(), 3);
|
||
|
assert_eq!(rec.get("a").to_string(), a.to_string(), "record a");
|
||
|
assert_eq!(rec.get("b").to_string(), b.to_string(), "record b");
|
||
|
assert_eq!(rec.get("c").to_string(), c.to_string(), "record c");
|
||
|
}
|
||
|
}
|