From 4b0af019c8f04d09df42a98b6343cfab8a797400 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sun, 26 Mar 2023 10:30:22 -0400 Subject: [PATCH] Moved session inside database. --- src/morethantext/mod.rs | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/morethantext/mod.rs b/src/morethantext/mod.rs index 1ef1637..96072c4 100644 --- a/src/morethantext/mod.rs +++ b/src/morethantext/mod.rs @@ -108,7 +108,9 @@ impl FileData for DataType { } #[derive(Clone)] -pub struct MoreThanText; +pub struct MoreThanText { + session: Vec, +} impl MoreThanText { pub async fn new

(dir: P) -> Result @@ -118,19 +120,26 @@ impl MoreThanText { let pathbuf = dir.into(); let entry = pathbuf.as_path().join(ENTRY); match Entry::get(entry.clone()).await { - Ok(_) => Ok(Self {}), + Ok(_) => (), Err(_) => { let store = DataType::new("store").unwrap(); match Entry::new(entry, store).await { - Ok(_) => Ok(Self {}), + Ok(_) => (), Err(err) => { let mut error = DBError::from_code(ErrorCode::CacheReadWrite); error.add_source(err); - Err(error) + return Err(error); } } } } + Ok(Self { + session: [ENTRY.to_string()].to_vec(), + }) + } + + fn set_session(&mut self, sess: Vec) { + self.session = sess; } } @@ -294,7 +303,7 @@ mod datatype_file { } #[cfg(test)] -mod create { +mod db { use super::*; use async_std::fs::write; use std::error::Error; @@ -303,9 +312,7 @@ mod create { #[async_std::test] async fn create() { let dir = tempdir().unwrap(); - MoreThanText::new(dir.path().to_str().unwrap()) - .await - .unwrap(); + let mtt = MoreThanText::new(dir.path()).await.unwrap(); let epoint = dir.path().join(ENTRY); assert!( epoint.is_file(), @@ -317,6 +324,8 @@ mod create { entry.data().list(["database"].to_vec()).unwrap(), Vec::::new() ); + let sess = [ENTRY]; + assert_eq!(mtt.session, sess); } #[async_std::test] @@ -341,13 +350,16 @@ mod create { } #[async_std::test] - async fn existing_entry_point() { + async fn existing_entry_point() -> Result<(), DBError> { let dir = tempdir().unwrap(); let data = DataType::new("store").unwrap(); Entry::new(dir.path().join(ENTRY), data.clone()) .await .unwrap(); - MoreThanText::new(dir.path()).await.unwrap(); + match MoreThanText::new(dir.path()).await { + Ok(_) => Ok(()), + Err(err) => Err(err), + } } #[async_std::test] @@ -360,4 +372,13 @@ mod create { Err(_) => Ok(()), } } + + #[async_std::test] + async fn set_session() { + let dir = tempdir().unwrap(); + let mut mtt = MoreThanText::new(dir.path()).await.unwrap(); + let sess = ["different".to_string()]; + mtt.set_session(sess.to_vec()); + assert_eq!(mtt.session, sess); + } }