Got tabler holding a record.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit
This commit is contained in:
@@ -1,40 +1,14 @@
|
||||
use crate::data::id::ID;
|
||||
use std::{collections::HashMap, fmt};
|
||||
use std::{collections::HashMap, fmt, ops::Deref};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Field {
|
||||
ID(ID),
|
||||
}
|
||||
|
||||
impl Field {
|
||||
fn new(field_type: &str) -> Field {
|
||||
ID::new_field()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FieldRecord {
|
||||
fn new_field() -> Field;
|
||||
fn get_type() -> String;
|
||||
}
|
||||
|
||||
pub 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 From<ID> for Field {
|
||||
fn from(value: ID) -> Self {
|
||||
Field::ID(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,16 +20,33 @@ impl fmt::Display for Field {
|
||||
}
|
||||
}
|
||||
|
||||
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 creaAte_new_id() {
|
||||
match Field::new("id") {
|
||||
Field::ID(_) => {}
|
||||
_ => unreachable!("Fould should be an ID type."),
|
||||
}
|
||||
fn from_id() {
|
||||
let id = ID::random();
|
||||
let field = Field::from(id.clone());
|
||||
assert_eq!(field.to_string(), id.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,18 +62,17 @@ mod records {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_record_more_data() {
|
||||
let a = ID::new_field();
|
||||
let b = ID::new_field();
|
||||
let c = ID::new_field();
|
||||
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);
|
||||
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");
|
||||
let rec = Record::new(data.clone());
|
||||
for (key, value) in rec.iter() {
|
||||
assert_eq!(value.to_string(), data.get(key).unwrap().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user