Moved session inside database.

This commit is contained in:
Jeff Baskin 2023-03-26 10:30:22 -04:00
parent ee45375085
commit 4b0af019c8
1 changed files with 31 additions and 10 deletions

View File

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