From 6d61af51368e90ff0ecf1956e7908d7326546977 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Tue, 28 Mar 2023 07:38:12 -0400 Subject: [PATCH] Added id generator. --- src/morethantext/mod.rs | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 96072c4..6b72ce7 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -7,11 +7,16 @@ use async_std::path::PathBuf; use database::Database; use entry::Entry; use error::{DBError, ErrorCode}; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; use std::{slice, str}; use store::Store; const ENTRY: &str = "EntryPoint"; +trait ID { + fn next(&self) -> String; +} + trait FileData { fn to_bytes(&self) -> Vec; fn from_bytes(data: &mut slice::Iter) -> Result; @@ -107,6 +112,20 @@ impl FileData for DataType { } } +pub struct IDs; + +impl IDs { + pub fn new() -> Self { + Self {} + } +} + +impl ID for IDs { + fn next(&self) -> String { + thread_rng().sample_iter(&Alphanumeric).take(64).collect() + } +} + #[derive(Clone)] pub struct MoreThanText { session: Vec, @@ -141,6 +160,10 @@ impl MoreThanText { fn set_session(&mut self, sess: Vec) { self.session = sess; } + + async fn new_entry(&self, _name: &str) -> Self { + self.clone() + } } #[cfg(test)] @@ -302,6 +325,27 @@ mod datatype_file { } } +#[cfg(test)] +mod ids { + use super::*; + + #[test] + fn get_next() { + let ids = IDs::new(); + let mut holder: Vec = Vec::new(); + for _ in 0..10 { + let id = ids.next(); + assert!( + !holder.contains(&id), + "No duplicates: found {} in {:?}", + id, + holder + ); + holder.push(id); + } + } +} + #[cfg(test)] mod db { use super::*; @@ -381,4 +425,11 @@ mod db { mtt.set_session(sess.to_vec()); assert_eq!(mtt.session, sess); } + + #[async_std::test] + async fn add_a_database() { + let dir = tempdir().unwrap(); + let mtt = MoreThanText::new(dir.path()).await.unwrap(); + mtt.new_entry("wilbur").await; + } }