2022-08-06 12:03:47 -04:00
|
|
|
pub mod error;
|
2022-12-02 10:34:45 -05:00
|
|
|
use async_std::{fs::create_dir, path::Path};
|
2022-08-06 12:03:47 -04:00
|
|
|
|
2022-09-27 07:31:59 -04:00
|
|
|
use error::DBError;
|
2022-12-02 10:34:45 -05:00
|
|
|
|
|
|
|
const DATA: &str = "data";
|
2022-08-06 12:03:47 -04:00
|
|
|
|
2022-07-18 17:24:45 -04:00
|
|
|
#[derive(Clone)]
|
2022-12-02 10:34:45 -05:00
|
|
|
pub struct MoreThanText;
|
2022-07-12 16:55:20 -04:00
|
|
|
|
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) => {
|
|
|
|
let mut error = DBError::new("failed to initialize");
|
|
|
|
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-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2022-12-02 10:34:45 -05:00
|
|
|
mod init {
|
2022-07-12 16:55:20 -04:00
|
|
|
use super::*;
|
2022-12-02 10:34:45 -05:00
|
|
|
use std::error::Error;
|
|
|
|
use tempfile::tempdir;
|
2022-07-12 16:55:20 -04:00
|
|
|
|
|
|
|
#[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:38:58 -04:00
|
|
|
}
|
|
|
|
|
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-07-12 16:55:20 -04:00
|
|
|
}
|
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-02 10:34:45 -05:00
|
|
|
assert_eq!(err.to_string(), "failed to initialize");
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|