Pulled back to use async channels.

This commit is contained in:
Jeff Baskin 2023-04-04 09:59:29 -04:00
parent 685ddfe32d
commit 659a2758bb
2 changed files with 103 additions and 2 deletions

View File

@ -7,7 +7,7 @@ use tide::{
mod morethantext;
mod settings;
use morethantext::MoreThanText;
use morethantext::{start_db, MoreThanText};
use settings::Settings;
#[async_std::main]
@ -20,7 +20,7 @@ async fn main() -> tide::Result<()> {
}
async fn app_setup(data_dir: &str) -> tide::Server<MoreThanText> {
let db = MoreThanText::new(data_dir).await.unwrap();
let db = start_db(data_dir).await.unwrap();
let mut app = tide::with_state(db);
app.at("/").get(home);
app.with(

101
src/morethantext/mod.rs Normal file
View File

@ -0,0 +1,101 @@
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]);
}
}