morethantext-web/src/morethantext/mod.rs

146 lines
3.2 KiB
Rust

mod cache;
mod database;
mod error;
mod store;
use async_std::{
channel::{unbounded, Sender},
path::PathBuf,
task::spawn,
};
use cache::Cache;
use database::Database;
use error::{ErrorCode, MTTError};
use store::Store;
const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
pub struct ToCacheMsg<D> {
data: D,
result: Sender<FromCache>,
}
#[derive(Debug)]
pub enum ToCache {
Get(ToCacheMsg<String>),
Commit(ToCacheMsg<Store>),
}
#[derive(Debug)]
pub enum FromCache {
Ok,
Str(Store),
Error(MTTError),
}
#[derive(Clone, Debug)]
pub struct Data<D> {
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,
}
}
fn from_data(data: D) -> Self {
Self {
id: None,
data: Some(data),
}
}
}
#[derive(Clone)]
pub struct MoreThanText {
to_cache: Sender<ToCache>,
entry: Data<Store>,
}
impl MoreThanText {
fn new(to_cache: Sender<ToCache>) -> Self {
Self {
to_cache: to_cache,
entry: Data::from_id(ENTRY),
}
}
async fn session(&self) -> Result<Store, MTTError> {
let (s, r) = unbounded();
let msg = ToCacheMsg {
data: 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),
_ => unreachable!(),
}
}
async fn commit(&self, store: Store) -> Result<(), MTTError> {
let (s, r) = unbounded();
let msg = ToCacheMsg {
data: store,
result: s,
};
self.to_cache.send(ToCache::Commit(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Ok => Ok(()),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod mtt {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn create_new() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.entry.id, Some(ENTRY.to_string()));
assert!(mtt.entry.data.is_none());
let store = mtt.session().await.unwrap();
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
}
#[async_std::test]
async fn commit_db() {
let dir = tempdir().unwrap();
let db = "fred";
let mtt = start_db(dir.path()).await.unwrap();
let mut store = mtt.session().await.unwrap();
store.add(db).unwrap();
mtt.commit(store).await.unwrap();
let store2 = mtt.session().await.unwrap();
assert_eq!(store2.list(), [db]);
}
}
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
where
P: Into<PathBuf>,
{
let path = dir.into();
let (s, r) = unbounded();
spawn(async move {
let mut cache = Cache::new(path).await;
cache.listen(r).await;
});
Ok(MoreThanText::new(s))
}