diff --git a/src/action/message.rs b/src/action/message.rs index 4dfa0aa..f6c8574 100644 --- a/src/action/message.rs +++ b/src/action/message.rs @@ -40,6 +40,7 @@ impl MessageAction for MsgAction { Self::Query(data) => data.doc_name(), Self::Records(data) => data.doc_name(), Self::Register(data) => data.doc_name(), + Self::Reply(data) => data.doc_name(), _ => &NameType::None, } } @@ -159,7 +160,7 @@ mod msgactions { #[test] fn turn_reply_into_action() { - let value = Reply::new(); + let value = Reply::new(Name::english("something")); let result: MsgAction = value.into(); match result { MsgAction::Reply(_) => {} diff --git a/src/action/reply.rs b/src/action/reply.rs index 30a6d58..5505f52 100644 --- a/src/action/reply.rs +++ b/src/action/reply.rs @@ -1,13 +1,21 @@ use super::{CalcValue, RequestData}; +use crate::{message::MessageAction, name::NameType}; #[derive(Clone, Debug)] pub struct Reply { + doc_name: NameType, data: Vec, } impl Reply { - pub fn new() -> Self { - Self { data: Vec::new() } + pub fn new(name: NT) -> Self + where + NT: Into, + { + Self { + doc_name: name.into(), + data: Vec::new(), + } } fn add(&mut self, doc: RequestData) { @@ -23,20 +31,35 @@ impl Reply { } } +impl MessageAction for Reply { + fn doc_name(&self) -> &NameType { + &self.doc_name + } +} + #[cfg(test)] mod replies { use super::*; use crate::{action::Field, name::Name}; + use uuid::Uuid; + + #[test] + fn does_default_reply_return_document() { + let name = Name::english(Uuid::new_v4().to_string().as_str()); + let expected: NameType = name.clone().into(); + let reply = Reply::new(name.clone()); + assert_eq!(reply.doc_name(), &expected); + } #[test] fn is_new_empty() { - let reply = Reply::new(); + let reply = Reply::new(Name::english("something")); assert_eq!(reply.len(), 0, "should have no records"); } #[test] fn can_add_documents() { - let mut reply = Reply::new(); + let mut reply = Reply::new(Name::english("something")); let doc = RequestData::new(); reply.add(doc.clone()); assert_eq!(reply.len(), 1); @@ -51,7 +74,7 @@ mod replies { doc1.add_field(fieldname.clone(), "one"); let mut doc2 = RequestData::new(); doc2.add_field(fieldname.clone(), "two"); - let mut reply = Reply::new(); + let mut reply = Reply::new(Name::english("something")); reply.add(doc1); reply.add(doc2); let mut reply_iter = reply.iter(); diff --git a/src/document/create.rs b/src/document/create.rs index 92e61d6..cea52de 100644 --- a/src/document/create.rs +++ b/src/document/create.rs @@ -537,11 +537,17 @@ impl DocumentFile { }; route_action.insert(route_id.clone(), path_action.doc_function()); } - let mut doc = DocumentFile::new(queue.clone(), rx, docdef, route_action, name_id.clone()); + let mut doc = DocumentFile::new( + queue.clone(), + rx, + docdef.clone(), + route_action, + name_id.clone(), + ); spawn(move || { doc.listen(); }); - let reply = msg.response(Reply::new()); + let reply = msg.response(Reply::new(names[0].clone())); queue.send(reply.clone()); } @@ -555,7 +561,9 @@ impl DocumentFile { DocFuncType::Add => self.add_document(&msg), DocFuncType::Delete => self.delete(&msg), DocFuncType::Query => self.query(&msg), - DocFuncType::Show => self.queue.send(msg.response(Reply::new())), + DocFuncType::Show => self.queue.send( + msg.response(Reply::new(self.docdef.get_document_names()[0].clone())), + ), DocFuncType::Update => self.update(&msg), DocFuncType::ExistingQuery(action) => self.existing_query(&msg, action), DocFuncType::Trigger(action) => self.trigger(&msg, action), diff --git a/src/message/wrapper.rs b/src/message/wrapper.rs index b4f4743..98a3a7a 100644 --- a/src/message/wrapper.rs +++ b/src/message/wrapper.rs @@ -176,7 +176,7 @@ mod messages { fn can_make_reply_message() { let name = Name::english("testing"); let msg = Message::new(name.clone(), Query::new(name.clone())); - let responce = Reply::new(); + let responce = Reply::new(Name::english("something")); let reply = msg.response(responce); assert_eq!(reply.get_message_id(), msg.get_message_id()); match &reply.document_id { @@ -215,7 +215,7 @@ mod messages { let msg = Message::new(doc_id.clone(), Query::new(doc_id.clone())); let data = Uuid::new_v4().to_string(); let result1 = msg.response(MTTError::new(NameType::None, ErrorID::DocumentNotFound)); - let result2 = msg.response(Reply::new()); + let result2 = msg.response(Reply::new(NameType::None)); assert_eq!(result1.get_message_id(), msg.get_message_id()); assert_eq!(result2.get_message_id(), msg.get_message_id()); assert_eq!(result1.document_id, msg.document_id); @@ -266,88 +266,6 @@ impl Operation { } } -/* -#[allow(dead_code)] -#[derive(Clone, Debug)] -pub struct Reply { - data: Vec, -} - -#[allow(dead_code)] -impl Reply { - pub fn new() -> Self { - Self { data: Vec::new() } - } - - fn add(&mut self, doc: Document) { - self.data.push(doc); - } - - pub fn len(&self) -> usize { - self.data.len() - } - - fn iter(&self) -> impl Iterator { - self.data.iter() - } -} - -#[cfg(test)] -mod replies { - use super::*; - use crate::name::Name; - - #[test] - fn is_new_empty() { - let reply = Reply::new(); - assert_eq!(reply.len(), 0, "should have no records"); - } - - #[test] - fn can_add_documents() { - let mut reply = Reply::new(); - let doc = Document::new(); - reply.add(doc.clone()); - assert_eq!(reply.len(), 1); - reply.add(doc.clone()); - assert_eq!(reply.len(), 2); - } - - #[test] - fn can_retrieve_documents() { - let fieldname = Name::english("field"); - let mut doc1 = Document::new(); - doc1.add_field(fieldname.clone(), "one"); - let mut doc2 = Document::new(); - doc2.add_field(fieldname.clone(), "two"); - let mut reply = Reply::new(); - reply.add(doc1); - reply.add(doc2); - let mut reply_iter = reply.iter(); - let result1 = reply_iter.next().unwrap(); - match result1.get_field(&fieldname) { - CalcValue::Value(data) => match data { - Field::StaticString(output) => assert_eq!(output, "one"), - _ => unreachable!("got {:?}: should have been static string", result1), - }, - _ => unreachable!("got {:?}, should have been value", result1), - } - let result2 = reply_iter.next().unwrap(); - match result2.get_field(&fieldname) { - CalcValue::Value(data) => match data { - Field::StaticString(output) => assert_eq!(output, "two"), - _ => unreachable!("got {:?}: should have been static string", result2), - }, - _ => unreachable!("got {:?}, should have been value", result2), - } - match reply_iter.next() { - None => {} - Some(_) => unreachable!("should be out of data"), - } - } -} -*/ - #[derive(Clone, Debug)] pub struct Document { data: HashMap,