2023-05-29 15:42:32 -04:00
|
|
|
mod cache;
|
2023-06-22 11:08:40 -04:00
|
|
|
mod database;
|
2023-05-29 15:42:32 -04:00
|
|
|
mod error;
|
2023-05-30 00:18:13 -04:00
|
|
|
mod store;
|
2023-05-29 15:42:32 -04:00
|
|
|
|
2023-04-04 09:59:29 -04:00
|
|
|
use async_std::{
|
2023-05-29 15:42:32 -04:00
|
|
|
channel::{unbounded, Sender},
|
2023-04-04 09:59:29 -04:00
|
|
|
path::PathBuf,
|
|
|
|
task::spawn,
|
|
|
|
};
|
2023-05-29 15:42:32 -04:00
|
|
|
use cache::Cache;
|
2023-06-22 11:08:40 -04:00
|
|
|
use database::Database;
|
2023-05-29 15:42:32 -04:00
|
|
|
use error::{ErrorCode, MTTError};
|
2023-05-30 00:18:13 -04:00
|
|
|
use store::Store;
|
2023-04-23 13:13:38 -04:00
|
|
|
|
2023-06-03 15:27:26 -04:00
|
|
|
const ENTRY: &str = "EntryPoint";
|
|
|
|
|
2023-06-20 11:51:32 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CacheGet {
|
|
|
|
id: String,
|
|
|
|
result: Sender<FromCache>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ToCache {
|
|
|
|
Get(CacheGet),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum FromCache {
|
|
|
|
Str(Store),
|
|
|
|
Error(MTTError),
|
|
|
|
}
|
|
|
|
|
2023-04-23 13:13:38 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2023-06-23 08:30:49 -04:00
|
|
|
pub struct Data<D> {
|
2023-06-03 15:27:26 -04:00
|
|
|
id: Option<String>,
|
|
|
|
data: Option<D>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<D> Data<D> {
|
|
|
|
fn from_id<S>(id: S) -> Self
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
id: Some(id.into()),
|
|
|
|
data: None,
|
|
|
|
}
|
|
|
|
}
|
2023-06-23 08:30:49 -04:00
|
|
|
|
|
|
|
fn from_data(data: D) -> Self {
|
|
|
|
Self {
|
|
|
|
id: None,
|
|
|
|
data: Some(data),
|
|
|
|
}
|
|
|
|
}
|
2023-04-08 15:04:04 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 09:59:29 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MoreThanText {
|
2023-06-20 11:51:32 -04:00
|
|
|
to_cache: Sender<ToCache>,
|
2023-06-03 15:27:26 -04:00
|
|
|
entry: Data<Store>,
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MoreThanText {
|
2023-06-20 11:51:32 -04:00
|
|
|
fn new(to_cache: Sender<ToCache>) -> Self {
|
2023-06-03 15:27:26 -04:00
|
|
|
Self {
|
|
|
|
to_cache: to_cache,
|
|
|
|
entry: Data::from_id(ENTRY),
|
|
|
|
}
|
2023-04-12 18:33:39 -04:00
|
|
|
}
|
|
|
|
|
2023-06-20 11:51:32 -04:00
|
|
|
async fn session(&self) -> Result<Store, MTTError> {
|
|
|
|
let (s, r) = unbounded();
|
|
|
|
let msg = CacheGet {
|
|
|
|
id: ENTRY.to_string(),
|
|
|
|
result: s,
|
|
|
|
};
|
|
|
|
self.to_cache.send(ToCache::Get(msg)).await.unwrap();
|
|
|
|
match r.recv().await.unwrap() {
|
|
|
|
FromCache::Str(store) => Ok(store),
|
|
|
|
FromCache::Error(err) => Err(err),
|
|
|
|
}
|
2023-04-08 15:04:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-05-29 15:42:32 -04:00
|
|
|
mod mtt {
|
2023-04-08 15:04:04 -04:00
|
|
|
use super::*;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
2023-04-18 09:02:18 -04:00
|
|
|
#[async_std::test]
|
2023-05-29 15:42:32 -04:00
|
|
|
async fn create_new() {
|
2023-04-18 09:02:18 -04:00
|
|
|
let dir = tempdir().unwrap();
|
2023-05-29 15:42:32 -04:00
|
|
|
let mtt = start_db(dir.path()).await.unwrap();
|
2023-06-03 15:27:26 -04:00
|
|
|
assert_eq!(mtt.entry.id, Some(ENTRY.to_string()));
|
|
|
|
assert!(mtt.entry.data.is_none());
|
2023-06-20 11:51:32 -04:00
|
|
|
let store = mtt.session().await.unwrap();
|
2023-06-03 15:27:26 -04:00
|
|
|
let expected: Vec<String> = Vec::new();
|
|
|
|
assert_eq!(store.list(), expected);
|
2023-04-12 18:33:39 -04:00
|
|
|
}
|
2023-04-04 12:36:10 -04:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:42:32 -04:00
|
|
|
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
|
2023-04-04 09:59:29 -04:00
|
|
|
where
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
{
|
2023-05-29 15:42:32 -04:00
|
|
|
let path = dir.into();
|
2023-04-04 09:59:29 -04:00
|
|
|
let (s, r) = unbounded();
|
|
|
|
spawn(async move {
|
2023-05-29 15:42:32 -04:00
|
|
|
let cache = Cache::new(path).await;
|
|
|
|
cache.listen(r).await;
|
2023-04-04 09:59:29 -04:00
|
|
|
});
|
2023-05-29 15:42:32 -04:00
|
|
|
Ok(MoreThanText::new(s))
|
2023-04-04 09:59:29 -04:00
|
|
|
}
|