2023-07-19 14:35:56 +00:00
|
|
|
use axum::response::{IntoResponse, Response};
|
2022-02-05 23:06:16 +00:00
|
|
|
use http::header::{HeaderValue, CONTENT_TYPE};
|
|
|
|
use http::StatusCode;
|
|
|
|
use serde::Serialize;
|
2023-05-05 18:54:49 +00:00
|
|
|
use serde_json::Value as Json;
|
|
|
|
use surrealdb::sql;
|
2022-02-05 23:06:16 +00:00
|
|
|
|
2023-07-19 14:35:56 +00:00
|
|
|
use super::headers::Accept;
|
|
|
|
|
2022-02-05 23:06:16 +00:00
|
|
|
pub enum Output {
|
|
|
|
None,
|
2022-07-04 00:00:39 +00:00
|
|
|
Fail,
|
2022-09-25 12:05:56 +00:00
|
|
|
Text(String),
|
2022-02-05 23:06:16 +00:00
|
|
|
Json(Vec<u8>), // JSON
|
|
|
|
Cbor(Vec<u8>), // CBOR
|
|
|
|
Pack(Vec<u8>), // MessagePack
|
2023-03-31 22:36:07 +00:00
|
|
|
Full(Vec<u8>), // Full type serialization
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 00:00:39 +00:00
|
|
|
pub fn none() -> Output {
|
|
|
|
Output::None
|
|
|
|
}
|
|
|
|
|
2022-09-25 12:05:56 +00:00
|
|
|
pub fn text(val: String) -> Output {
|
|
|
|
Output::Text(val)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 22:36:07 +00:00
|
|
|
pub fn full<T>(val: &T) -> Output
|
2023-03-27 12:52:28 +00:00
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
2023-07-01 19:09:15 +00:00
|
|
|
match surrealdb::sql::serde::serialize(val) {
|
2023-03-31 22:36:07 +00:00
|
|
|
Ok(v) => Output::Full(v),
|
2023-03-27 12:52:28 +00:00
|
|
|
Err(_) => Output::Fail,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 18:54:49 +00:00
|
|
|
/// Convert and simplify the value into JSON
|
|
|
|
pub fn simplify<T: Serialize>(v: T) -> Json {
|
|
|
|
sql::to_value(v).unwrap().into()
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:35:56 +00:00
|
|
|
impl IntoResponse for Output {
|
|
|
|
fn into_response(self) -> Response {
|
2022-02-05 23:06:16 +00:00
|
|
|
match self {
|
2022-09-25 12:05:56 +00:00
|
|
|
Output::Text(v) => {
|
2023-07-19 14:35:56 +00:00
|
|
|
([(CONTENT_TYPE, HeaderValue::from(Accept::TextPlain))], v).into_response()
|
2022-09-25 12:05:56 +00:00
|
|
|
}
|
2022-02-05 23:06:16 +00:00
|
|
|
Output::Json(v) => {
|
2023-07-19 14:35:56 +00:00
|
|
|
([(CONTENT_TYPE, HeaderValue::from(Accept::ApplicationJson))], v).into_response()
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
Output::Cbor(v) => {
|
2023-07-19 14:35:56 +00:00
|
|
|
([(CONTENT_TYPE, HeaderValue::from(Accept::ApplicationCbor))], v).into_response()
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
|
|
|
Output::Pack(v) => {
|
2023-07-19 14:35:56 +00:00
|
|
|
([(CONTENT_TYPE, HeaderValue::from(Accept::ApplicationPack))], v).into_response()
|
2023-03-27 12:52:28 +00:00
|
|
|
}
|
2023-03-31 22:36:07 +00:00
|
|
|
Output::Full(v) => {
|
2023-07-19 14:35:56 +00:00
|
|
|
([(CONTENT_TYPE, HeaderValue::from(Accept::Surrealdb))], v).into_response()
|
2022-02-05 23:06:16 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|