From b3a313f8e5e0185104727e75747251121eddf92d Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Wed, 30 Oct 2019 09:40:39 -0400 Subject: Refactor messages module to be separate from routes and easily referencable --- src/messages/callback.rs | 67 ++++++++++++++++++++++++++++++++++++++++++ src/messages/mod.rs | 26 ++++++++++++++++ src/messages/not_understood.rs | 48 ++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 src/messages/callback.rs create mode 100644 src/messages/mod.rs create mode 100644 src/messages/not_understood.rs (limited to 'src/messages') diff --git a/src/messages/callback.rs b/src/messages/callback.rs new file mode 100644 index 0000000..d98ac15 --- /dev/null +++ b/src/messages/callback.rs @@ -0,0 +1,67 @@ +use messages::Message; +use std::collections::HashMap; + +#[derive(Debug, Serialize)] +pub struct Callback { + pub path: Vec, + pub request: String, + pub content: HashMap, +} + +impl Message for Callback { + fn name(&self) -> String { + String::from("CALLBACK") + } +} + +impl PartialEq for Callback { + fn eq(&self, other: &Self) -> bool { + self.request == other.request && self.path == other.path && self.content == other.content + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_name() { + // Arrange + let query = HashMap::new(); + let message = Callback { + path: vec![], + request: String::from("GET"), + content: query, + }; + + // Act + let name = message.name(); + + // Assert + assert_eq!(name, "CALLBACK"); + } + + #[test] + fn test_asoutgoing() { + // Arrange + let message = Callback { + path: vec![], + request: String::from("GET"), + content: HashMap::new(), + }; + + // Act + let outgoing = message.as_outgoing(); + + // Assert + assert_eq!(outgoing.result_type, "CALLBACK"); + assert_eq!( + outgoing.content, + Callback { + path: vec![], + request: String::from("GET"), + content: HashMap::new(), + } + ); + } +} diff --git a/src/messages/mod.rs b/src/messages/mod.rs new file mode 100644 index 0000000..ca74078 --- /dev/null +++ b/src/messages/mod.rs @@ -0,0 +1,26 @@ +use std::marker::Sized; + +pub mod callback; +pub mod not_understood; + +pub use self::callback::Callback; +pub use self::not_understood::NotUnderstood; + +#[derive(Serialize)] +pub struct OutgoingMsg { + pub result_type: String, + pub content: T, +} + +pub trait Message { + fn name(&self) -> String; + fn as_outgoing(self) -> OutgoingMsg + where + Self: Sized, + { + OutgoingMsg { + result_type: self.name(), + content: self, + } + } +} diff --git a/src/messages/not_understood.rs b/src/messages/not_understood.rs new file mode 100644 index 0000000..6c2c3ae --- /dev/null +++ b/src/messages/not_understood.rs @@ -0,0 +1,48 @@ +use messages::Message; + +#[derive(Debug, Serialize)] +pub struct NotUnderstood { + pub path: Vec, +} + +impl Message for NotUnderstood { + fn name(&self) -> String { + String::from("NOT_UNDERSTOOD") + } +} + +impl PartialEq for NotUnderstood { + fn eq(&self, other: &Self) -> bool { + self.path == other.path + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_name() { + // Arrange + let message = NotUnderstood { path: vec![] }; + + // Act + let name = message.name(); + + // Assert + assert_eq!(name, "NOT_UNDERSTOOD"); + } + + #[test] + fn test_asoutgoing() { + // Arrange + let message = NotUnderstood { path: vec![] }; + + // Act + let outgoing = message.as_outgoing(); + + // Assert + assert_eq!(outgoing.result_type, "NOT_UNDERSTOOD"); + assert_eq!(outgoing.content, NotUnderstood { path: vec![] }); + } +} -- cgit