blob: 34e4981e10ac7d22f67cb80fe66d711ae71c874f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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))
}
}
|