meta_srv/service/admin/
util.rs1use std::fmt::Debug;
16
17use serde::Serialize;
18use snafu::ResultExt;
19use tonic::codegen::http;
20
21use crate::error::{self, Result};
22
23pub fn to_text_response(text: &str) -> Result<http::Response<String>> {
25 http::Response::builder()
26 .header("Content-Type", "text/plain")
27 .status(http::StatusCode::OK)
28 .body(text.to_string())
29 .context(error::InvalidHttpBodySnafu)
30}
31
32pub fn to_json_response<T>(response: T) -> Result<http::Response<String>>
34where
35 T: Serialize + Debug,
36{
37 let response = serde_json::to_string(&response).context(error::SerializeToJsonSnafu {
38 input: format!("{response:?}"),
39 })?;
40 http::Response::builder()
41 .header("Content-Type", "application/json")
42 .status(http::StatusCode::OK)
43 .body(response)
44 .context(error::InvalidHttpBodySnafu)
45}
46
47pub fn to_not_found_response() -> Result<http::Response<String>> {
49 http::Response::builder()
50 .status(http::StatusCode::NOT_FOUND)
51 .body("".to_string())
52 .context(error::InvalidHttpBodySnafu)
53}