Consolidated code.
This commit is contained in:
parent
0a889b6c3c
commit
cef9843720
@ -1,204 +0,0 @@
|
||||
use super::DBError;
|
||||
use std::{collections::HashMap, fmt, str};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Databases;
|
||||
|
||||
impl Databases {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn add_database(&self, _name: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum CacheType {
|
||||
Raw(String),
|
||||
DBMap(Databases),
|
||||
TableMap,
|
||||
}
|
||||
|
||||
impl CacheType {
|
||||
pub fn entry_type(&self) -> String {
|
||||
match self {
|
||||
CacheType::Raw(_) => "Raw".to_string(),
|
||||
CacheType::DBMap(_) => "DBMap".to_string(),
|
||||
CacheType::TableMap => "TableMap".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut output = self.entry_type().into_bytes();
|
||||
output.push(0);
|
||||
match self {
|
||||
CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()),
|
||||
CacheType::DBMap(_) => (),
|
||||
CacheType::TableMap => (),
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn from_bytes(data: Vec<u8>) -> Result<CacheType, DBError> {
|
||||
let mut data_iter = data.iter();
|
||||
let mut letter: u8;
|
||||
match data_iter.next() {
|
||||
Some(item) => letter = *item,
|
||||
None => return Err(DBError::new("empty file")),
|
||||
}
|
||||
let mut header: Vec<u8> = Vec::new();
|
||||
while letter != 0 {
|
||||
header.push(letter.clone());
|
||||
match data_iter.next() {
|
||||
Some(item) => letter = *item,
|
||||
None => return Err(DBError::new("incomplete file")),
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string()))
|
||||
}
|
||||
"DBMap" => Ok(CacheType::DBMap(Databases::new())),
|
||||
"TableMap" => Ok(CacheType::TableMap),
|
||||
_ => Err(DBError::new("data corruption")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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!(),
|
||||
CacheType::TableMap => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod databases {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn add_database() {
|
||||
let dbs = Databases::new();
|
||||
let name = "db1";
|
||||
dbs.add_database(name);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod enum_ctype {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bad_file_header() {
|
||||
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(err) => assert_eq!(err.to_string(), "data corruption"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete_file() {
|
||||
let mut data: Vec<u8> = Vec::new();
|
||||
let mut ctype = "uoisfde".as_bytes().to_vec();
|
||||
data.append(&mut ctype);
|
||||
match CacheType::from_bytes(data) {
|
||||
Ok(_) => assert!(false, "This should fail."),
|
||||
Err(err) => assert_eq!(err.to_string(), "incomplete file"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_file() {
|
||||
let data: Vec<u8> = Vec::new();
|
||||
match CacheType::from_bytes(data) {
|
||||
Ok(_) => assert!(false, "This should fail."),
|
||||
Err(err) => assert_eq!(err.to_string(), "empty file"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_raw_type() {
|
||||
let holder = CacheType::Raw("nothing important".to_string());
|
||||
assert_eq!(holder.entry_type(), "Raw");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_raw_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_raw_bytes() {
|
||||
let holder = CacheType::Raw("stored item".to_string());
|
||||
let data = holder.to_bytes();
|
||||
let output = CacheType::from_bytes(data).unwrap();
|
||||
assert_eq!(output.to_string(), holder.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_dbmap_type() {
|
||||
let holder = CacheType::DBMap(Databases::new());
|
||||
assert_eq!(holder.entry_type(), "DBMap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_new_databases_bytes() {
|
||||
let holder = CacheType::DBMap(Databases::new());
|
||||
let mut expected = "DBMap".as_bytes().to_vec();
|
||||
expected.push(0);
|
||||
let output = holder.to_bytes();
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_new_databases_bytes() {
|
||||
let mut data = "DBMap".as_bytes().to_vec();
|
||||
data.push(0);
|
||||
let output = CacheType::from_bytes(data).unwrap();
|
||||
assert_eq!(output.entry_type(), "DBMap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_tablemap_type() {
|
||||
let holder = CacheType::TableMap;
|
||||
assert_eq!(holder.entry_type(), "TableMap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_new_database_bytes() {
|
||||
let holder = CacheType::TableMap;
|
||||
let mut expected = "TableMap".as_bytes().to_vec();
|
||||
expected.push(0);
|
||||
let output = holder.to_bytes();
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_new_database_bytes() {
|
||||
let mut data = "TableMap".as_bytes().to_vec();
|
||||
data.push(0);
|
||||
let output = CacheType::from_bytes(data).unwrap();
|
||||
assert_eq!(output.entry_type(), "TableMap");
|
||||
}
|
||||
}
|
@ -1,89 +0,0 @@
|
||||
mod static_string;
|
||||
|
||||
use crate::morethantext::error::MTTError;
|
||||
use static_string::StaticString;
|
||||
use std::fmt;
|
||||
|
||||
pub enum FieldType {
|
||||
StaticString(StaticString),
|
||||
}
|
||||
|
||||
impl FieldType {
|
||||
fn new(ftype: &str, data: &str) -> Result<Self, MTTError> {
|
||||
let field = match ftype {
|
||||
"StaticString" => StaticString::new(data),
|
||||
_ => Err(MTTError::new(format!(
|
||||
"field type {} does not exist",
|
||||
ftype
|
||||
))),
|
||||
};
|
||||
match field {
|
||||
Ok(fld) => Ok(fld.into()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for FieldType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
FieldType::StaticString(data) => write!(f, "{}", data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StaticString> for FieldType {
|
||||
fn from(data: StaticString) -> Self {
|
||||
FieldType::StaticString(data)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod converstion {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn from_static_string() {
|
||||
let data = "a static string";
|
||||
let field = StaticString::new(data).unwrap();
|
||||
let ftype: FieldType = field.into();
|
||||
assert!(
|
||||
ftype.to_string() == data,
|
||||
"\n\nGot: {}\nWant: {}",
|
||||
ftype.to_string(),
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_field_type() -> Result<(), String> {
|
||||
let field_type = "dragon";
|
||||
let err_msg = format!("field type {} does not exist", field_type);
|
||||
match FieldType::new(field_type, "marmalade") {
|
||||
Ok(_) => Err("Should have returned an error.".to_string()),
|
||||
Err(err) => {
|
||||
if err.to_string() == err_msg {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!(
|
||||
"Error message is incorrect: Got: '{}' Want: '{}'",
|
||||
err.to_string(),
|
||||
err_msg
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_static_string() {
|
||||
let data = "This is a test.";
|
||||
let field = FieldType::new("StaticString", data).unwrap();
|
||||
assert!(
|
||||
field.to_string() == data,
|
||||
"\n\nGot: {}\nWant: {}\n\n",
|
||||
field.to_string(),
|
||||
data
|
||||
);
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
use crate::morethantext::error::MTTError;
|
||||
use std::fmt;
|
||||
|
||||
pub struct StaticString {
|
||||
data: String,
|
||||
}
|
||||
|
||||
impl StaticString {
|
||||
pub fn new<S>(name: S) -> Result<Self, MTTError>
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
Ok(Self { data: name.into() })
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for StaticString {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", &self.data)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod creation {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new_accepts_str() {
|
||||
let data = "some data";
|
||||
let field = StaticString::new(data).unwrap();
|
||||
assert!(
|
||||
field.to_string() == data,
|
||||
"\n\nGot: {}\nWant: {}",
|
||||
field.to_string(),
|
||||
data
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_accepts_string() {
|
||||
let data = "actual string";
|
||||
let field = StaticString::new(data.to_string()).unwrap();
|
||||
assert!(
|
||||
field.to_string() == data,
|
||||
"\n\nGot: {}\nWant: {}",
|
||||
field.to_string(),
|
||||
data
|
||||
);
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
mod cachetype;
|
||||
pub mod error;
|
||||
|
||||
use async_std::{
|
||||
@ -7,7 +6,6 @@ use async_std::{
|
||||
sync::{Arc, Mutex},
|
||||
task::{sleep, spawn},
|
||||
};
|
||||
use cachetype::{CacheType, Databases};
|
||||
use error::DBError;
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
use std::{
|
||||
@ -19,6 +17,86 @@ use std::{
|
||||
const DATA: &str = "data";
|
||||
const ENTRY: &str = "databases";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Databases;
|
||||
|
||||
impl Databases {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
|
||||
fn add_database(&self, _name: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum CacheType {
|
||||
Raw(String),
|
||||
DBMap(Databases),
|
||||
TableMap,
|
||||
}
|
||||
|
||||
impl CacheType {
|
||||
pub fn entry_type(&self) -> String {
|
||||
match self {
|
||||
CacheType::Raw(_) => "Raw".to_string(),
|
||||
CacheType::DBMap(_) => "DBMap".to_string(),
|
||||
CacheType::TableMap => "TableMap".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut output = self.entry_type().into_bytes();
|
||||
output.push(0);
|
||||
match self {
|
||||
CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()),
|
||||
CacheType::DBMap(_) => (),
|
||||
CacheType::TableMap => (),
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
pub fn from_bytes(data: Vec<u8>) -> Result<CacheType, DBError> {
|
||||
let mut data_iter = data.iter();
|
||||
let mut letter: u8;
|
||||
match data_iter.next() {
|
||||
Some(item) => letter = *item,
|
||||
None => return Err(DBError::new("empty file")),
|
||||
}
|
||||
let mut header: Vec<u8> = Vec::new();
|
||||
while letter != 0 {
|
||||
header.push(letter.clone());
|
||||
match data_iter.next() {
|
||||
Some(item) => letter = *item,
|
||||
None => return Err(DBError::new("incomplete file")),
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string()))
|
||||
}
|
||||
"DBMap" => Ok(CacheType::DBMap(Databases::new())),
|
||||
"TableMap" => Ok(CacheType::TableMap),
|
||||
_ => Err(DBError::new("data corruption")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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!(),
|
||||
CacheType::TableMap => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct CacheEntry {
|
||||
data: CacheType,
|
||||
@ -579,3 +657,125 @@ mod cache_entry {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod databases {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn add_database() {
|
||||
let dbs = Databases::new();
|
||||
let name = "db1";
|
||||
dbs.add_database(name);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod enum_ctype {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bad_file_header() {
|
||||
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(err) => assert_eq!(err.to_string(), "data corruption"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incomplete_file() {
|
||||
let mut data: Vec<u8> = Vec::new();
|
||||
let mut ctype = "uoisfde".as_bytes().to_vec();
|
||||
data.append(&mut ctype);
|
||||
match CacheType::from_bytes(data) {
|
||||
Ok(_) => assert!(false, "This should fail."),
|
||||
Err(err) => assert_eq!(err.to_string(), "incomplete file"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_file() {
|
||||
let data: Vec<u8> = Vec::new();
|
||||
match CacheType::from_bytes(data) {
|
||||
Ok(_) => assert!(false, "This should fail."),
|
||||
Err(err) => assert_eq!(err.to_string(), "empty file"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_raw_type() {
|
||||
let holder = CacheType::Raw("nothing important".to_string());
|
||||
assert_eq!(holder.entry_type(), "Raw");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_raw_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_raw_bytes() {
|
||||
let holder = CacheType::Raw("stored item".to_string());
|
||||
let data = holder.to_bytes();
|
||||
let output = CacheType::from_bytes(data).unwrap();
|
||||
assert_eq!(output.to_string(), holder.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_dbmap_type() {
|
||||
let holder = CacheType::DBMap(Databases::new());
|
||||
assert_eq!(holder.entry_type(), "DBMap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_new_databases_bytes() {
|
||||
let holder = CacheType::DBMap(Databases::new());
|
||||
let mut expected = "DBMap".as_bytes().to_vec();
|
||||
expected.push(0);
|
||||
let output = holder.to_bytes();
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_new_databases_bytes() {
|
||||
let mut data = "DBMap".as_bytes().to_vec();
|
||||
data.push(0);
|
||||
let output = CacheType::from_bytes(data).unwrap();
|
||||
assert_eq!(output.entry_type(), "DBMap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_tablemap_type() {
|
||||
let holder = CacheType::TableMap;
|
||||
assert_eq!(holder.entry_type(), "TableMap");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_new_database_bytes() {
|
||||
let holder = CacheType::TableMap;
|
||||
let mut expected = "TableMap".as_bytes().to_vec();
|
||||
expected.push(0);
|
||||
let output = holder.to_bytes();
|
||||
assert_eq!(output, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_new_database_bytes() {
|
||||
let mut data = "TableMap".as_bytes().to_vec();
|
||||
data.push(0);
|
||||
let output = CacheType::from_bytes(data).unwrap();
|
||||
assert_eq!(output.entry_type(), "TableMap");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user