From 52c8213c0889f0ff16ff48c83121fb41bad56ba7 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 25 Aug 2020 18:59:34 -0700 Subject: fmt --- src/lib.rs | 95 ++++++---- src/main.rs | 38 ++-- src/parse.rs | 435 ++++++++++++++++++++++----------------------- tests/common/mod.rs | 23 ++- tests/parse_es_requests.rs | 24 +-- 5 files changed, 322 insertions(+), 293 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c5fe410..c8967e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ - +use hyper::{Body, Method, Request, Uri}; use serde::Deserialize; -use hyper::{Request, Body, Method, Uri}; use serde_json::json; pub mod parse; @@ -9,10 +8,10 @@ use parse::UrlQueryParams; #[derive(Default, Deserialize, Debug, Clone)] pub struct ProxyConfig { - pub bind_addr: Option, // 127.0.0.1:9292 - pub upstream_addr: Option, // 127.0.0.1:9200 + pub bind_addr: Option, // 127.0.0.1:9292 + pub upstream_addr: Option, // 127.0.0.1:9200 pub allow_all_indices: Option, - pub index: Vec + pub index: Vec, } #[derive(Deserialize, Debug, Clone)] @@ -21,14 +20,13 @@ pub struct IndexConfig { } impl ProxyConfig { - pub fn allow_index(&self, name: &str) -> bool { if self.allow_all_indices == Some(true) { - return true + return true; } for index in &self.index { if index.name == name { - return true + return true; } } false @@ -45,7 +43,6 @@ pub enum ProxyError { } impl ProxyError { - pub fn to_json(&self) -> serde_json::Value { json!({ "error": { @@ -57,7 +54,10 @@ impl ProxyError { } } -pub async fn filter_request(req: Request, config: &ProxyConfig) -> Result, ProxyError> { +pub async fn filter_request( + req: Request, + config: &ProxyConfig, +) -> Result, ProxyError> { let (parts, body) = req.into_parts(); // split path into at most 3 chunks @@ -67,7 +67,9 @@ pub async fn filter_request(req: Request, config: &ProxyConfig) -> Result< } let path_chunks: Vec<&str> = req_path.split("/").collect(); if path_chunks.len() > 3 { - return Err(ProxyError::NotSupported("only request paths with up to three segments allowed".to_string())) + return Err(ProxyError::NotSupported( + "only request paths with up to three segments allowed".to_string(), + )); } let params: UrlQueryParams = serde_urlencoded::from_str(parts.uri.query().unwrap_or("")) @@ -75,30 +77,28 @@ pub async fn filter_request(req: Request, config: &ProxyConfig) -> Result< // this is sort of like a router let body = match (&parts.method, path_chunks.as_slice()) { - (&Method::GET, [""]) | (&Method::HEAD, [""]) => { - Body::empty() - }, + (&Method::GET, [""]) | (&Method::HEAD, [""]) => Body::empty(), (&Method::POST, ["_search", "scroll"]) | (&Method::DELETE, ["_search", "scroll"]) => { let whole_body = hyper::body::to_bytes(body) .await .map_err(|e| ProxyError::Malformed(e.to_string()))?; filter_scroll_request(¶ms, &whole_body, config)? - }, + } (&Method::GET, [index, "_search"]) | (&Method::POST, [index, "_search"]) => { let whole_body = hyper::body::to_bytes(body) .await .map_err(|e| ProxyError::Malformed(e.to_string()))?; filter_search_request(index, ¶ms, &whole_body, config)? - }, + } (&Method::GET, [index, "_count"]) | (&Method::POST, [index, "_count"]) => { let whole_body = hyper::body::to_bytes(body) .await .map_err(|e| ProxyError::Malformed(e.to_string()))?; filter_search_request(index, ¶ms, &whole_body, config)? - }, + } (&Method::GET, [index, "_doc", key]) | (&Method::GET, [index, "_source", key]) => { filter_read_request(index, path_chunks[1], key, ¶ms, config)? - }, + } _ => Err(ProxyError::NotSupported("unknown endpoint".to_string()))?, }; @@ -110,7 +110,13 @@ pub async fn filter_request(req: Request, config: &ProxyConfig) -> Result< }; let upstream_uri = Uri::builder() .scheme("http") - .authority(config.upstream_addr.as_ref().unwrap_or(&"localhost:9200".to_string()).as_str()) + .authority( + config + .upstream_addr + .as_ref() + .unwrap_or(&"localhost:9200".to_string()) + .as_str(), + ) .path_and_query(upstream_query_and_params.as_str()) .build() .expect("constructing upstream request URI"); @@ -123,21 +129,31 @@ pub async fn filter_request(req: Request, config: &ProxyConfig) -> Result< Ok(upstream_req) } -pub fn filter_scroll_request(_params: &UrlQueryParams, body: &[u8], _config: &ProxyConfig) -> Result { +pub fn filter_scroll_request( + _params: &UrlQueryParams, + body: &[u8], + _config: &ProxyConfig, +) -> Result { if body.len() > 0 { - let parsed: parse::ScrollBody = serde_json::from_slice(body) - .map_err(|e| ProxyError::ParseError(e.to_string()))?; + let parsed: parse::ScrollBody = + serde_json::from_slice(body).map_err(|e| ProxyError::ParseError(e.to_string()))?; // check that scroll_id is not "_all" or too short match &parsed.scroll_id { parse::StringOrArray::String(single) => { if single == "_all" || single.len() < 8 { - return Err(ProxyError::NotSupported(format!("short scroll_id: {}", single))); + return Err(ProxyError::NotSupported(format!( + "short scroll_id: {}", + single + ))); } - }, + } parse::StringOrArray::Array(array) => { for single in array { if single == "_all" || single.len() < 8 { - return Err(ProxyError::NotSupported(format!("short scroll_id: {}", single))); + return Err(ProxyError::NotSupported(format!( + "short scroll_id: {}", + single + ))); } } } @@ -148,21 +164,38 @@ pub fn filter_scroll_request(_params: &UrlQueryParams, body: &[u8], _config: &Pr } } -pub fn filter_read_request(index: &str, _endpoint: &str, _key: &str, _params: &UrlQueryParams, config: &ProxyConfig) -> Result{ +pub fn filter_read_request( + index: &str, + _endpoint: &str, + _key: &str, + _params: &UrlQueryParams, + config: &ProxyConfig, +) -> Result { if !config.allow_index(index) { - return Err(ProxyError::NotAllowed(format!("index doesn't exist or isn't proxied: {}", index))); + return Err(ProxyError::NotAllowed(format!( + "index doesn't exist or isn't proxied: {}", + index + ))); } Ok(Body::empty()) } -pub fn filter_search_request(index: &str, _params: &UrlQueryParams, body: &[u8], config: &ProxyConfig) -> Result { +pub fn filter_search_request( + index: &str, + _params: &UrlQueryParams, + body: &[u8], + config: &ProxyConfig, +) -> Result { if !config.allow_index(index) { - return Err(ProxyError::NotAllowed(format!("index doesn't exist or isn't proxied: {}", index))); + return Err(ProxyError::NotAllowed(format!( + "index doesn't exist or isn't proxied: {}", + index + ))); } // XXX: more checks if body.len() > 0 { - let parsed: parse::SearchBody = serde_json::from_slice(body) - .map_err(|e| ProxyError::ParseError(e.to_string()))?; + let parsed: parse::SearchBody = + serde_json::from_slice(body).map_err(|e| ProxyError::ParseError(e.to_string()))?; Ok(Body::from(serde_json::to_string(&parsed).unwrap())) } else { Ok(Body::empty()) diff --git a/src/main.rs b/src/main.rs index 2b25f32..0250976 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,15 @@ - use hyper::service::{make_service_fn, service_fn}; use hyper::{Body, Client, Request, Response, Server, StatusCode}; -use std::net::SocketAddr; use std::env; +use std::net::SocketAddr; use toml; -use es_public_proxy::{ProxyConfig, filter_request}; +use es_public_proxy::{filter_request, ProxyConfig}; -async fn upstream_req(req: Request, config: ProxyConfig) -> Result, hyper::Error> { +async fn upstream_req( + req: Request, + config: ProxyConfig, +) -> Result, hyper::Error> { println!("hit: {}", req.uri()); let parsed = filter_request(req, &config).await; let resp = match parsed { @@ -15,13 +17,11 @@ async fn upstream_req(req: Request, config: ProxyConfig) -> Result { - Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .header("Content-Type", "application/json; charset=UTF-8") - .body(serde_json::to_string(&other.to_json()).unwrap().into()) - .unwrap() - }, + Err(other) => Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .header("Content-Type", "application/json; charset=UTF-8") + .body(serde_json::to_string(&other.to_json()).unwrap().into()) + .unwrap(), }; println!("resp!"); Ok(resp) @@ -35,7 +35,6 @@ async fn shutdown_signal() { } async fn run_server(config: ProxyConfig) { - let addr = match &config.bind_addr { None => SocketAddr::from(([127, 0, 0, 1], 9292)), Some(addr) => addr.parse().unwrap(), @@ -46,11 +45,7 @@ async fn run_server(config: ProxyConfig) { // TODO: possible to avoid cloning config on every connection? let make_svc = make_service_fn(move |_| { let inner = config.clone(); - async move { - Ok::<_, hyper::Error>(service_fn(move |req| { - upstream_req(req, inner.clone()) - })) - } + async move { Ok::<_, hyper::Error>(service_fn(move |req| upstream_req(req, inner.clone()))) } }); let serve_future = Server::bind(&addr).serve(make_svc); let graceful = serve_future.with_graceful_shutdown(shutdown_signal()); @@ -65,7 +60,6 @@ fn usage() -> String { } fn load_config() -> ProxyConfig { - let args: Vec = env::args().collect(); let args: Vec<&str> = args.iter().map(|x| x.as_str()).collect(); let mut config_path: Option = None; @@ -73,13 +67,13 @@ fn load_config() -> ProxyConfig { // first parse CLI arg match args.as_slice() { - [_] | [] => {}, + [_] | [] => {} [_, "-h"] | [_, "--help"] => { println!("{}", usage()); std::process::exit(0); - }, - [_, "--config", p] => { config_path = Some(p.to_string()) }, - [_, "--allow-all-indices"] => { allow_all_indices = true }, + } + [_, "--config", p] => config_path = Some(p.to_string()), + [_, "--allow-all-indices"] => allow_all_indices = true, _ => { eprintln!("{}", usage()); eprintln!("couldn't parse arguments"); diff --git a/src/parse.rs b/src/parse.rs index 2a4c0a5..338ec64 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,5 +1,4 @@ - -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use std::collections::HashMap; #[derive(Serialize, Deserialize, Debug)] @@ -12,85 +11,85 @@ pub struct ApiRequest { #[derive(Serialize, Deserialize, Debug, Default)] #[serde(deny_unknown_fields)] pub struct UrlQueryParams { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub allow_no_indices: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub allow_partial_search_results: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub batched_reduce_size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub ccs_minimize_roundtrips: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub docvalue_fields: Option, // array of strings, comma-separated - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub expand_wildcards: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub explain: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub from: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub ignore_throttled: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub ignore_unavailable: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub max_concurrent_shard_requests: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub pre_filter_shard_size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub preference: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub q: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub request_cache: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub rest_total_hits_as_int: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub routing: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub scroll: Option, // string is "time value" - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub search_type: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub seq_no_primary_term: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub sort: Option, // array of strings, comma-separated - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub _source: Option, // TODO: bool or string - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub _source_excludes: Option, // array of strings, comma-separated - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub _source_includes: Option, // array of strings, comma-separated - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub stats: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub stored_fields: Option, // array of strings, comma-separated - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub suggest_field: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub suggest_text: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub terminate_after: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub timeout: Option, // string is "time units" - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub track_scores: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub track_total_hits: Option, // XXX: bool or integer - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub typed_keys: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub version: Option, // additional generic params - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub human: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub pretty: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub filter_path: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub error_trace: Option, } @@ -98,49 +97,49 @@ pub struct UrlQueryParams { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct SearchBody { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub query: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub highlight: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub collapse: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub post_filter: Option, // TODO: leaf query only? - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub rescore: Option, // TODO: single or an array of rescore objects // script_fields disabled - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub aggs: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub aggregations: Option>, // https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub sort: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub slice: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub stored_fields: Option, // array of strings, or "_none_" // overlap with URL query parameters - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub docvalue_fields: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub explain: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub from: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub min_score: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub seq_no_primary_term: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub _source: Option, // XXX: bool, string, or object - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub terminate_after: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub timeout: Option, // string is "time units" } @@ -148,7 +147,7 @@ pub struct SearchBody { #[serde(deny_unknown_fields)] pub struct ScrollBody { pub scroll_id: StringOrArray, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub scroll: Option, } @@ -157,16 +156,16 @@ pub struct ScrollBody { pub struct ApiSlice { id: u32, max: u32, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] field: Option, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct ApiRescore{ - #[serde(skip_serializing_if="Option::is_none")] +pub struct ApiRescore { + #[serde(skip_serializing_if = "Option::is_none")] pub query: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub window_size: Option, } @@ -176,65 +175,65 @@ pub struct ApiRescore{ pub struct ApiQuery { // compound queries #[serde(rename = "bool")] - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] bool_query: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boosting: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] constant_score: Option, // fulltext (leaf) queries #[serde(rename = "match")] - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] match_query: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] match_phrase: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] multi_match: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] query_string: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] simple_query_string: Option, // term-level (leaf) queries - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] exists: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] match_all: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] match_none: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] ids: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] wildcard: Option>, // also works for wildcard - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] prefix: Option>, // also works for prefix query - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] range: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] term: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] // TODO: boost in terms query terms: Option>>, // other - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] nested: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] rescore_query: Option>, // fields as part of a rescore query - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] score_mode: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] query_weight: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] rescore_query_weight: Option, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct ApiHighlight{ +pub struct ApiHighlight { // TODO: fields could also be an array of strings? fields: HashMap, @@ -249,7 +248,7 @@ pub enum SortMapValue { String(String), Object { order: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] mode: Option, }, } @@ -257,7 +256,7 @@ pub enum SortMapValue { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] #[serde(untagged)] -pub enum SortElement{ +pub enum SortElement { String(String), Object(HashMap), } @@ -269,7 +268,7 @@ pub enum DocValOrString { String(String), Object { field: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] format: Option, }, } @@ -298,10 +297,10 @@ pub struct MatchQuery { pub struct MultiMatchQuery { query: String, fields: Vec, - #[serde(skip_serializing_if="Option::is_none")] - #[serde(rename="type")] + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "type")] query_type: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] tie_breaker: Option, #[serde(flatten)] options: MatchOptions, @@ -310,27 +309,27 @@ pub struct MultiMatchQuery { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct MatchOptions { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] analyzer: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] auto_generate_synonyms_phrase_query: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzziness: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] max_expansions: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] prefix_length: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzzy_transpositions: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzzy_rewrite: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] lenient: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] operator: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] minimum_should_match: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] zero_terms_query: Option, } @@ -339,57 +338,57 @@ pub struct MatchOptions { #[serde(deny_unknown_fields)] pub struct QueryStringQuery { query: String, - #[serde(skip_serializing_if="Option::is_none")] - #[serde(rename="type")] + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "type")] query_type: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] default_field: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] allow_leading_wildcard: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] analyze_wildcard: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] analyzer: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] auto_generate_synonyms_phrase_query: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] default_operator: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] enable_position_increments: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fields: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzziness: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzzy_max_expansions: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzzy_prefix_length: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzzy_transpositions: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] lenient: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] max_determinized_states: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] minimum_should_match: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] quote_analyzer: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] phrase_slop: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] quote_field_suffix: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] rewrite: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] time_zone: Option, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct SimpleBoost { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, } @@ -411,9 +410,9 @@ pub enum TermQueryOrString { #[serde(deny_unknown_fields)] pub struct TermQuery { value: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] rewrite: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, } @@ -445,21 +444,21 @@ pub enum StringOrArray { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct RangeQuery { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] gt: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] gte: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] lt: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] lte: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] format: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] relation: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] timezone: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, } @@ -473,30 +472,30 @@ pub enum QueryFieldOrString { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct QueryField{ +pub struct QueryField { query: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fuzziness: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] slop: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct BoolQuery { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] must: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] filter: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] should: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] must_not: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] minimum_should_match: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, } @@ -505,11 +504,11 @@ pub struct BoolQuery { pub struct NestedQuery { path: String, query: Box, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] score_mode: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] ignore_unmapped: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] inner_hits: Option, } @@ -525,54 +524,54 @@ pub struct BoostingQuery { #[serde(deny_unknown_fields)] pub struct ConstantScoreQuery { filter: Box, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boost: Option, } // https://www.elastic.co/guide/en/elasticsearch/reference/current/highlighting.html #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct HighlightField{ - #[serde(skip_serializing_if="Option::is_none")] +pub struct HighlightField { + #[serde(skip_serializing_if = "Option::is_none")] boundary_chars: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boundary_max_scan: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boundary_scanner: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] boundary_scanner_locale: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] encoder: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] force_source: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fragmenter: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fragment_offset: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fragment_size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] highlight_query: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] matched_fields: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] no_match_size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] number_of_fragments: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] order: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] phrase_limit: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pre_tags: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] post_tags: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] require_field_match: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] tags_schema: Option, #[serde(rename = "type")] - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] highlight_type: Option, } @@ -586,9 +585,9 @@ pub enum SimpleFieldOrString { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct ApiCollapse{ +pub struct ApiCollapse { field: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] inner_hits: Option, } @@ -603,15 +602,15 @@ pub enum InnerHitsOneOrMore { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct InnerHits { - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] from: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] sort: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] name: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] collapse: Option>, } @@ -619,77 +618,77 @@ pub struct InnerHits { #[serde(deny_unknown_fields)] pub struct ApiAggregation { // bucket type aggregations - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] nested: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] filter: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] histogram: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] terms: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] significant_terms: Option, // metrics type aggregations - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] avg: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] min: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] max: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] sum: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] value_count: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] stats: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] percentiles: Option, // nested aggregations - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] aggs: Option>>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] aggregations: Option>>, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct NestedAggregation{ +pub struct NestedAggregation { path: String, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct SimpleAggregation{ +pub struct SimpleAggregation { field: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] interval: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] missing: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] keyed: Option, } #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] -pub struct DateHistogramAggregation{ +pub struct DateHistogramAggregation { field: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] fixed_interval: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] calendar_interval: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] format: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] time_zone: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] offset: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] order: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] keyed: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] missing: Option, } @@ -697,22 +696,22 @@ pub struct DateHistogramAggregation{ #[serde(deny_unknown_fields)] pub struct TermsAggregation { field: String, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] shard_size: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] min_doc_count: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] show_term_doc_count_error: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] order: Option>, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] include: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] exclude: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] execution_hint: Option, - #[serde(skip_serializing_if="Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] missing: Option, } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 811b4b9..2d06d81 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,7 +1,6 @@ - -use hyper::{Request, Body, Method}; -use std::path::Path; +use hyper::{Body, Method, Request}; use std::io::BufRead; +use std::path::Path; pub struct ExampleParts { pub method: String, @@ -10,11 +9,19 @@ pub struct ExampleParts { } pub fn load_parts(path: &Path) -> ExampleParts { - let file = std::fs::File::open(path).unwrap(); let mut lines = std::io::BufReader::new(file).lines(); - let first_line: Vec = lines.next().unwrap().unwrap().split(" ").map(|v| v.into()).collect(); - let body: Vec = lines.map(|v| v.into()).collect::, _>>().unwrap(); + let first_line: Vec = lines + .next() + .unwrap() + .unwrap() + .split(" ") + .map(|v| v.into()) + .collect(); + let body: Vec = lines + .map(|v| v.into()) + .collect::, _>>() + .unwrap(); let body: Option = if body.len() <= 1 { None } else { @@ -32,7 +39,9 @@ pub fn load_request(path: &Path) -> Request { let parts = load_parts(path); Request::builder() .uri(parts.path_and_query) - .method(Method::from_bytes(parts.method.as_bytes()).expect("valid method in example text file")) + .method( + Method::from_bytes(parts.method.as_bytes()).expect("valid method in example text file"), + ) .body(match parts.body { Some(data) => Body::from(data), None => Body::empty(), diff --git a/tests/parse_es_requests.rs b/tests/parse_es_requests.rs index a55e461..c8081c7 100644 --- a/tests/parse_es_requests.rs +++ b/tests/parse_es_requests.rs @@ -1,8 +1,7 @@ - -use std::fs; -use std::ffi::OsStr; use es_public_proxy::parse::{ScrollBody, SearchBody}; -use es_public_proxy::{ProxyConfig, filter_request}; +use es_public_proxy::{filter_request, ProxyConfig}; +use std::ffi::OsStr; +use std::fs; mod common; @@ -15,13 +14,12 @@ fn basic_load() { #[test] fn parse_search_bodies() { - let file_paths = fs::read_dir("tests/files/search").unwrap(); for path in file_paths { let path = path.unwrap().path(); if path.extension() != Some(OsStr::new("txt")) { - continue + continue; } let parts = common::load_parts(&path); if let Some(body) = parts.body { @@ -36,13 +34,12 @@ fn parse_search_bodies() { #[test] fn parse_scroll_bodies() { - let file_paths = fs::read_dir("tests/files/scroll").unwrap(); for path in file_paths { let path = path.unwrap().path(); if path.extension() != Some(OsStr::new("txt")) { - continue + continue; } let parts = common::load_parts(&path); if let Some(body) = parts.body { @@ -55,7 +52,6 @@ fn parse_scroll_bodies() { #[test] fn filter_search_requests() { - let file_paths = fs::read_dir("tests/files/search").unwrap(); let mut config = ProxyConfig::default(); config.allow_all_indices = Some(true); @@ -64,7 +60,7 @@ fn filter_search_requests() { for path in file_paths { let path = path.unwrap().path(); if path.extension() != Some(OsStr::new("txt")) { - continue + continue; } println!(" filtering: {}", path.display()); let req = common::load_request(&path); @@ -74,7 +70,6 @@ fn filter_search_requests() { #[test] fn filter_scroll_requests() { - let file_paths = fs::read_dir("tests/files/scroll").unwrap(); let mut config = ProxyConfig::default(); config.allow_all_indices = Some(true); @@ -83,7 +78,7 @@ fn filter_scroll_requests() { for path in file_paths { let path = path.unwrap().path(); if path.extension() != Some(OsStr::new("txt")) { - continue + continue; } println!(" filtering: {}", path.display()); let req = common::load_request(&path); @@ -93,7 +88,6 @@ fn filter_scroll_requests() { #[test] fn filter_failures() { - let file_paths = fs::read_dir("tests/files/search_fail").unwrap(); let mut config = ProxyConfig::default(); config.allow_all_indices = Some(true); @@ -102,7 +96,7 @@ fn filter_failures() { for path in file_paths { let path = path.unwrap().path(); if path.extension() != Some(OsStr::new("txt")) { - continue + continue; } println!(" filtering: {}", path.display()); let req = common::load_request(&path); @@ -115,7 +109,7 @@ fn filter_failures() { for path in file_paths { let path = path.unwrap().path(); if path.extension() != Some(OsStr::new("txt")) { - continue + continue; } println!(" filtering: {}", path.display()); let req = common::load_request(&path); -- cgit v1.2.3