2025-03-27 07:18:19 -04:00
|
|
|
mod client;
|
2025-04-10 13:42:43 -04:00
|
|
|
mod clock;
|
2025-04-11 22:06:36 -04:00
|
|
|
mod document;
|
2025-03-26 17:42:34 -04:00
|
|
|
mod field;
|
2025-03-29 09:22:53 -04:00
|
|
|
mod queue;
|
2025-04-05 09:50:54 -04:00
|
|
|
mod session;
|
2024-05-05 23:18:42 -04:00
|
|
|
|
2025-04-21 21:44:52 -04:00
|
|
|
use client::{Client, ClientChannel};
|
2025-04-10 13:42:43 -04:00
|
|
|
use clock::Clock;
|
2025-04-15 09:53:38 -04:00
|
|
|
use document::Document;
|
2025-03-26 17:42:34 -04:00
|
|
|
use field::Field;
|
2025-04-19 07:57:16 -04:00
|
|
|
use queue::{Message, MsgType, Queue};
|
2025-04-07 00:41:28 -04:00
|
|
|
use session::Session;
|
2025-04-19 07:57:16 -04:00
|
|
|
use uuid::Uuid;
|
2025-02-10 08:05:59 -05:00
|
|
|
|
2025-05-01 13:46:46 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2025-04-25 14:02:40 -04:00
|
|
|
pub enum ActionType {
|
|
|
|
Get,
|
|
|
|
Add,
|
|
|
|
Update,
|
|
|
|
}
|
|
|
|
|
2025-04-24 12:00:17 -04:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum ErrorType {
|
|
|
|
DocumentNotFound,
|
|
|
|
}
|
|
|
|
|
2025-04-25 14:02:40 -04:00
|
|
|
pub struct MTTReply {
|
|
|
|
document: String,
|
|
|
|
error_type: Option<ErrorType>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MTTReply {
|
|
|
|
fn new(msg: Message) -> Self {
|
|
|
|
Self {
|
|
|
|
document: match msg.get_data("doc") {
|
|
|
|
Some(doc) => doc.to_string(),
|
|
|
|
None => "".to_string(),
|
|
|
|
},
|
|
|
|
error_type: match msg.get_data("error_type") {
|
|
|
|
Some(err) => Some(err.to_error_type().unwrap()),
|
|
|
|
None => None,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_document(&self) -> String {
|
|
|
|
self.document.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_error(&self) -> Option<ErrorType> {
|
|
|
|
self.error_type.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod mtt_replies {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_reply_with_no_error() {
|
|
|
|
let mut msg = Message::new(MsgType::Document);
|
|
|
|
let content = format!("content-{}", Uuid::new_v4());
|
|
|
|
msg.add_data("doc", content.to_string());
|
|
|
|
let reply = MTTReply::new(msg);
|
|
|
|
assert!(reply.get_error().is_none());
|
|
|
|
assert_eq!(reply.get_document(), content);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_reply_with_error() {
|
|
|
|
let mut msg = Message::new(MsgType::Error);
|
|
|
|
msg.add_data("error_type", ErrorType::DocumentNotFound);
|
|
|
|
let reply = MTTReply::new(msg);
|
|
|
|
match reply.get_error() {
|
|
|
|
Some(err) => match err {
|
2025-04-26 10:29:58 -04:00
|
|
|
ErrorType::DocumentNotFound => {}
|
2025-04-25 14:02:40 -04:00
|
|
|
},
|
|
|
|
None => unreachable!("should return an error type"),
|
|
|
|
}
|
|
|
|
assert_eq!(reply.get_document(), "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_error() {
|
|
|
|
let msg = Message::new(MsgType::Document);
|
|
|
|
let reply = MTTReply::new(msg);
|
|
|
|
assert!(reply.get_error().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn some_error() {
|
|
|
|
let mut msg = Message::new(MsgType::Error);
|
|
|
|
msg.add_data("error_type", ErrorType::DocumentNotFound);
|
|
|
|
let reply = MTTReply::new(msg);
|
|
|
|
match reply.get_error() {
|
|
|
|
Some(err) => match err {
|
2025-04-26 10:29:58 -04:00
|
|
|
ErrorType::DocumentNotFound => {}
|
2025-04-25 14:02:40 -04:00
|
|
|
},
|
|
|
|
None => unreachable!("should return an error type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-22 10:53:05 -05:00
|
|
|
#[derive(Clone)]
|
2025-03-27 07:18:19 -04:00
|
|
|
pub struct MoreThanText {
|
2025-04-21 21:44:52 -04:00
|
|
|
client_channel: ClientChannel,
|
2025-03-27 07:18:19 -04:00
|
|
|
}
|
2025-02-22 10:53:05 -05:00
|
|
|
|
2025-03-26 17:42:34 -04:00
|
|
|
impl MoreThanText {
|
|
|
|
pub fn new() -> Self {
|
2025-04-03 21:48:16 -04:00
|
|
|
let queue = Queue::new();
|
2025-04-10 13:42:43 -04:00
|
|
|
Clock::start(queue.clone());
|
2025-04-15 09:53:38 -04:00
|
|
|
Document::start(queue.clone());
|
2025-04-07 00:41:28 -04:00
|
|
|
Session::start(queue.clone());
|
2025-03-30 11:38:41 -04:00
|
|
|
Self {
|
2025-04-21 21:44:52 -04:00
|
|
|
client_channel: Client::start(queue.clone()),
|
2025-03-30 11:38:41 -04:00
|
|
|
}
|
2025-02-11 11:33:54 -05:00
|
|
|
}
|
|
|
|
|
2025-04-19 07:57:16 -04:00
|
|
|
pub fn validate_session<F>(&mut self, session: Option<F>) -> Uuid
|
|
|
|
where
|
|
|
|
F: Into<Field>,
|
|
|
|
{
|
|
|
|
let mut msg = Message::new(MsgType::SessionValidate);
|
|
|
|
match session {
|
|
|
|
Some(id) => msg.add_data("sess_id", id.into()),
|
2025-04-20 10:15:55 -04:00
|
|
|
None => {}
|
2025-04-19 07:57:16 -04:00
|
|
|
}
|
2025-04-21 21:44:52 -04:00
|
|
|
let rx = self.client_channel.send(msg);
|
2025-04-19 07:57:16 -04:00
|
|
|
let reply = rx.recv().unwrap();
|
|
|
|
reply.get_data("sess_id").unwrap().to_uuid().unwrap()
|
|
|
|
}
|
2025-04-22 08:31:25 -04:00
|
|
|
|
2025-04-26 10:29:58 -04:00
|
|
|
pub fn get_document<S>(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2025-04-22 08:31:25 -04:00
|
|
|
let mut msg = Message::new(MsgType::DocumentRequest);
|
|
|
|
msg.add_data("sess_id", sess_id);
|
2025-05-01 13:46:46 -04:00
|
|
|
msg.add_data("action", action);
|
2025-04-25 14:02:40 -04:00
|
|
|
msg.add_data("name", doc_name.into());
|
2025-04-22 08:31:25 -04:00
|
|
|
let rx = self.client_channel.send(msg);
|
|
|
|
let reply = rx.recv().unwrap();
|
2025-04-25 14:02:40 -04:00
|
|
|
MTTReply::new(reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod mtt {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn session_id_is_unique() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
let input: Option<String> = None;
|
|
|
|
let mut ids: Vec<Uuid> = Vec::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
let id = mtt.validate_session(input.clone());
|
|
|
|
assert!(!ids.contains(&id));
|
|
|
|
ids.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn reuse_existing_session() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
let initial: Option<String> = None;
|
|
|
|
let id = mtt.validate_session(initial);
|
|
|
|
let output = mtt.validate_session(Some(id.clone()));
|
|
|
|
assert_eq!(output, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_root_document_with_str() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
let id = mtt.validate_session(Some(Uuid::new_v4()));
|
|
|
|
let output = mtt.get_document(id, ActionType::Get, "root");
|
|
|
|
assert!(output.get_error().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_root_document_with_string() {
|
|
|
|
let mut mtt = MoreThanText::new();
|
|
|
|
let id = mtt.validate_session(Some(Uuid::new_v4()));
|
|
|
|
let output = mtt.get_document(id, ActionType::Get, "root".to_string());
|
|
|
|
assert!(output.get_error().is_none());
|
2025-04-22 08:31:25 -04:00
|
|
|
}
|
2024-05-05 23:18:42 -04:00
|
|
|
}
|