From 6fa8950cf867a8e1246c2f2c90a4047ae5175f6f Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Wed, 30 Oct 2019 14:36:53 -0400 Subject: Replace Json handler with handler for status codes --- src/messages/format_msg.rs | 37 +++++++++++++++++++++++++++++++++++++ src/messages/mod.rs | 2 ++ 2 files changed, 39 insertions(+) create mode 100644 src/messages/format_msg.rs (limited to 'src/messages') diff --git a/src/messages/format_msg.rs b/src/messages/format_msg.rs new file mode 100644 index 0000000..34e4981 --- /dev/null +++ b/src/messages/format_msg.rs @@ -0,0 +1,37 @@ +use actix_web::{http::StatusCode, Error, HttpRequest, HttpResponse, Responder}; +use serde::Serialize; + +pub struct FormatMsg { + pub message: T, + pub code: StatusCode, +} + +impl FormatMsg { + /// Deconstruct to an inner value + pub fn into_inner(self) -> T { + self.message + } + + pub fn ok(message: T) -> Self { + FormatMsg { + message: message, + code: StatusCode::OK, + } + } +} + +impl Responder for FormatMsg { + type Error = Error; + type Future = Result; + + fn respond_to(self, _: &HttpRequest) -> Self::Future { + let body = match serde_json::to_string(&self.message) { + Ok(body) => body, + Err(e) => return Err(e.into()), + }; + + Ok(HttpResponse::build(self.code) + .content_type("application/json") + .body(body)) + } +} \ No newline at end of file diff --git a/src/messages/mod.rs b/src/messages/mod.rs index ef326a0..18f2bb7 100644 --- a/src/messages/mod.rs +++ b/src/messages/mod.rs @@ -1,8 +1,10 @@ use std::marker::Sized; +pub mod format_msg; pub mod callback; pub mod not_understood; +pub use self::format_msg::FormatMsg; pub use self::callback::Callback; pub use self::not_understood::NotUnderstood; -- cgit