forked from rojo-rbx/rojo
* Upgrade dependencies, oneshot channel ref * New service style? * Fix warning * A server is running again * Working server with async blocks * UI working again * Finish upgrade * Bump MSRV to 1.46.0 for if/match in const fn * Update the README as part of this
26 lines
755 B
Rust
26 lines
755 B
Rust
use hyper::{header::CONTENT_TYPE, Body, Response, StatusCode};
|
|
use serde::Serialize;
|
|
|
|
pub fn json_ok<T: Serialize>(value: T) -> Response<Body> {
|
|
json(value, StatusCode::OK)
|
|
}
|
|
|
|
pub fn json<T: Serialize>(value: T, code: StatusCode) -> Response<Body> {
|
|
let serialized = match serde_json::to_string(&value) {
|
|
Ok(v) => v,
|
|
Err(err) => {
|
|
return Response::builder()
|
|
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
.header(CONTENT_TYPE, "text/plain")
|
|
.body(Body::from(err.to_string()))
|
|
.unwrap();
|
|
}
|
|
};
|
|
|
|
Response::builder()
|
|
.status(code)
|
|
.header(CONTENT_TYPE, "application/json")
|
|
.body(Body::from(serialized))
|
|
.unwrap()
|
|
}
|