diff options
| author | Kevin J Hoerr <kjhoerr@submelon.tech> | 2019-10-30 14:36:53 -0400 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@submelon.tech> | 2019-10-30 14:36:53 -0400 |
| commit | 6fa8950cf867a8e1246c2f2c90a4047ae5175f6f (patch) | |
| tree | b5a538ceff7d66c4f98417394db3eecc242b5ffb /src/messages/format_msg.rs | |
| parent | 82c3cc2c1952020ef13c330047cbc8ba1dc7cc75 (diff) | |
| download | august-offensive-6fa8950cf867a8e1246c2f2c90a4047ae5175f6f.tar.gz august-offensive-6fa8950cf867a8e1246c2f2c90a4047ae5175f6f.tar.bz2 august-offensive-6fa8950cf867a8e1246c2f2c90a4047ae5175f6f.zip | |
Replace Json handler with handler for status codes
Diffstat (limited to 'src/messages/format_msg.rs')
| -rw-r--r-- | src/messages/format_msg.rs | 37 |
1 files changed, 37 insertions, 0 deletions
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<T> { + pub message: T, + pub code: StatusCode, +} + +impl<T> FormatMsg<T> { + /// 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<T: Serialize> Responder for FormatMsg<T> { + type Error = Error; + type Future = Result<HttpResponse, Error>; + + 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 |
