From dd1c45ddbeff08cfe77f84a2b116875802ac024c Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 17 Jun 2023 12:01:58 -0400 Subject: [PATCH] Continuing channel setup. --- src/morethantext/cache.rs | 59 +++++++++++++++++++++++++++++++++++++-- src/morethantext/error.rs | 27 ++++++++++++++++-- 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/morethantext/cache.rs b/src/morethantext/cache.rs index 62ad209..d7314f2 100644 --- a/src/morethantext/cache.rs +++ b/src/morethantext/cache.rs @@ -1,3 +1,4 @@ +use super::{ErrorCode, MTTError, Store, ENTRY}; use async_std::{channel::Receiver, path::PathBuf}; pub struct Cache; @@ -15,6 +16,18 @@ impl Cache { listener.recv().await.unwrap(); } } + + pub fn get(&self, id: S) -> Result + where + S: Into, + { + let idd = id.into(); + if idd == ENTRY { + Ok(Store::new()) + } else { + Err(MTTError::from_code(ErrorCode::IDNotFound(idd))) + } + } } #[cfg(test)] @@ -23,8 +36,50 @@ mod engine { use tempfile::tempdir; #[async_std::test] - async fn create() { + async fn get_entry() { let dir = tempdir().unwrap(); - Cache::new(dir.path()).await; + let cache = Cache::new(dir.path()).await; + let store = cache.get(ENTRY).unwrap(); + let expected: Vec = Vec::new(); + assert_eq!(store.list(), expected); + } + + #[async_std::test] + async fn get_bad_entry() -> Result<(), MTTError> { + let dir = tempdir().unwrap(); + 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 { + ErrorCode::IDNotFound(_) => { + assert!( + err.to_string().contains(id), + "Had error: {}, Did not contain: {}", + err.to_string(), + id + ); + } + _ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))), + }, + } + } + Ok(()) + } +} + +#[cfg(test)] +mod messages { + use super::*; + use super::super::start_db; + use tempfile::tempdir; + + #[async_std::test] + async fn get_the_store() { + 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(); } } diff --git a/src/morethantext/error.rs b/src/morethantext/error.rs index 682c980..f4e080a 100644 --- a/src/morethantext/error.rs +++ b/src/morethantext/error.rs @@ -4,12 +4,15 @@ use std::{error::Error, fmt}; pub enum ErrorCode { // General Undefined(String), + // Cache + IDNotFound(String), } impl fmt::Display for ErrorCode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ErrorCode::Undefined(msg) => write!(f, "{}", msg), + ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id), } } } @@ -26,15 +29,23 @@ mod errorcodes { assert_eq!(err.to_string(), item); } } + + #[test] + fn cache_missing() { + for item in ITEMS { + let err = ErrorCode::IDNotFound(item.to_string()); + assert_eq!(err.to_string(), format!("ID '{}' not found", item)); + } + } } #[derive(Debug)] pub struct MTTError { - code: ErrorCode, + pub code: ErrorCode, } impl MTTError { - fn new(msg: S) -> Self + pub fn new(msg: S) -> Self where S: Into, { @@ -44,7 +55,7 @@ impl MTTError { } } - fn from_code(code: ErrorCode) -> Self { + pub fn from_code(code: ErrorCode) -> Self { Self { code: code } } } @@ -86,4 +97,14 @@ mod errors { _ => assert!(false, "{:?} is not undefined", err.code), } } + + #[test] + fn create_missing_id_from_code() { + let code = ErrorCode::IDNotFound("123".to_string()); + let err = MTTError::from_code(code); + match err.code { + ErrorCode::IDNotFound(_) => (), + _ => assert!(false, "{:?} is not undefined", err.code), + } + } }