2022-02-05 23:06:16 +00:00
|
|
|
use http::header::{HeaderValue, CONTENT_TYPE};
|
|
|
|
use http::StatusCode;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
pub enum Output {
|
|
|
|
None,
|
2022-07-04 00:00:39 +00:00
|
|
|
Fail,
|
2022-02-05 23:06:16 +00:00
|
|
|
Json(Vec<u8>), // JSON
|
|
|
|
Cbor(Vec<u8>), // CBOR
|
|
|
|
Pack(Vec<u8>), // MessagePack
|
|
|
|
}
|
|
|
|
|
2022-07-04 00:00:39 +00:00
|
|
|
pub fn none() -> Output {
|
|
|
|
Output::None
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:06:16 +00:00
|
|
|
pub fn json<T>(val: &T) -> Output
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
match serde_json::to_vec(val) {
|
|
|
|
Ok(v) => Output::Json(v),
|
2022-07-04 00:00:39 +00:00
|
|
|
Err(_) => Output::Fail,
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cbor<T>(val: &T) -> Output
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
match serde_cbor::to_vec(val) {
|
|
|
|
Ok(v) => Output::Cbor(v),
|
2022-07-04 00:00:39 +00:00
|
|
|
Err(_) => Output::Fail,
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pack<T>(val: &T) -> Output
|
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
match serde_pack::to_vec(val) {
|
|
|
|
Ok(v) => Output::Pack(v),
|
2022-07-04 00:00:39 +00:00
|
|
|
Err(_) => Output::Fail,
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl warp::Reply for Output {
|
|
|
|
fn into_response(self) -> warp::reply::Response {
|
|
|
|
match self {
|
|
|
|
Output::Json(v) => {
|
|
|
|
let mut res = warp::reply::Response::new(v.into());
|
|
|
|
let con = HeaderValue::from_static("application/json");
|
|
|
|
res.headers_mut().insert(CONTENT_TYPE, con);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
Output::Cbor(v) => {
|
|
|
|
let mut res = warp::reply::Response::new(v.into());
|
|
|
|
let con = HeaderValue::from_static("application/cbor");
|
|
|
|
res.headers_mut().insert(CONTENT_TYPE, con);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
Output::Pack(v) => {
|
|
|
|
let mut res = warp::reply::Response::new(v.into());
|
|
|
|
let con = HeaderValue::from_static("application/msgpack");
|
|
|
|
res.headers_mut().insert(CONTENT_TYPE, con);
|
|
|
|
res
|
|
|
|
}
|
2022-07-04 00:00:39 +00:00
|
|
|
Output::None => StatusCode::OK.into_response(),
|
|
|
|
Output::Fail => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|