morethantext/src/lib.rs

122 lines
3.1 KiB
Rust
Raw Normal View History

2024-05-12 14:10:36 -04:00
mod cache;
mod messages;
2024-05-05 23:18:42 -04:00
2024-05-12 14:10:36 -04:00
use cache::Cache;
use messages::{ReceiveMsg, SendMsg, SessionRequest};
2024-03-19 19:54:14 -04:00
use std::{
2024-05-12 14:10:36 -04:00
sync::mpsc::{channel, Sender},
2024-03-19 19:54:14 -04:00
thread::spawn,
};
2024-05-12 14:10:36 -04:00
use uuid::Uuid;
2024-03-19 19:54:14 -04:00
2024-05-12 14:10:36 -04:00
/// Application connection to the database
2024-03-19 19:54:14 -04:00
#[derive(Clone)]
pub struct MoreThanText {
2024-05-12 14:10:36 -04:00
id: Option<Uuid>,
2024-03-19 19:54:14 -04:00
tx: Sender<SendMsg>,
2024-07-30 15:11:31 -04:00
nodes: Vec<String>
2024-03-19 19:54:14 -04:00
}
impl MoreThanText {
2024-05-12 14:10:36 -04:00
/// Create a MoreThanText database.
///
/// Example:
///
/// ```
/// use morethantext::MoreThanText;
///
2024-07-30 15:11:31 -04:00
/// MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
/// ```
2024-07-30 15:11:31 -04:00
pub fn new(_nodes: Vec<String>) -> Self {
2024-03-19 19:54:14 -04:00
let (tx, rx) = channel();
spawn(move || {
2024-03-29 07:51:14 -04:00
let mut cache = Cache::new(rx);
cache.listen();
2024-03-19 19:54:14 -04:00
});
2024-07-30 15:11:31 -04:00
Self { id: None, tx: tx, nodes: Vec::new() }
2024-05-12 14:10:36 -04:00
}
/// Opens an existing or new session with the database.
///
/// If the string is None, incorrect, or expired,
/// a new session will be created.
///
/// Example:
///
/// ```
/// use morethantext::MoreThanText;
///
2024-07-30 15:11:31 -04:00
/// let mut mtt = MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
/// mtt.open_session(None);
/// mtt.open_session(Some("7b1ff340-7dfa-4f29-b144-601384e54423".to_string()));
/// ```
pub fn open_session(&mut self, id: Option<String>) {
let (tx, rx) = channel();
2024-05-12 14:22:11 -04:00
let request = SessionRequest { id: id, tx: tx };
2024-05-12 14:10:36 -04:00
self.tx.send(SendMsg::OpenSession(request)).unwrap();
match rx.recv().unwrap() {
ReceiveMsg::Session(sid) => self.id = Some(sid),
}
2024-03-19 19:54:14 -04:00
}
2024-05-12 14:10:36 -04:00
/// Get the session id
///
/// Example:
///
/// ```
/// use morethantext::MoreThanText;
///
2024-07-30 15:11:31 -04:00
/// let mut mtt = MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
/// mtt.get_id();
/// ```
pub fn get_id(&self) -> String {
match self.id {
Some(sid) => sid.to_string(),
None => "none".to_string(),
}
2024-03-19 19:54:14 -04:00
}
}
#[cfg(test)]
2024-05-12 14:10:36 -04:00
mod mtt_client {
2024-03-19 19:54:14 -04:00
use super::*;
#[test]
2024-05-12 14:10:36 -04:00
fn uniques_ids() {
2024-07-30 15:11:31 -04:00
let mut mtt = MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
let mut ids: Vec<Uuid> = Vec::new();
2024-03-19 19:54:14 -04:00
for _ in 1..10 {
2024-05-12 14:10:36 -04:00
mtt.open_session(None);
let id = mtt.id.clone().unwrap();
if ids.contains(&id) {
assert!(false, "{} is a duplicate id", id);
2024-03-19 19:54:14 -04:00
}
2024-05-12 14:10:36 -04:00
ids.push(id);
2024-03-19 19:54:14 -04:00
}
}
#[test]
2024-05-12 14:10:36 -04:00
fn existing_ids_are_reused() {
2024-07-30 15:11:31 -04:00
let mut mtt = MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
mtt.open_session(None);
let holder = mtt.id.clone().unwrap().to_string();
mtt.open_session(Some(holder.clone()));
assert_eq!(mtt.id.clone().unwrap().to_string(), holder);
2024-03-19 19:54:14 -04:00
}
2024-05-05 23:18:42 -04:00
#[test]
2024-05-12 14:10:36 -04:00
fn bad_ids_generate_new_ones() {
2024-07-30 15:11:31 -04:00
let mut mtt = MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
mtt.open_session(Some("bad test string".to_string()));
assert!(mtt.id.is_some());
2024-05-05 23:18:42 -04:00
}
#[test]
2024-05-12 14:10:36 -04:00
fn incorrect_ids_generate_new_ones() {
2024-07-30 15:11:31 -04:00
let mut mtt = MoreThanText::new(Vec::new());
2024-05-12 14:10:36 -04:00
let holder = Uuid::new_v4();
mtt.open_session(Some(holder.clone().to_string()));
assert_ne!(mtt.id, Some(holder));
2024-05-05 23:18:42 -04:00
}
}