diff --git a/src/morethantext/cachetype.rs b/src/morethantext/cachetype.rs new file mode 100644 index 0000000..b7076c7 --- /dev/null +++ b/src/morethantext/cachetype.rs @@ -0,0 +1,74 @@ +use std::{fmt, str}; + +#[derive(Clone)] +pub enum CacheType { + Raw(String), +} + +impl CacheType { + fn entry_type(&self) -> String { + match self { + CacheType::Raw(_) => "Raw".to_string(), + } + } + + pub fn to_bytes(&self) -> Vec { + let mut output = self.entry_type().into_bytes(); + output.push(0); + match self { + CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()), + } + return output; + } + + pub fn from_bytes(data: Vec) -> CacheType { + let mut data_iter = data.iter(); + let mut holder: u8 = *data_iter.next().unwrap(); + while holder != 0 { + holder = *data_iter.next().unwrap(); + } + let mut output: Vec = Vec::new(); + for letter in data_iter { + output.push(letter.clone()); + } + CacheType::Raw(str::from_utf8(&output).unwrap().to_string()) + } +} + +impl fmt::Display for CacheType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + CacheType::Raw(s) => write!(f, "{}", s), + } + } +} + +#[cfg(test)] +mod raw { + use super::*; + + #[test] + fn get_type() { + let holder = CacheType::Raw("nothing important".to_string()); + assert_eq!(holder.entry_type(), "Raw"); + } + + #[test] + fn get_bytes() { + let data = "addams"; + let holder = CacheType::Raw(data.to_string()); + let mut expected = holder.entry_type().into_bytes(); + expected.push(0); + expected.append(&mut data.as_bytes().to_vec()); + let output = holder.to_bytes(); + assert_eq!(output, expected); + } + + #[test] + fn from_bytes() { + let holder = CacheType::Raw("stored item".to_string()); + let data = holder.to_bytes(); + let output = CacheType::from_bytes(data); + assert_eq!(output.to_string(), holder.to_string()); + } +} diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 3f10b96..e23a218 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -1,4 +1,5 @@ pub mod error; +mod cachetype; use async_std::{ fs::{create_dir, read, remove_file, write}, @@ -6,6 +7,7 @@ use async_std::{ sync::{Arc, Mutex}, task::{sleep, spawn}, }; +use cachetype::CacheType; use error::DBError; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use std::{ @@ -16,49 +18,6 @@ use std::{ const DATA: &str = "data"; -#[derive(Clone)] -enum CacheType { - Raw(String), -} - -impl CacheType { - fn entry_type(&self) -> String { - match self { - CacheType::Raw(_) => "Raw".to_string(), - } - } - - fn to_bytes(&self) -> Vec { - let mut output = self.entry_type().into_bytes(); - output.push(0); - match self { - CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()), - } - return output; - } - - fn from_bytes(data: Vec) -> CacheType { - let mut data_iter = data.iter(); - let mut holder: u8 = *data_iter.next().unwrap(); - while holder != 0 { - holder = *data_iter.next().unwrap(); - } - let mut output: Vec = Vec::new(); - for letter in data_iter { - output.push(letter.clone()); - } - CacheType::Raw(str::from_utf8(&output).unwrap().to_string()) - } -} - -impl fmt::Display for CacheType { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - CacheType::Raw(s) => write!(f, "{}", s), - } - } -} - #[derive(Clone)] struct CacheEntry { data: CacheType, @@ -512,36 +471,6 @@ mod cache { } } -#[cfg(test)] -mod cache_type { - use super::*; - - #[test] - fn raw_get_type() { - let holder = CacheType::Raw("nothing important".to_string()); - assert_eq!(holder.entry_type(), "Raw"); - } - - #[test] - fn raw_get_bytes() { - let data = "addams"; - let holder = CacheType::Raw(data.to_string()); - let mut expected = holder.entry_type().into_bytes(); - expected.push(0); - expected.append(&mut data.as_bytes().to_vec()); - let output = holder.to_bytes(); - assert_eq!(output, expected); - } - - #[test] - fn raw_from_bytes() { - let holder = CacheType::Raw("stored item".to_string()); - let data = holder.to_bytes(); - let output = CacheType::from_bytes(data); - assert_eq!(output.to_string(), holder.to_string()); - } -} - #[cfg(test)] mod cache_entry { use super::*;