aboutsummaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/messages.rs33
-rw-r--r--src/routes/mod.rs27
2 files changed, 49 insertions, 11 deletions
diff --git a/src/routes/messages.rs b/src/routes/messages.rs
index c4f2b5e..7b7a70b 100644
--- a/src/routes/messages.rs
+++ b/src/routes/messages.rs
@@ -1,15 +1,29 @@
-use std::collections::HashMap;
+use std::{collections::HashMap, marker::Sized};
-trait Content {}
+#[derive(Serialize)]
+pub struct OutgoingMsg<T> {
+ pub result_type: String,
+ pub content: T,
+}
-trait Message {
+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: HashMap<String, String>,
+ pub request: String,
+ pub content: HashMap<String, String>,
}
impl Message for Callback {
@@ -17,3 +31,14 @@ impl Message for Callback {
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/mod.rs b/src/routes/mod.rs
index 017413b..16fcce9 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -1,24 +1,37 @@
-use actix_web::{HttpRequest, Json, Result, Responder};
+use actix_web::{HttpRequest, Json, Result};
use std::ops::Deref;
pub mod messages;
use messages::*;
-pub fn not_understood(_req: &HttpRequest) -> impl Responder {
- "Hello, world!"
+type JsonMessage<U> = Result<Json<OutgoingMsg<U>>>;
+
+// Sends a default response message when requested an undefined resource.
+pub fn not_understood(req: &HttpRequest) -> JsonMessage<NotUnderstood> {
+ let message = NotUnderstood {
+ path: destruct_path(req.path()),
+ };
+
+ Ok(Json(message.as_outgoing()))
}
-pub fn callback(req: &HttpRequest) -> Result<Json<Callback>> {
+// Sends Callback message with information from HttpRequest.
+pub fn callback(req: &HttpRequest) -> JsonMessage<Callback> {
let path = req.path();
+ let method = req.method().as_str();
let query_ref = req.query();
let request = query_ref.deref().clone();
- Ok(Json(Callback {
+ let callback = Callback {
path: destruct_path(path),
- request: request,
- }))
+ request: String::from(method),
+ content: request,
+ };
+
+ Ok(Json(callback.as_outgoing()))
}
+// Takes an HttpRequest path and splits it into an array.
fn destruct_path(path: &str) -> Vec<String> {
path.split_terminator("/")
// first element is always blank due to link starting with "/api"