Began work to add entry point.
This commit is contained in:
parent
d09474bb14
commit
b5b0016aba
@ -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<u8>) -> CacheType {
|
||||
pub fn from_bytes(data: Vec<u8>) -> Result<CacheType, DBError> {
|
||||
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<u8> = Vec::new();
|
||||
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();
|
||||
for letter in data_iter {
|
||||
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 {
|
||||
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<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(_) => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user