From 18c59207664cd56f5ae82ff4d4df88549bd557ae Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 2 Feb 2025 12:58:10 -0500 Subject: [PATCH] Forgotten file. --- src/data/mod.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/data/mod.rs diff --git a/src/data/mod.rs b/src/data/mod.rs new file mode 100644 index 0000000..d0c2d0d --- /dev/null +++ b/src/data/mod.rs @@ -0,0 +1,118 @@ +pub mod database; +pub mod global; +pub mod id; +mod record; +pub mod table; + +use std::collections::BTreeMap; +use uuid::Uuid; + +#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Clone, Debug)] +struct IDPath { + path: Vec, +} + +impl IDPath { + fn new() -> Self { + let mut path = Vec::new(); + Self { path: path } + } + + fn next() -> Uuid { + Uuid::new_v4() + } + + fn extend(&self, addition: Uuid) -> Self { + let mut result = self.clone(); + result.path.push(addition); + result + } + + fn root(&self) -> Self { + Self::new() + } +} + +#[cfg(test)] +mod ids { + use super::*; + + #[test] + fn create_idpath() { + let id = IDPath::new(); + assert_eq!(id.path.len(), 0); + } + + #[test] + fn next_is_random() { + let mut ids: Vec = Vec::new(); + for _ in 0..10 { + let id = IDPath::next(); + assert!(!ids.contains(&id), "{} is a duplicate", id); + ids.push(id); + } + } + + #[test] + fn extend_idpath() { + let mut path: Vec = Vec::new(); + let mut id = IDPath::new(); + for _ in 1..5 { + let extended = IDPath::next(); + id = id.extend(extended.clone()); + path.push(extended); + assert_eq!(id.path, path); + } + } + + #[test] + fn get_root() { + let mut id = IDPath::new(); + id.root(); + } +} + +struct Cache { + data: BTreeMap, +} + +impl Cache { + fn new() -> Self { + Self { + data: BTreeMap::new(), + } + } + + fn add_database(&mut self, name: S) + where + S: Into, + { + self.data.insert(name.into(), IDPath::new()); + } + + fn get_databases(&self) -> Vec { + self.data.keys().cloned().collect() + } +} + +#[cfg(test)] +mod caches { + use super::*; + + #[test] + fn create_cache() { + let cache = Cache::new(); + let dbs = cache.get_databases(); + assert!(dbs.is_empty()); + } + + #[test] + fn add_databases() { + let mut cache = Cache::new(); + cache.add_database("zed"); + cache.add_database("alpha".to_string()); + cache.add_database("beta"); + cache.add_database("gamma".to_string()); + assert_eq!(cache.get_databases(), ["alpha", "beta", "gamma", "zed"]); + } +}