morethantext-web/src/morethantext/mod.rs

114 lines
2.9 KiB
Rust
Raw Normal View History

2022-08-06 12:03:47 -04:00
pub mod error;
2022-12-04 11:09:56 -05:00
use async_std::{fs::create_dir, path::Path};
2022-09-27 07:31:59 -04:00
use error::DBError;
2022-12-04 11:09:56 -05:00
use rand::{distributions::Alphanumeric, thread_rng, Rng};
2022-12-02 10:34:45 -05:00
const DATA: &str = "data";
2022-08-06 12:03:47 -04:00
2022-12-03 09:15:58 -05:00
enum CacheEntry {
Raw(String),
}
2022-07-18 17:24:45 -04:00
#[derive(Clone)]
2022-12-02 10:34:45 -05:00
pub struct MoreThanText;
2022-07-18 17:24:45 -04:00
impl MoreThanText {
2022-12-02 10:34:45 -05:00
pub async fn new(dir: &str) -> Result<Self, DBError> {
let data_dir = Path::new(dir).join(DATA);
if !data_dir.is_dir().await {
match create_dir(data_dir).await {
Ok(_) => (),
Err(err) => {
2022-12-03 08:26:21 -05:00
let mut error = DBError::new("failed to create data directory");
2022-12-02 10:34:45 -05:00
error.add_source(err);
return Err(error);
}
2022-08-06 12:03:47 -04:00
}
}
2022-12-02 10:34:45 -05:00
Ok(Self {})
2022-08-07 09:29:08 -04:00
}
2022-12-03 09:15:58 -05:00
async fn add_entry(&self, _entry: CacheEntry) -> String {
2022-12-04 11:09:56 -05:00
let id: String = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
return id;
2022-12-03 09:15:58 -05:00
}
}
2022-12-03 10:22:54 -05:00
#[cfg(test)]
mod setup {
use super::*;
use tempfile::{tempdir, TempDir};
pub struct MTT {
pub db: MoreThanText,
pub dir: TempDir,
}
impl MTT {
pub async fn new() -> Self {
let dir = tempdir().unwrap();
let db = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
Self { db: db, dir: dir }
}
}
}
#[cfg(test)]
2022-12-02 10:34:45 -05:00
mod init {
use super::*;
2022-12-02 10:34:45 -05:00
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
2022-12-02 10:34:45 -05:00
async fn create_data_dir() {
let dir = tempdir().unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let data_dir = dir.path().join(DATA);
assert!(data_dir.is_dir(), "Did not create the data directory.");
dir.close().unwrap();
}
2022-08-06 12:03:47 -04:00
#[async_std::test]
2022-12-02 10:34:45 -05:00
async fn existing_data_dir() {
let dir = tempdir().unwrap();
let data_dir = dir.path().join(DATA);
create_dir(data_dir).await.unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
dir.close().unwrap();
}
2022-08-07 09:29:08 -04:00
#[async_std::test]
2022-12-02 10:34:45 -05:00
async fn bad_data_dir() {
match MoreThanText::new("kljsdgfhslkfrh").await {
Ok(_) => assert!(false, "This test should fail to create a data directory"),
2022-09-27 07:31:59 -04:00
Err(err) => {
2022-12-03 08:26:21 -05:00
assert_eq!(err.to_string(), "failed to create data directory");
2022-12-02 10:34:45 -05:00
assert!(err.source().is_some(), "Must include the source error.");
2022-09-27 07:31:59 -04:00
}
2022-12-02 10:34:45 -05:00
};
2022-08-05 16:47:01 -04:00
}
}
2022-12-03 09:15:58 -05:00
#[cfg(test)]
mod cache {
use super::*;
2022-12-03 10:22:54 -05:00
use setup::MTT;
2022-12-03 09:15:58 -05:00
#[async_std::test]
2022-12-04 11:09:56 -05:00
async fn ids_are_random() {
2022-12-03 10:22:54 -05:00
let mtt = MTT::new().await;
2022-12-04 11:09:56 -05:00
let data1 = CacheEntry::Raw("one".to_string());
let data2 = CacheEntry::Raw("two".to_string());
let id1 = mtt.db.add_entry(data1).await;
let id2 = mtt.db.add_entry(data2).await;
assert_ne!(id1, id2, "Ids should be unique.")
2022-12-03 09:15:58 -05:00
}
}