From ecfc8fdf90e69e074c9516e2191ed4efbdb7390c Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Tue, 20 Jun 2023 11:51:32 -0400 Subject: [PATCH] Got the client and engine connecting. --- src/morethantext/cache.rs | 72 +++++++++++++++++++++++++++++++-------- src/morethantext/mod.rs | 36 +++++++++++++++++--- src/morethantext/store.rs | 2 +- 3 files changed, 90 insertions(+), 20 deletions(-) diff --git a/src/morethantext/cache.rs b/src/morethantext/cache.rs index d7314f2..6f27d40 100644 --- a/src/morethantext/cache.rs +++ b/src/morethantext/cache.rs @@ -1,4 +1,4 @@ -use super::{ErrorCode, MTTError, Store, ENTRY}; +use super::{ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY}; use async_std::{channel::Receiver, path::PathBuf}; pub struct Cache; @@ -11,21 +11,25 @@ impl Cache { Self {} } - pub async fn listen(&self, listener: Receiver) { + pub async fn listen(&self, listener: Receiver) { loop { - listener.recv().await.unwrap(); + match listener.recv().await.unwrap() { + ToCache::Get(data) => { + data.result.send(self.get(data.id)).await.unwrap(); + } + } } } - pub fn get(&self, id: S) -> Result + pub fn get(&self, id: S) -> FromCache where S: Into, { let idd = id.into(); if idd == ENTRY { - Ok(Store::new()) + FromCache::Str(Store::new()) } else { - Err(MTTError::from_code(ErrorCode::IDNotFound(idd))) + FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))) } } } @@ -39,9 +43,12 @@ mod engine { async fn get_entry() { let dir = tempdir().unwrap(); let cache = Cache::new(dir.path()).await; - let store = cache.get(ENTRY).unwrap(); let expected: Vec = Vec::new(); - assert_eq!(store.list(), expected); + let result = cache.get(ENTRY); + match result { + FromCache::Str(store) => assert_eq!(store.list(), expected), + _ => assert!(false, "{:?} should be FromCache::Str", result), + } } #[async_std::test] @@ -50,9 +57,9 @@ mod engine { let cache = Cache::new(dir.path()).await; let ids = ["bad1", "bad2"]; for id in ids { - match cache.get(id) { - Ok(_) => return Err(MTTError::new("should have errored")), - Err(err) => match err.code { + let output = cache.get(id); + match output { + FromCache::Error(err) => match err.code { ErrorCode::IDNotFound(_) => { assert!( err.to_string().contains(id), @@ -63,6 +70,12 @@ mod engine { } _ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))), }, + _ => { + return Err(MTTError::new(format!( + "{:?} is not FromCache::Error", + output + ))) + } } } Ok(()) @@ -71,8 +84,11 @@ mod engine { #[cfg(test)] mod messages { - use super::*; - use super::super::start_db; + use super::{ + super::{start_db, CacheGet}, + *, + }; + use async_std::channel::unbounded; use tempfile::tempdir; #[async_std::test] @@ -80,6 +96,34 @@ mod messages { let dir = tempdir().unwrap(); let mtt = start_db(dir.path()).await.unwrap(); let in_s = mtt.to_cache.clone(); - in_s.send(ENTRY.to_string()).await.unwrap(); + let (out_s, out_r) = unbounded(); + let msg = CacheGet { + id: ENTRY.to_string(), + result: out_s, + }; + in_s.send(ToCache::Get(msg)).await.unwrap(); + let result = out_r.recv().await.unwrap(); + match result { + FromCache::Str(_) => (), + _ => assert!(false, "{:?} is not FromCache::Str", result), + } + } + + #[async_std::test] + async fn get_bad_id() { + let dir = tempdir().unwrap(); + let mtt = start_db(dir.path()).await.unwrap(); + let in_s = mtt.to_cache.clone(); + let (out_s, out_r) = unbounded(); + let msg = CacheGet { + id: "bad_id!".to_string(), + result: out_s, + }; + in_s.send(ToCache::Get(msg)).await.unwrap(); + let output = out_r.recv().await.unwrap(); + match output { + FromCache::Error(_) => (), + _ => assert!(false, "{:?} is not FromCache::Error", output), + } } } diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index e18b4e6..4ab55ea 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -13,6 +13,23 @@ use store::Store; const ENTRY: &str = "EntryPoint"; +#[derive(Debug)] +pub struct CacheGet { + id: String, + result: Sender, +} + +#[derive(Debug)] +pub enum ToCache { + Get(CacheGet), +} + +#[derive(Debug)] +pub enum FromCache { + Str(Store), + Error(MTTError), +} + #[derive(Clone, Debug)] struct Data { id: Option, @@ -33,20 +50,29 @@ impl Data { #[derive(Clone)] pub struct MoreThanText { - to_cache: Sender, + to_cache: Sender, entry: Data, } impl MoreThanText { - fn new(to_cache: Sender) -> Self { + fn new(to_cache: Sender) -> Self { Self { to_cache: to_cache, entry: Data::from_id(ENTRY), } } - async fn session(&self) -> Store { - Store::new() + async fn session(&self) -> Result { + 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), + } } } @@ -61,7 +87,7 @@ mod mtt { 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; + let store = mtt.session().await.unwrap(); let expected: Vec = Vec::new(); assert_eq!(store.list(), expected); } diff --git a/src/morethantext/store.rs b/src/morethantext/store.rs index 26dcb3a..14947ac 100644 --- a/src/morethantext/store.rs +++ b/src/morethantext/store.rs @@ -1,4 +1,4 @@ -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Store; impl Store {