Added ability to add page, but hangs in main.

This commit is contained in:
2025-05-01 13:46:46 -04:00
parent 1bfbfc5e44
commit 4fbbd30c4b
6 changed files with 131 additions and 24 deletions

View File

@@ -1,8 +1,9 @@
use crate::{
queue::{Message, MsgType, Queue},
ErrorType,
ActionType, ErrorType,
};
use std::{
collections::HashMap,
sync::mpsc::{channel, Receiver},
thread::spawn,
};
@@ -10,13 +11,17 @@ use std::{
const RESPONDS_TO: [MsgType; 1] = [MsgType::DocumentRequest];
pub struct Document {
data: HashMap<String, String>,
queue: Queue,
rx: Receiver<Message>,
}
impl Document {
fn new(queue: Queue, rx: Receiver<Message>) -> Self {
let mut data = HashMap::new();
data.insert("root".to_string(), "Something goes here.".to_string());
Self {
data: data,
queue: queue,
rx: rx,
}
@@ -34,22 +39,42 @@ impl Document {
fn listen(&mut self) {
loop {
let msg = self.rx.recv().unwrap();
let mut reply = match msg.get_data("name") {
Some(name) => {
if name.to_string() == "root" {
msg.reply(MsgType::Document)
} else {
let mut output = msg.reply(MsgType::Error);
output.add_data("error_type", ErrorType::DocumentNotFound);
output
}
}
None => msg.reply(MsgType::Document),
};
reply.add_data("doc", "Something goes hwew");
self.queue.send(reply).unwrap();
match msg.get_data("action") {
Some(action) => match action.to_action().unwrap() {
ActionType::Add => self.add(msg),
_ => self.get(msg),
},
None => self.get(msg),
}
}
}
fn add(&mut self, msg: Message) {
let name = msg.get_data("name").unwrap().to_string();
let doc = msg.get_data("doc").unwrap().to_string();
self.data.insert(name, doc);
self.queue.send(msg.reply(MsgType::ActionOk)).unwrap();
}
fn get(&self, msg: Message) {
let name = match msg.get_data("name") {
Some(doc) => doc.to_string(),
None => "root".to_string(),
};
let mut reply = match self.data.get(&name) {
Some(data) => {
let mut holder = msg.reply(MsgType::Document);
holder.add_data("doc", data.clone());
holder
},
None => {
let mut holder = msg.reply(MsgType::Error);
holder.add_data("error_type", ErrorType::DocumentNotFound);
holder
}
};
self.queue.send(reply).unwrap();
}
}
#[cfg(test)]
@@ -63,7 +88,7 @@ pub mod documents {
fn setup_document() -> (Queue, Receiver<Message>) {
let queue = Queue::new();
let (tx, rx) = channel();
queue.add(tx, [MsgType::Document, MsgType::Error].to_vec());
queue.add(tx, [MsgType::ActionOk, MsgType::Document, MsgType::Error].to_vec());
Document::start(queue.clone());
(queue, rx)
}
@@ -113,7 +138,7 @@ pub mod documents {
let mut msg = Message::new(MsgType::DocumentRequest);
msg.add_data("name", "root");
queue.send(msg);
let reply = rx.recv().unwrap();
let reply = rx.recv_timeout(TIMEOUT).unwrap();
match reply.get_msg_type() {
MsgType::Document => {}
_ => unreachable!(
@@ -122,4 +147,39 @@ pub mod documents {
),
}
}
#[test]
fn add_new_document() {
let (queue, rx) = setup_document();
let name = format!("name-{}", Uuid::new_v4());
let content = format!("content-{}", Uuid::new_v4());
let mut msg1 = Message::new(MsgType::DocumentRequest);
msg1.add_data("name", name.clone());
msg1.add_data("action", ActionType::Add);
msg1.add_data("doc", content.clone());
queue.send(msg1.clone()).unwrap();
let reply1 = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply1.get_id(), msg1.get_id());
match reply1.get_msg_type() {
MsgType::ActionOk => {}
_ => unreachable!(
"got {:?}: should have received action ok",
reply1.get_msg_type()
),
}
let mut msg2 = Message::new(MsgType::DocumentRequest);
msg2.add_data("name", name.clone());
msg2.add_data("action", ActionType::Get);
queue.send(msg2.clone()).unwrap();
let reply2 = rx.recv_timeout(TIMEOUT).unwrap();
assert_eq!(reply2.get_id(), msg2.get_id());
match reply2.get_msg_type() {
MsgType::Document => {}
_ => unreachable!(
"got {:?}: should have received document",
reply2.get_msg_type()
),
}
assert_eq!(reply2.get_data("doc").unwrap().to_string(), content);
}
}