From 510a4fa7f86c82ca24cbf58252f93689215967f6 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 10 Nov 2024 11:34:50 -0500 Subject: [PATCH] Beginning to add tables. --- src/data/id.rs | 26 ++++++++++++++++++ src/data/mod.rs | 64 ++++++------------------------------------- src/data/record.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 55 deletions(-) create mode 100644 src/data/record.rs diff --git a/src/data/id.rs b/src/data/id.rs index 7e61cab..d98c3a0 100644 --- a/src/data/id.rs +++ b/src/data/id.rs @@ -1,3 +1,4 @@ +use crate::data::record::{Field, FieldRecord}; use std::fmt; 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 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.data) } } +#[cfg(test)] +mod fielddata { + use super::*; + + #[test] + fn new_ids_are_unique() { + let mut ids: Vec = 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)] mod id { use super::*; diff --git a/src/data/mod.rs b/src/data/mod.rs index 3ed5f54..6a8279d 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,66 +1,20 @@ -mod id; +pub mod id; +pub mod record; -use id::ID; -use std::{collections::HashMap, fmt}; +struct Table; -#[derive(Clone)] -enum Field { - ID(ID), -} - -struct Record { - data: HashMap, -} - -impl Record { - fn new(data: HashMap) -> 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), - } +impl Table { + fn new() -> Self { + Self {} } } #[cfg(test)] -mod records { +mod table { use super::*; #[test] - fn new_record() { - let data: HashMap = HashMap::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 = 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"); + fn new_table() { + let tbl = Table::new(); } } diff --git a/src/data/record.rs b/src/data/record.rs new file mode 100644 index 0000000..7a3ab1f --- /dev/null +++ b/src/data/record.rs @@ -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, +} + +impl Record { + fn new(data: HashMap) -> 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 = 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 = 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"); + } +}