move rpc environment implementation to separate files

This commit is contained in:
Dietmar Maurer 2019-01-26 15:08:02 +01:00
parent 6049b71f41
commit 0f253593c6
6 changed files with 58 additions and 46 deletions

View File

@ -7,30 +7,10 @@ use serde_json::Value;
use crate::api::schema::*; use crate::api::schema::*;
use crate::api::router::*; use crate::api::router::*;
//use crate::api::config::*; //use crate::api::config::*;
use super::environment::CliEnvironment;
use crate::getopts; use crate::getopts;
struct CliEnvironment {
result_attributes: HashMap<String, Value>,
}
impl CliEnvironment {
fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for CliEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}
pub fn print_cli_usage() { pub fn print_cli_usage() {
eprintln!("Usage: TODO"); eprintln!("Usage: TODO");

25
src/cli/environment.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::api::router::*;
use std::collections::HashMap;
use serde_json::Value;
pub struct CliEnvironment {
result_attributes: HashMap<String, Value>,
}
impl CliEnvironment {
pub fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for CliEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}

View File

@ -25,6 +25,7 @@ pub mod api {
#[macro_use] #[macro_use]
pub mod server { pub mod server {
pub mod environment;
pub mod formatter; pub mod formatter;
#[macro_use] #[macro_use]
pub mod rest; pub mod rest;
@ -52,6 +53,7 @@ pub mod getopts;
pub mod cli { pub mod cli {
pub mod environment;
pub mod command; pub mod command;
} }

25
src/server/environment.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::api::router::*;
use std::collections::HashMap;
use serde_json::Value;
pub struct RestEnvironment {
result_attributes: HashMap<String, Value>,
}
impl RestEnvironment {
pub fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for RestEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}

View File

@ -13,7 +13,7 @@ pub struct OutputFormatter {
pub format_error: fn(err: Error) -> Response<Body>, pub format_error: fn(err: Error) -> Response<Body>,
} }
static json_content_type: &str = "application/json;charset=UTF-8"; static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
fn json_response(result: Value) -> Response<Body> { fn json_response(result: Value) -> Response<Body> {
@ -25,7 +25,7 @@ fn json_response(result: Value) -> Response<Body> {
let mut response = Response::new(raw.into()); let mut response = Response::new(raw.into());
response.headers_mut().insert( response.headers_mut().insert(
header::CONTENT_TYPE, header::CONTENT_TYPE,
header::HeaderValue::from_static(json_content_type)); header::HeaderValue::from_static(JSON_CONTENT_TYPE));
response response
} }
@ -52,7 +52,7 @@ fn json_format_error(err: Error) -> Response<Body> {
let mut response = Response::new(Body::from(err.to_string())); let mut response = Response::new(Body::from(err.to_string()));
response.headers_mut().insert( response.headers_mut().insert(
header::CONTENT_TYPE, header::CONTENT_TYPE,
header::HeaderValue::from_static(json_content_type)); header::HeaderValue::from_static(JSON_CONTENT_TYPE));
*response.status_mut() = StatusCode::BAD_REQUEST; *response.status_mut() = StatusCode::BAD_REQUEST;
response response

View File

@ -2,6 +2,7 @@ use crate::tools;
use crate::api::schema::*; use crate::api::schema::*;
use crate::api::router::*; use crate::api::router::*;
use crate::api::config::*; use crate::api::config::*;
use super::environment::RestEnvironment;
use super::formatter::*; use super::formatter::*;
use std::fmt; use std::fmt;
@ -26,27 +27,6 @@ use hyper::service::{Service, NewService};
use hyper::rt::{Future, Stream}; use hyper::rt::{Future, Stream};
use hyper::header; use hyper::header;
struct RestEnvironment {
result_attributes: HashMap<String, Value>,
}
impl RestEnvironment {
fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for RestEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}
pub struct RestServer { pub struct RestServer {
pub api_config: Arc<ApiConfig>, pub api_config: Arc<ApiConfig>,
} }