Beginning to add tables.
Some checks failed
MoreThanText/morethantext/pipeline/head There was a failure building this commit

This commit is contained in:
Jeff Baskin 2024-11-10 11:34:50 -05:00
parent f966feb0e1
commit 510a4fa7f8
3 changed files with 103 additions and 55 deletions

View File

@ -1,3 +1,4 @@
use crate::data::record::{Field, FieldRecord};
use std::fmt; use std::fmt;
use uuid::Uuid; use uuid::Uuid;
@ -14,12 +15,37 @@ impl ID {
} }
} }
impl FieldRecord for ID {
fn new_field() -> Field {
Field::ID(ID::new())
}
}
impl fmt::Display for ID { impl fmt::Display for ID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.data) write!(f, "{}", self.data)
} }
} }
#[cfg(test)]
mod fielddata {
use super::*;
#[test]
fn new_ids_are_unique() {
let mut ids: Vec<String> = Vec::new();
for _ in 1..10 {
match ID::new_field() {
Field::ID(id) => {
let id_string = id.to_string();
assert!(!ids.contains(&id_string), "'{}' repeated", id_string);
ids.push(id_string);
},
}
}
}
}
#[cfg(test)] #[cfg(test)]
mod id { mod id {
use super::*; use super::*;

View File

@ -1,66 +1,20 @@
mod id; pub mod id;
pub mod record;
use id::ID; struct Table;
use std::{collections::HashMap, fmt};
#[derive(Clone)] impl Table {
enum Field { fn new() -> Self {
ID(ID), Self {}
}
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)] #[cfg(test)]
mod records { mod table {
use super::*; use super::*;
#[test] #[test]
fn new_record() { fn new_table() {
let data: HashMap<String, Field> = HashMap::new(); let tbl = Table::new();
let rec = Record::new(data);
assert_eq!(rec.len(), 0);
}
#[test]
fn new_record_more_data() {
let a = ID::new();
let b = ID::new();
let c = ID::new();
let mut data: HashMap<String, Field> = HashMap::new();
data.insert("a".to_string(), Field::ID(a.clone()));
data.insert("b".to_string(), Field::ID(b.clone()));
data.insert("c".to_string(), Field::ID(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");
} }
} }

68
src/data/record.rs Normal file
View File

@ -0,0 +1,68 @@
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");
}
}