aboutsummaryrefslogtreecommitdiff
path: root/src/messages
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@submelon.tech>2019-10-30 09:40:39 -0400
committerKevin J Hoerr <kjhoerr@submelon.tech>2019-10-30 09:40:39 -0400
commitb3a313f8e5e0185104727e75747251121eddf92d (patch)
tree7619d8d14d99767803658e62c21792136d7a1ada /src/messages
parent067e9784b54e4645412e5e7d8d7d2bc70bdcf646 (diff)
downloadaugust-offensive-b3a313f8e5e0185104727e75747251121eddf92d.tar.gz
august-offensive-b3a313f8e5e0185104727e75747251121eddf92d.tar.bz2
august-offensive-b3a313f8e5e0185104727e75747251121eddf92d.zip
Refactor messages module to be separate from routes and easily referencable
Diffstat (limited to 'src/messages')
-rw-r--r--src/messages/callback.rs67
-rw-r--r--src/messages/mod.rs26
-rw-r--r--src/messages/not_understood.rs48
3 files changed, 141 insertions, 0 deletions
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<String>,
+ pub request: String,
+ pub content: HashMap<String, String>,
+}
+
+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<T> {
+ pub result_type: String,
+ pub content: T,
+}
+
+pub trait Message {
+ fn name(&self) -> String;
+ fn as_outgoing(self) -> OutgoingMsg<Self>
+ 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<String>,
+}
+
+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![] });
+ }
+}