Added id generator.

This commit is contained in:
Jeff Baskin 2023-03-28 07:38:12 -04:00
parent 4b0af019c8
commit 6d61af5136
1 changed files with 51 additions and 0 deletions

View File

@ -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<F> {
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>;
@ -107,6 +112,20 @@ impl FileData<Self> 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<String>,
@ -141,6 +160,10 @@ impl MoreThanText {
fn set_session(&mut self, sess: Vec<String>) {
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<String> = 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;
}
}