rest server: rust fmt

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2022-04-06 16:55:39 +02:00
parent b300e6fbc2
commit 41583796b1
13 changed files with 453 additions and 368 deletions

View File

@ -1,11 +1,11 @@
//! Helpers to format response data
use std::collections::HashMap;
use anyhow::{Error};
use anyhow::Error;
use serde_json::{json, Value};
use hyper::{Body, Response, StatusCode};
use hyper::header;
use hyper::{Body, Response, StatusCode};
use proxmox_router::{HttpError, RpcEnvironment};
use proxmox_schema::ParameterError;
@ -22,7 +22,11 @@ pub trait OutputFormatter: Send + Sync {
fn format_error(&self, err: Error) -> Response<Body>;
/// Transform a [Result] into a http response
fn format_result(&self, result: Result<Value, Error>, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
fn format_result(
&self,
result: Result<Value, Error>,
rpcenv: &dyn RpcEnvironment,
) -> Response<Body> {
match result {
Ok(data) => self.format_data(data, rpcenv),
Err(err) => self.format_error(err),
@ -33,7 +37,6 @@ pub trait OutputFormatter: Send + Sync {
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
fn json_data_response(data: Value) -> Response<Body> {
let json_str = data.to_string();
let raw = json_str.into_bytes();
@ -41,13 +44,13 @@ fn json_data_response(data: Value) -> Response<Body> {
let mut response = Response::new(raw.into());
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
header::HeaderValue::from_static(JSON_CONTENT_TYPE),
);
response
}
fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment)
{
fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment) {
let attributes = match rpcenv.result_attrib().as_object() {
Some(attr) => attr,
None => return,
@ -58,7 +61,6 @@ fn add_result_attributes(result: &mut Value, rpcenv: &dyn RpcEnvironment)
}
}
struct JsonFormatter();
/// Format data as ``application/json``
@ -73,13 +75,9 @@ struct JsonFormatter();
/// message as string.
pub static JSON_FORMATTER: &'static dyn OutputFormatter = &JsonFormatter();
impl OutputFormatter for JsonFormatter {
impl OutputFormatter for JsonFormatter {
fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
let mut result = json!({
"data": data
});
let mut result = json!({ "data": data });
add_result_attributes(&mut result, rpcenv);
@ -87,7 +85,6 @@ impl OutputFormatter for JsonFormatter {
}
fn format_error(&self, err: Error) -> Response<Body> {
let mut response = if let Some(apierr) = err.downcast_ref::<HttpError>() {
let mut resp = Response::new(Body::from(apierr.message.clone()));
*resp.status_mut() = apierr.code;
@ -100,9 +97,12 @@ impl OutputFormatter for JsonFormatter {
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
header::HeaderValue::from_static(JSON_CONTENT_TYPE),
);
response.extensions_mut().insert(ErrorMessageExtension(err.to_string()));
response
.extensions_mut()
.insert(ErrorMessageExtension(err.to_string()));
response
}
@ -128,10 +128,8 @@ pub static EXTJS_FORMATTER: &'static dyn OutputFormatter = &ExtJsFormatter();
struct ExtJsFormatter();
impl OutputFormatter for ExtJsFormatter {
impl OutputFormatter for ExtJsFormatter {
fn format_data(&self, data: Value, rpcenv: &dyn RpcEnvironment) -> Response<Body> {
let mut result = json!({
"data": data,
"success": true
@ -143,7 +141,6 @@ impl OutputFormatter for ExtJsFormatter {
}
fn format_error(&self, err: Error) -> Response<Body> {
let message: String;
let mut errors = HashMap::new();
@ -165,7 +162,9 @@ impl OutputFormatter for ExtJsFormatter {
let mut response = json_data_response(result);
response.extensions_mut().insert(ErrorMessageExtension(message));
response
.extensions_mut()
.insert(ErrorMessageExtension(message));
response
}