pub mod database; pub mod global; pub mod id; mod record; pub mod table; use std::{ collections::BTreeMap, sync::mpsc::{Receiver, Sender, channel}, thread::spawn, }; use super::Message; 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(); path.push(Uuid::nil()); Self { path: path } } fn next() -> Uuid { Uuid::new_v4() } fn extend(&self, addition: Uuid) -> Self { let mut result = self.clone(); result.path.pop(); result.path.push(addition); result.path.push(Uuid::nil()); result } } #[cfg(test)] mod idpaths { use super::*; #[test] fn create_idpath() { let expected = [Uuid::nil()]; let id = IDPath::new(); assert_eq!(id.path, expected); } #[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(); path.push(Uuid::nil()); let mut id = IDPath::new(); for count in 1..5 { assert_eq!(id.path.len(), count); let extended = IDPath::next(); id = id.extend(extended.clone()); path.pop(); path.push(extended); path.push(Uuid::nil()); assert_eq!(id.path, path); } } } struct Data { router_tx: Sender, data_rx: Receiver, } impl Data { fn new(router_tx: Sender, data_rx: Receiver) -> Self { Self { router_tx: router_tx, data_rx: data_rx, } } pub fn start(router_tx: Sender) -> Sender { let (data_tx, data_rx) = channel(); spawn(move || { let mut req = Data::new(router_tx, data_rx); req.listen(); }); data_tx } fn listen(&mut self) { loop { let msg = self.data_rx.recv().unwrap(); } } } #[cfg(test)] mod requests { use super::*; #[test] fn add_database() { let (tx, rx) = channel(); let data_tx = Data::start(tx); } }