2023-04-04 09:59:29 -04:00
|
|
|
use async_std::{
|
|
|
|
channel::{unbounded, Sender},
|
|
|
|
path::PathBuf,
|
|
|
|
task::spawn,
|
|
|
|
};
|
|
|
|
use std::{error::Error, fmt};
|
|
|
|
|
|
|
|
const ENTRY: &str = "EntryPoint";
|
|
|
|
|
2023-04-04 12:36:10 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum ErrorCode {
|
|
|
|
Undefined(String),
|
|
|
|
IncorrectDataType(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ErrorCode {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
|
|
|
|
ErrorCode::IncorrectDataType(item) => write!(f, "data type '{}' does not exist", item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod errorcodes {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const ITEMS: [&str; 2] = ["one", "two"];
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn undefined_display() {
|
|
|
|
for item in ITEMS {
|
|
|
|
let err = ErrorCode::Undefined(item.to_string());
|
|
|
|
assert_eq!(err.to_string(), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn incorrect_data_type() {
|
|
|
|
for item in ITEMS {
|
|
|
|
let err = ErrorCode::IncorrectDataType(item.to_string());
|
|
|
|
assert_eq!(
|
|
|
|
err.to_string(),
|
|
|
|
format!("data type '{}' does not exist", item)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 09:59:29 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MTTError {
|
2023-04-04 12:36:10 -04:00
|
|
|
code: ErrorCode,
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MTTError {
|
2023-04-04 12:36:10 -04:00
|
|
|
fn new<S>(msg: S) -> Self
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2023-04-04 09:59:29 -04:00
|
|
|
let text = msg.into();
|
|
|
|
Self {
|
2023-04-04 12:36:10 -04:00
|
|
|
code: ErrorCode::Undefined(text),
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-04 12:36:10 -04:00
|
|
|
|
|
|
|
fn from_code(code: ErrorCode) -> Self {
|
|
|
|
Self { code: code }
|
|
|
|
}
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for MTTError {}
|
|
|
|
|
|
|
|
impl fmt::Display for MTTError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-04-04 12:36:10 -04:00
|
|
|
write!(f, "{}", self.code)
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod errors {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_with_str() {
|
|
|
|
let msgs = ["one", "two"];
|
|
|
|
for msg in msgs {
|
|
|
|
let err = MTTError::new(msg);
|
|
|
|
assert_eq!(err.to_string(), msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_with_string() {
|
|
|
|
let msg = "three";
|
|
|
|
let err = MTTError::new(msg.to_string());
|
|
|
|
assert_eq!(err.to_string(), msg);
|
|
|
|
}
|
2023-04-04 12:36:10 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_from_code() {
|
|
|
|
let code = ErrorCode::Undefined("oops".to_string());
|
|
|
|
let err = MTTError::from_code(code);
|
|
|
|
match err.code {
|
|
|
|
ErrorCode::Undefined(_) => (),
|
|
|
|
_ => assert!(false, "{:?} is not undefined", err.code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_from_different_code() {
|
|
|
|
let code = ErrorCode::IncorrectDataType("nuts".to_string());
|
|
|
|
let err = MTTError::from_code(code);
|
|
|
|
match err.code {
|
|
|
|
ErrorCode::IncorrectDataType(_) => (),
|
|
|
|
_ => assert!(false, "{:?} is not incorrect data type", err.code),
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 12:36:10 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2023-04-04 09:59:29 -04:00
|
|
|
struct Store;
|
|
|
|
|
2023-04-04 12:36:10 -04:00
|
|
|
impl Store {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod stores {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create() {
|
|
|
|
Store::new();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2023-04-04 09:59:29 -04:00
|
|
|
enum DataType {
|
|
|
|
DBMap(Store),
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:36:10 -04:00
|
|
|
impl DataType {
|
|
|
|
fn new(dtype: &str) -> Result<DataType, MTTError> {
|
|
|
|
match dtype {
|
|
|
|
"store" => Ok(Self::DBMap(Store::new())),
|
|
|
|
_ => Err(MTTError::from_code(ErrorCode::IncorrectDataType(
|
|
|
|
dtype.to_string(),
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod datatypes {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_on_bad_datatype() {
|
|
|
|
let items = ["sdgthsth", "jfjty"];
|
|
|
|
for item in items {
|
|
|
|
match DataType::new(item) {
|
|
|
|
Ok(_) => assert!(false, "bad data types should return an error"),
|
|
|
|
Err(err) => match err.code {
|
|
|
|
ErrorCode::IncorrectDataType(dtype) => assert_eq!(dtype, item),
|
|
|
|
_ => assert!(false, "{:?} is not incorrect data type", err.code),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_store() {
|
|
|
|
let dtype = DataType::new("store").unwrap();
|
|
|
|
match dtype {
|
|
|
|
DataType::DBMap(_) => (),
|
|
|
|
_ => assert!(false, "{:?} is not incorrect data type", dtype),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 09:59:29 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MoreThanText {
|
|
|
|
session: Vec<String>,
|
|
|
|
channel: Sender<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MoreThanText {
|
2023-04-04 12:36:10 -04:00
|
|
|
async fn new() {}
|
|
|
|
|
2023-04-04 09:59:29 -04:00
|
|
|
async fn get_entry(&self, id: String) {
|
|
|
|
self.channel.send(id).await.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:36:10 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod mtt {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn create() {
|
|
|
|
MoreThanText::new().await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 09:59:29 -04:00
|
|
|
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
|
|
|
|
where
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
{
|
|
|
|
let data_dir = dir.into();
|
|
|
|
let (s, r) = unbounded();
|
|
|
|
spawn(async move {
|
|
|
|
loop {
|
|
|
|
r.recv().await.unwrap();
|
|
|
|
}
|
|
|
|
});
|
2023-04-04 12:36:10 -04:00
|
|
|
Ok(MoreThanText {
|
2023-04-04 09:59:29 -04:00
|
|
|
session: [ENTRY.to_string()].to_vec(),
|
2023-04-04 12:36:10 -04:00
|
|
|
channel: s,
|
2023-04-04 09:59:29 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod db_start_up {
|
|
|
|
use super::*;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn initial_session() {
|
|
|
|
let dir = tempdir().unwrap();
|
|
|
|
let mtt = start_db(dir.path()).await.unwrap();
|
|
|
|
assert_eq!(mtt.session, [ENTRY]);
|
|
|
|
}
|
|
|
|
}
|