2024-11-06 21:05:52 -05:00
|
|
|
mod client;
|
2024-11-09 07:56:49 -05:00
|
|
|
mod record;
|
2024-11-06 21:05:52 -05:00
|
|
|
mod message;
|
|
|
|
mod router;
|
|
|
|
mod session;
|
2024-05-05 23:18:42 -04:00
|
|
|
|
2024-11-06 21:05:52 -05:00
|
|
|
use client::{Client, ClientMsg};
|
|
|
|
use message::{Message, MsgData};
|
|
|
|
use router::Router;
|
|
|
|
use session::{Session, SessionData, SessionMsg};
|
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
|
|
|
};
|
|
|
|
|
2024-11-06 21:05:52 -05:00
|
|
|
/// Support functions for Messages.
|
|
|
|
pub trait Msg {
|
|
|
|
fn to_msgdata(&self) -> MsgData;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_message {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub enum Tester {
|
|
|
|
Test1,
|
|
|
|
Test2,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Msg for Tester {
|
|
|
|
fn to_msgdata(&self) -> MsgData {
|
|
|
|
match self {
|
|
|
|
Tester::Test1 => MsgData::Test1,
|
|
|
|
Tester::Test2 => MsgData::Test2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Application client to MoreThanText
|
2024-03-19 19:54:14 -04:00
|
|
|
pub struct MoreThanText {
|
2024-11-06 21:05:52 -05:00
|
|
|
session: Option<SessionData>,
|
|
|
|
tx: Sender<Message>,
|
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-11-06 21:05:52 -05:00
|
|
|
/// MoreThanText::new();
|
2024-05-12 14:10:36 -04:00
|
|
|
/// ```
|
2024-11-06 21:05:52 -05:00
|
|
|
pub fn new() -> Self {
|
2024-03-19 19:54:14 -04:00
|
|
|
let (tx, rx) = channel();
|
2024-11-06 21:05:52 -05:00
|
|
|
let mut senders = Vec::new();
|
|
|
|
senders.push(Client::start(tx.clone()));
|
|
|
|
senders.push(Session::start(tx.clone()));
|
|
|
|
Router::start(senders, rx);
|
|
|
|
Self {
|
|
|
|
session: None,
|
|
|
|
tx: tx,
|
|
|
|
}
|
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,
|
2024-11-06 21:05:52 -05:00
|
|
|
/// a new session will be created.
|
2024-05-12 14:10:36 -04:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use morethantext::MoreThanText;
|
|
|
|
///
|
2024-11-06 21:05:52 -05:00
|
|
|
/// let mut mtt = MoreThanText::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-11-06 21:05:52 -05:00
|
|
|
let req = ClientMsg::OpenSession(id, tx);
|
|
|
|
let msg = Message::new(&req);
|
|
|
|
self.tx.send(msg).unwrap();
|
|
|
|
match rx.recv().unwrap().get_message() {
|
|
|
|
MsgData::Session(data) => {
|
|
|
|
match data {
|
|
|
|
SessionMsg::Opened(sess) => self.session = Some(sess.clone()),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
};
|
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-11-06 21:05:52 -05:00
|
|
|
/// let mut mtt = MoreThanText::new();
|
|
|
|
/// let id = mtt.get_id();
|
2024-05-12 14:10:36 -04:00
|
|
|
/// ```
|
|
|
|
pub fn get_id(&self) -> String {
|
2024-11-06 21:05:52 -05:00
|
|
|
match &self.session {
|
|
|
|
Some(id) => id.to_string(),
|
|
|
|
None => "".to_string(),
|
2024-05-12 14:10:36 -04:00
|
|
|
}
|
2024-03-19 19:54:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 21:28:44 -05:00
|
|
|
impl Clone for MoreThanText {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
session: None,
|
|
|
|
tx: self.tx.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-06 21:05:52 -05:00
|
|
|
fn default_values() {
|
|
|
|
let mtt = MoreThanText::new();
|
|
|
|
assert!(mtt.session.is_none());
|
2024-03-19 19:54:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-11-06 21:05:52 -05:00
|
|
|
fn new_session() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
2024-05-12 14:10:36 -04:00
|
|
|
mtt.open_session(None);
|
2024-11-06 21:05:52 -05:00
|
|
|
assert!(mtt.session.is_some());
|
2024-03-19 19:54:14 -04:00
|
|
|
}
|
2024-05-05 23:18:42 -04:00
|
|
|
|
|
|
|
#[test]
|
2024-11-06 21:05:52 -05:00
|
|
|
fn session_ids_are_unique() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
mtt.open_session(None);
|
|
|
|
let id1 = mtt.get_id();
|
|
|
|
mtt.open_session(None);
|
|
|
|
assert_ne!(mtt.get_id(), id1);
|
2024-05-05 23:18:42 -04:00
|
|
|
}
|
2024-11-06 21:28:44 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cloned_clients_have_no_session() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
mtt.open_session(None);
|
|
|
|
let result = mtt.clone();
|
|
|
|
assert!(result.session.is_none());
|
|
|
|
}
|
2024-05-05 23:18:42 -04:00
|
|
|
}
|