aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2019-10-29 00:21:26 -0400
committerKevin J Hoerr <kjhoerr@protonmail.com>2019-10-29 00:21:26 -0400
commit067e9784b54e4645412e5e7d8d7d2bc70bdcf646 (patch)
tree08dfd1a323a88f5db41eb25b568fc9b9b88b0b03 /src
parent7fa4d1973e6f82551d842919e54629b9a7ab08f0 (diff)
downloadaugust-offensive-067e9784b54e4645412e5e7d8d7d2bc70bdcf646.tar.gz
august-offensive-067e9784b54e4645412e5e7d8d7d2bc70bdcf646.tar.bz2
august-offensive-067e9784b54e4645412e5e7d8d7d2bc70bdcf646.zip
Split messages into separate module; add unit tests for each message type
Diffstat (limited to 'src')
-rw-r--r--src/routes/messages.rs44
-rw-r--r--src/routes/messages/callback.rs67
-rw-r--r--src/routes/messages/mod.rs23
-rw-r--r--src/routes/messages/not_understood.rs48
-rw-r--r--src/routes/mod.rs2
5 files changed, 139 insertions, 45 deletions
diff --git a/src/routes/messages.rs b/src/routes/messages.rs
deleted file mode 100644
index 7b7a70b..0000000
--- a/src/routes/messages.rs
+++ /dev/null
@@ -1,44 +0,0 @@
-use std::{collections::HashMap, marker::Sized};
-
-#[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,
- }
- }
-}
-
-#[derive(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")
- }
-}
-
-#[derive(Serialize)]
-pub struct NotUnderstood {
- pub path: Vec<String>,
-}
-
-impl Message for NotUnderstood {
- fn name(&self) -> String {
- String::from("NOT_UNDERSTOOD")
- }
-}
diff --git a/src/routes/messages/callback.rs b/src/routes/messages/callback.rs
new file mode 100644
index 0000000..d98ac15
--- /dev/null
+++ b/src/routes/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/routes/messages/mod.rs b/src/routes/messages/mod.rs
new file mode 100644
index 0000000..26ae1bb
--- /dev/null
+++ b/src/routes/messages/mod.rs
@@ -0,0 +1,23 @@
+use std::marker::Sized;
+
+pub mod callback;
+pub mod not_understood;
+
+#[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/routes/messages/not_understood.rs b/src/routes/messages/not_understood.rs
new file mode 100644
index 0000000..6c2c3ae
--- /dev/null
+++ b/src/routes/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![] });
+ }
+}
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index e0b17cf..4a8ed5a 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -2,7 +2,7 @@ use actix_web::{web::Json, web::Query, HttpRequest, Result};
use std::collections::HashMap;
pub mod messages;
-use messages::*;
+use messages::{callback::*, not_understood::*, *};
type JsonMessage<U> = Result<Json<OutgoingMsg<U>>>;