From b5b0016aba3fbec7f6325415157200deb7eff8fc Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 11 Jan 2023 09:10:15 -0500 Subject: [PATCH] Began work to add entry point. --- src/morethantext/cachetype.rs | 69 ++++++++++++++++++++++++++++++----- src/morethantext/mod.rs | 2 +- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/morethantext/cachetype.rs b/src/morethantext/cachetype.rs index 40bcefa..0c9f19d 100644 --- a/src/morethantext/cachetype.rs +++ b/src/morethantext/cachetype.rs @@ -1,15 +1,26 @@ use super::DBError; use std::{collections::HashMap, fmt, str}; +#[derive(Clone)] +pub struct Databases; + +impl Databases { + fn new() -> Self { + Self {} + } +} + #[derive(Clone)] pub enum CacheType { Raw(String), + DBMap(Databases), } impl CacheType { fn entry_type(&self) -> String { match self { CacheType::Raw(_) => "Raw".to_string(), + CacheType::DBMap(_) => "DBMap".to_string(), } } @@ -18,21 +29,30 @@ impl CacheType { output.push(0); match self { CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()), + CacheType::DBMap(_) => todo!(), } return output; } - pub fn from_bytes(data: Vec) -> CacheType { + pub fn from_bytes(data: Vec) -> Result { let mut data_iter = data.iter(); - let mut holder: u8 = *data_iter.next().unwrap(); - while holder != 0 { - holder = *data_iter.next().unwrap(); + let mut letter: u8 = *data_iter.next().unwrap(); + let mut header: Vec = Vec::new(); + while letter != 0 { + header.push(letter.clone()); + letter = *data_iter.next().unwrap(); } - let mut output: Vec = Vec::new(); - for letter in data_iter { - output.push(letter.clone()); + let header = str::from_utf8(&header).unwrap().to_string(); + match header.as_str() { + "Raw" => { + let mut output: Vec = Vec::new(); + for letter in data_iter { + output.push(letter.clone()); + } + Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string())) + } + _ => Err(DBError::new("fred")), } - CacheType::Raw(str::from_utf8(&output).unwrap().to_string()) } } @@ -40,10 +60,21 @@ impl fmt::Display for CacheType { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { CacheType::Raw(s) => write!(f, "{}", s), + CacheType::DBMap(_) => todo!(), } } } +#[cfg(test)] +mod databases { + use super::*; + + #[test] + fn get_databases() { + Databases::new(); + } +} + #[cfg(test)] mod raw { use super::*; @@ -63,7 +94,7 @@ mod raw { fn from_bytes() { let holder = CacheType::Raw("stored item".to_string()); let data = holder.to_bytes(); - let output = CacheType::from_bytes(data); + let output = CacheType::from_bytes(data).unwrap(); assert_eq!(output.to_string(), holder.to_string()); } } @@ -77,4 +108,24 @@ mod enum_ctype { let holder = CacheType::Raw("nothing important".to_string()); assert_eq!(holder.entry_type(), "Raw"); } + + #[test] + fn get_dbmap_type() { + let holder = CacheType::DBMap(Databases::new()); + assert_eq!(holder.entry_type(), "DBMap"); + } + + #[test] + fn bad_file() { + let mut data: Vec = Vec::new(); + let mut ctype = "jlksdfg".as_bytes().to_vec(); + let mut cdata = "ghjk".as_bytes().to_vec(); + data.append(&mut ctype); + data.push(0); + data.append(&mut cdata); + match CacheType::from_bytes(data) { + Ok(_) => assert!(false, "This should fail."), + Err(_) => (), + } + } } diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 06fba46..c6e6914 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -130,7 +130,7 @@ impl MoreThanText { } None => match read(Path::new(&self.filename(id))).await { Ok(content) => { - let data = CacheEntry::new(CacheType::from_bytes(content)); + let data = CacheEntry::new(CacheType::from_bytes(content).unwrap()); cache.insert(id.to_string(), data.clone()); Ok(data) }