diff --git a/src/cli/command.rs b/src/cli/command.rs index b7e2f52f..e31bbeb8 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -7,30 +7,10 @@ use serde_json::Value; use crate::api::schema::*; use crate::api::router::*; //use crate::api::config::*; +use super::environment::CliEnvironment; + use crate::getopts; -struct CliEnvironment { - result_attributes: HashMap, -} - -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() { eprintln!("Usage: TODO"); diff --git a/src/cli/environment.rs b/src/cli/environment.rs new file mode 100644 index 00000000..3ff85508 --- /dev/null +++ b/src/cli/environment.rs @@ -0,0 +1,25 @@ +use crate::api::router::*; + +use std::collections::HashMap; +use serde_json::Value; + +pub struct CliEnvironment { + result_attributes: HashMap, +} + +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) + } +} diff --git a/src/lib.rs b/src/lib.rs index 1a7ae89d..6d506aed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ pub mod api { #[macro_use] pub mod server { + pub mod environment; pub mod formatter; #[macro_use] pub mod rest; @@ -52,6 +53,7 @@ pub mod getopts; pub mod cli { + pub mod environment; pub mod command; } diff --git a/src/server/environment.rs b/src/server/environment.rs new file mode 100644 index 00000000..18346be4 --- /dev/null +++ b/src/server/environment.rs @@ -0,0 +1,25 @@ +use crate::api::router::*; + +use std::collections::HashMap; +use serde_json::Value; + +pub struct RestEnvironment { + result_attributes: HashMap, +} + +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) + } +} diff --git a/src/server/formatter.rs b/src/server/formatter.rs index 526c8e07..8314d7a8 100644 --- a/src/server/formatter.rs +++ b/src/server/formatter.rs @@ -13,7 +13,7 @@ pub struct OutputFormatter { pub format_error: fn(err: Error) -> Response, } -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 { @@ -25,7 +25,7 @@ fn json_response(result: Value) -> Response { 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 } @@ -52,7 +52,7 @@ fn json_format_error(err: Error) -> Response { let mut response = Response::new(Body::from(err.to_string())); response.headers_mut().insert( header::CONTENT_TYPE, - header::HeaderValue::from_static(json_content_type)); + header::HeaderValue::from_static(JSON_CONTENT_TYPE)); *response.status_mut() = StatusCode::BAD_REQUEST; response diff --git a/src/server/rest.rs b/src/server/rest.rs index 3812ffb6..180455d3 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -2,6 +2,7 @@ use crate::tools; use crate::api::schema::*; use crate::api::router::*; use crate::api::config::*; +use super::environment::RestEnvironment; use super::formatter::*; use std::fmt; @@ -26,27 +27,6 @@ use hyper::service::{Service, NewService}; use hyper::rt::{Future, Stream}; use hyper::header; -struct RestEnvironment { - result_attributes: HashMap, -} - -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 api_config: Arc, }