Began work to add entry point.

This commit is contained in:
Jeff Baskin 2023-01-11 09:10:15 -05:00
parent d09474bb14
commit b5b0016aba
2 changed files with 61 additions and 10 deletions

View File

@ -1,15 +1,26 @@
use super::DBError; use super::DBError;
use std::{collections::HashMap, fmt, str}; use std::{collections::HashMap, fmt, str};
#[derive(Clone)]
pub struct Databases;
impl Databases {
fn new() -> Self {
Self {}
}
}
#[derive(Clone)] #[derive(Clone)]
pub enum CacheType { pub enum CacheType {
Raw(String), Raw(String),
DBMap(Databases),
} }
impl CacheType { impl CacheType {
fn entry_type(&self) -> String { fn entry_type(&self) -> String {
match self { match self {
CacheType::Raw(_) => "Raw".to_string(), CacheType::Raw(_) => "Raw".to_string(),
CacheType::DBMap(_) => "DBMap".to_string(),
} }
} }
@ -18,21 +29,30 @@ impl CacheType {
output.push(0); output.push(0);
match self { match self {
CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()), CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()),
CacheType::DBMap(_) => todo!(),
} }
return output; return output;
} }
pub fn from_bytes(data: Vec<u8>) -> CacheType { pub fn from_bytes(data: Vec<u8>) -> Result<CacheType, DBError> {
let mut data_iter = data.iter(); let mut data_iter = data.iter();
let mut holder: u8 = *data_iter.next().unwrap(); let mut letter: u8 = *data_iter.next().unwrap();
while holder != 0 { let mut header: Vec<u8> = Vec::new();
holder = *data_iter.next().unwrap(); while letter != 0 {
header.push(letter.clone());
letter = *data_iter.next().unwrap();
} }
let header = str::from_utf8(&header).unwrap().to_string();
match header.as_str() {
"Raw" => {
let mut output: Vec<u8> = Vec::new(); let mut output: Vec<u8> = Vec::new();
for letter in data_iter { for letter in data_iter {
output.push(letter.clone()); output.push(letter.clone());
} }
CacheType::Raw(str::from_utf8(&output).unwrap().to_string()) Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string()))
}
_ => Err(DBError::new("fred")),
}
} }
} }
@ -40,10 +60,21 @@ impl fmt::Display for CacheType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
CacheType::Raw(s) => write!(f, "{}", s), CacheType::Raw(s) => write!(f, "{}", s),
CacheType::DBMap(_) => todo!(),
} }
} }
} }
#[cfg(test)]
mod databases {
use super::*;
#[test]
fn get_databases() {
Databases::new();
}
}
#[cfg(test)] #[cfg(test)]
mod raw { mod raw {
use super::*; use super::*;
@ -63,7 +94,7 @@ mod raw {
fn from_bytes() { fn from_bytes() {
let holder = CacheType::Raw("stored item".to_string()); let holder = CacheType::Raw("stored item".to_string());
let data = holder.to_bytes(); 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()); assert_eq!(output.to_string(), holder.to_string());
} }
} }
@ -77,4 +108,24 @@ mod enum_ctype {
let holder = CacheType::Raw("nothing important".to_string()); let holder = CacheType::Raw("nothing important".to_string());
assert_eq!(holder.entry_type(), "Raw"); 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<u8> = 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(_) => (),
}
}
} }

View File

@ -130,7 +130,7 @@ impl MoreThanText {
} }
None => match read(Path::new(&self.filename(id))).await { None => match read(Path::new(&self.filename(id))).await {
Ok(content) => { 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()); cache.insert(id.to_string(), data.clone());
Ok(data) Ok(data)
} }