morethantext-web/src/morethantext/mod.rs

102 lines
1.8 KiB
Rust
Raw Normal View History

2023-04-04 09:59:29 -04:00
use async_std::{
channel::{unbounded, Sender},
path::PathBuf,
task::spawn,
};
use std::{error::Error, fmt};
const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
pub struct MTTError {
msg: String,
}
impl MTTError {
fn new<S>(msg: S) -> Self where S: Into<String> {
let text = msg.into();
Self {
msg: text,
}
}
}
impl Error for MTTError {}
impl fmt::Display for MTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
#[cfg(test)]
mod errors {
use super::*;
#[test]
fn create_with_str() {
let msgs = ["one", "two"];
for msg in msgs {
let err = MTTError::new(msg);
assert_eq!(err.to_string(), msg);
}
}
#[test]
fn create_with_string() {
let msg = "three";
let err = MTTError::new(msg.to_string());
assert_eq!(err.to_string(), msg);
}
}
#[derive(Clone)]
struct Store;
#[derive(Clone)]
enum DataType {
DBMap(Store),
}
#[derive(Clone)]
pub struct MoreThanText {
session: Vec<String>,
channel: Sender<String>,
}
impl MoreThanText {
async fn get_entry(&self, id: String) {
self.channel.send(id).await.unwrap();
}
}
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
where
P: Into<PathBuf>,
{
let data_dir = dir.into();
let (s, r) = unbounded();
spawn(async move {
loop {
r.recv().await.unwrap();
}
});
Ok(MoreThanText {
session: [ENTRY.to_string()].to_vec(),
channel: s,
})
}
#[cfg(test)]
mod db_start_up {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn initial_session() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.session, [ENTRY]);
}
}