From ee47df621371096dd76953de1933f7164408716a Mon Sep 17 00:00:00 2001 From: Fabrice Reix Date: Sun, 28 Mar 2021 09:51:21 +0200 Subject: [PATCH] Fix new clippy warnings (Rust 1.51) - upper_case_acronyms - redundant_slicing - vec_init_then_push --- packages/hurl/src/cli/fs.rs | 14 ++--- packages/hurl/src/cli/mod.rs | 2 +- packages/hurl/src/cli/variables.rs | 14 ++--- packages/hurl/src/http/client.rs | 4 +- packages/hurl/src/main.rs | 64 +++++++++---------- packages/hurl/src/runner/content_decoding.rs | 6 +- packages/hurl/src/runner/core.rs | 4 +- packages/hurl/src/runner/entry.rs | 4 +- packages/hurl/src/runner/error.rs | 8 +-- packages/hurl/src/runner/query.rs | 2 +- packages/hurl/src/runner/xpath.rs | 8 +-- packages/hurl/tests/libcurl.rs | 2 +- packages/hurl_core/src/parser/template.rs | 2 +- packages/hurlfmt/src/cli/fs.rs | 14 ++--- packages/hurlfmt/src/cli/mod.rs | 2 +- packages/hurlfmt/src/format/json.rs | 65 ++++++++++---------- packages/hurlfmt/src/format/token.rs | 33 +++------- 17 files changed, 119 insertions(+), 129 deletions(-) diff --git a/packages/hurl/src/cli/fs.rs b/packages/hurl/src/cli/fs.rs index c668562fd..d4741f5e7 100644 --- a/packages/hurl/src/cli/fs.rs +++ b/packages/hurl/src/cli/fs.rs @@ -16,7 +16,7 @@ * */ -use crate::cli::CLIError; +use crate::cli::CliError; use std::fs; use std::fs::File; use std::io::prelude::*; @@ -30,11 +30,11 @@ fn strip_bom(bytes: &mut Vec) { /// Similar to the standard read_to_string() /// But remove any existing BOM -pub fn read_to_string(filename: &str) -> Result { +pub fn read_to_string(filename: &str) -> Result { let mut f = match File::open(&filename) { Ok(f) => f, Err(e) => { - return Err(CLIError { + return Err(CliError { message: e.to_string(), }) } @@ -42,19 +42,19 @@ pub fn read_to_string(filename: &str) -> Result { let metadata = fs::metadata(&filename).expect("unable to read metadata"); let mut buffer = vec![0; metadata.len() as usize]; if let Err(e) = f.read(&mut buffer) { - return Err(CLIError { + return Err(CliError { message: e.to_string(), }); } string_from_utf8(buffer) } -pub fn string_from_utf8(buffer: Vec) -> Result { +pub fn string_from_utf8(buffer: Vec) -> Result { let mut buffer = buffer; strip_bom(&mut buffer); match String::from_utf8(buffer) { Ok(s) => Ok(s), - Err(e) => Err(CLIError { + Err(e) => Err(CliError { message: e.to_string(), }), } @@ -94,7 +94,7 @@ pub mod tests { ); assert_eq!( string_from_utf8(vec![0xef]).err().unwrap(), - CLIError { + CliError { message: "incomplete utf-8 byte sequence from index 0".to_string() } ); diff --git a/packages/hurl/src/cli/mod.rs b/packages/hurl/src/cli/mod.rs index f9377630b..53e3464b0 100644 --- a/packages/hurl/src/cli/mod.rs +++ b/packages/hurl/src/cli/mod.rs @@ -29,6 +29,6 @@ mod logger; mod variables; #[derive(Clone, Debug, PartialEq, Eq)] -pub struct CLIError { +pub struct CliError { pub message: String, } diff --git a/packages/hurl/src/cli/variables.rs b/packages/hurl/src/cli/variables.rs index 9b387b760..7f753c731 100644 --- a/packages/hurl/src/cli/variables.rs +++ b/packages/hurl/src/cli/variables.rs @@ -16,12 +16,12 @@ * */ -use crate::cli::CLIError; +use crate::cli::CliError; use crate::runner::Value; -pub fn parse(s: &str) -> Result<(String, Value), CLIError> { +pub fn parse(s: &str) -> Result<(String, Value), CliError> { match s.find('=') { - None => Err(CLIError { + None => Err(CliError { message: format!("Missing value for variable {}!", s), }), Some(index) => { @@ -32,7 +32,7 @@ pub fn parse(s: &str) -> Result<(String, Value), CLIError> { } } -fn parse_value(s: &str) -> Result { +fn parse_value(s: &str) -> Result { if s == "true" { Ok(Value::Bool(true)) } else if s == "false" { @@ -50,7 +50,7 @@ fn parse_value(s: &str) -> Result { if let Some(s) = s.strip_suffix('"') { Ok(Value::String(s.to_string())) } else { - Err(CLIError { + Err(CliError { message: "Value should end with a double quote".to_string(), }) } @@ -98,7 +98,7 @@ pub mod tests { fn test_parse_error() { assert_eq!( parse("name").err().unwrap(), - CLIError { + CliError { message: "Missing value for variable name!".to_string() } ); @@ -129,7 +129,7 @@ pub mod tests { fn test_parse_value_error() { assert_eq!( parse_value("\"123").err().unwrap(), - CLIError { + CliError { message: "Value should end with a double quote".to_string() } ) diff --git a/packages/hurl/src/http/client.rs b/packages/hurl/src/http/client.rs index 7248c97d4..126334652 100644 --- a/packages/hurl/src/http/client.rs +++ b/packages/hurl/src/http/client.rs @@ -36,7 +36,7 @@ pub enum HttpError { FailToConnect, TooManyRedirect, CouldNotParseResponse, - SSLCertificate(Option), + SslCertificate(Option), InvalidUrl, Timeout, StatuslineIsMissing, @@ -208,7 +208,7 @@ impl Client { 6 => Err(HttpError::CouldNotResolveHost), 7 => Err(HttpError::FailToConnect), 28 => Err(HttpError::Timeout), - 60 => Err(HttpError::SSLCertificate( + 60 => Err(HttpError::SslCertificate( e.extra_description().map(String::from), )), _ => Err(HttpError::Other { diff --git a/packages/hurl/src/main.rs b/packages/hurl/src/main.rs index 16bfa9073..00702b305 100644 --- a/packages/hurl/src/main.rs +++ b/packages/hurl/src/main.rs @@ -29,7 +29,7 @@ use clap::{AppSettings, ArgMatches}; use hurl::cli; use hurl::cli::interactive; -use hurl::cli::CLIError; +use hurl::cli::CliError; use hurl::html; use hurl::http; use hurl::runner; @@ -40,7 +40,7 @@ use hurl_core::parser; use std::fs::File; #[derive(Clone, Debug, PartialEq, Eq)] -pub struct CLIOptions { +pub struct CliOptions { pub verbose: bool, pub color: bool, pub fail_fast: bool, @@ -75,7 +75,7 @@ fn execute( contents: String, current_dir: &Path, file_root: Option, - cli_options: CLIOptions, + cli_options: CliOptions, log_verbose: &impl Fn(&str), log_error_message: &impl Fn(bool, &str), ) -> HurlResult { @@ -218,11 +218,11 @@ fn output_color(matches: ArgMatches) -> bool { } } -fn to_entry(matches: ArgMatches) -> Result, CLIError> { +fn to_entry(matches: ArgMatches) -> Result, CliError> { match matches.value_of("to_entry") { Some(value) => match value.parse() { Ok(v) => Ok(Some(v)), - Err(_) => Err(CLIError { + Err(_) => Err(CliError { message: "Invalid value for option --to-entry - must be a positive integer!" .to_string(), }), @@ -234,7 +234,7 @@ fn to_entry(matches: ArgMatches) -> Result, CLIError> { fn json_file( matches: ArgMatches, log_verbose: impl Fn(&str), -) -> Result<(Vec, Option), CLIError> { +) -> Result<(Vec, Option), CliError> { if let Some(filename) = matches.value_of("json") { let path = Path::new(filename); @@ -245,7 +245,7 @@ fn json_file( let v: serde_json::Value = match serde_json::from_str(data.as_str()) { Ok(val) => val, Err(_) => { - return Err(CLIError { + return Err(CliError { message: format!("The file {} is not a valid json file", path.display()), }); } @@ -253,7 +253,7 @@ fn json_file( match runner::deserialize_results(v) { Err(msg) => { - return Err(CLIError { + return Err(CliError { message: format!("Existing Hurl json can not be parsed! - {}", msg), }); } @@ -269,14 +269,14 @@ fn json_file( } } -fn html_report(matches: ArgMatches) -> Result, CLIError> { +fn html_report(matches: ArgMatches) -> Result, CliError> { if let Some(dir) = matches.value_of("html_report") { let path = Path::new(dir); if std::path::Path::new(&path).exists() { Ok(Some(path.to_path_buf())) } else { match std::fs::create_dir(path) { - Err(_) => Err(CLIError { + Err(_) => Err(CliError { message: format!("Html dir {} can not be created", path.display()), }), Ok(_) => Ok(Some(path.to_path_buf())), @@ -287,13 +287,13 @@ fn html_report(matches: ArgMatches) -> Result, CLIErr } } -fn variables(matches: ArgMatches) -> Result, CLIError> { +fn variables(matches: ArgMatches) -> Result, CliError> { let mut variables = HashMap::new(); if let Some(filename) = matches.value_of("variables_file") { let path = std::path::Path::new(filename); if !path.exists() { - return Err(CLIError { + return Err(CliError { message: format!("Properties file {} does not exist", path.display()), }); } @@ -304,7 +304,7 @@ fn variables(matches: ArgMatches) -> Result, CLIError> { let line = match line { Ok(s) => s, Err(_) => { - return Err(CLIError { + return Err(CliError { message: format!("Can not parse line {} of {}", index + 1, path.display()), }) } @@ -513,7 +513,7 @@ fn app() -> clap::App<'static, 'static> { pub fn unwrap_or_exit( log_error_message: &impl Fn(bool, &str), - result: Result, + result: Result, ) -> T { match result { Ok(v) => v, @@ -524,7 +524,7 @@ pub fn unwrap_or_exit( } } -fn parse_options(matches: ArgMatches) -> Result { +fn parse_options(matches: ArgMatches) -> Result { let verbose = matches.is_present("verbose") || matches.is_present("interactive"); let color = output_color(matches.clone()); let fail_fast = !matches.is_present("fail_at_end"); @@ -543,7 +543,7 @@ fn parse_options(matches: ArgMatches) -> Result { Some(s) => match s.parse::() { Ok(x) => Some(x), Err(_) => { - return Err(CLIError { + return Err(CliError { message: "max_redirs option can not be parsed".to_string(), }); } @@ -555,7 +555,7 @@ fn parse_options(matches: ArgMatches) -> Result { Some(s) => match s.parse::() { Ok(n) => Duration::from_secs(n), Err(_) => { - return Err(CLIError { + return Err(CliError { message: "max_time option can not be parsed".to_string(), }); } @@ -567,7 +567,7 @@ fn parse_options(matches: ArgMatches) -> Result { Some(s) => match s.parse::() { Ok(n) => Duration::from_secs(n), Err(_) => { - return Err(CLIError { + return Err(CliError { message: "connect-timeout option can not be parsed".to_string(), }); } @@ -576,7 +576,7 @@ fn parse_options(matches: ArgMatches) -> Result { let compressed = matches.is_present("compressed"); let user = matches.value_of("user").map(|x| x.to_string()); let interactive = matches.is_present("interactive"); - Ok(CLIOptions { + Ok(CliOptions { verbose, color, fail_fast, @@ -810,7 +810,7 @@ fn exit_code(hurl_results: Vec) -> i32 { } } -fn write_output(bytes: Vec, filename: Option<&str>) -> Result<(), CLIError> { +fn write_output(bytes: Vec, filename: Option<&str>) -> Result<(), CliError> { match filename { None => { let stdout = io::stdout(); @@ -825,7 +825,7 @@ fn write_output(bytes: Vec, filename: Option<&str>) -> Result<(), CLIError> let path = Path::new(filename); let mut file = match std::fs::File::create(&path) { Err(why) => { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", path.display(), why), }); } @@ -838,9 +838,9 @@ fn write_output(bytes: Vec, filename: Option<&str>) -> Result<(), CLIError> } } -pub fn cookies_output_file(filename: String, n: usize) -> Result { +pub fn cookies_output_file(filename: String, n: usize) -> Result { if n > 1 { - Err(CLIError { + Err(CliError { message: "Only save cookies for a unique session".to_string(), }) } else { @@ -849,10 +849,10 @@ pub fn cookies_output_file(filename: String, n: usize) -> Result) -> Result<(), CLIError> { +fn write_cookies_file(file_path: PathBuf, hurl_results: Vec) -> Result<(), CliError> { let mut file = match std::fs::File::create(&file_path) { Err(why) => { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", file_path.display(), why), }); } @@ -865,7 +865,7 @@ fn write_cookies_file(file_path: PathBuf, hurl_results: Vec) -> Resu .to_string(); match hurl_results.first() { None => { - return Err(CLIError { + return Err(CliError { message: "Issue fetching results".to_string(), }); } @@ -878,14 +878,14 @@ fn write_cookies_file(file_path: PathBuf, hurl_results: Vec) -> Resu } if let Err(why) = file.write_all(s.as_bytes()) { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", file_path.display(), why), }); } Ok(()) } -fn write_html_report(dir_path: PathBuf, hurl_results: Vec) -> Result<(), CLIError> { +fn write_html_report(dir_path: PathBuf, hurl_results: Vec) -> Result<(), CliError> { //let now: DateTime = Utc::now(); let now: DateTime = Local::now(); let html = create_html_index(now.to_rfc2822(), hurl_results); @@ -894,14 +894,14 @@ fn write_html_report(dir_path: PathBuf, hurl_results: Vec) -> Result let file_path = dir_path.join("index.html"); let mut file = match std::fs::File::create(&file_path) { Err(why) => { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", file_path.display(), why), }); } Ok(file) => file, }; if let Err(why) = file.write_all(s.as_bytes()) { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", file_path.display(), why), }); } @@ -909,14 +909,14 @@ fn write_html_report(dir_path: PathBuf, hurl_results: Vec) -> Result let file_path = dir_path.join("report.css"); let mut file = match std::fs::File::create(&file_path) { Err(why) => { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", file_path.display(), why), }); } Ok(file) => file, }; if let Err(why) = file.write_all(include_bytes!("report.css")) { - return Err(CLIError { + return Err(CliError { message: format!("Issue writing to {}: {:?}", file_path.display(), why), }); } diff --git a/packages/hurl/src/runner/content_decoding.rs b/packages/hurl/src/runner/content_decoding.rs index 884e20589..305196bc0 100644 --- a/packages/hurl/src/runner/content_decoding.rs +++ b/packages/hurl/src/runner/content_decoding.rs @@ -48,9 +48,9 @@ impl Encoding { pub fn decode(&self, data: &[u8]) -> Result, RunnerError> { match self { Encoding::Identity => Ok(data.to_vec()), - Encoding::Gzip => uncompress_gzip(&data[..]), - Encoding::Deflate => uncompress_zlib(&data[..]), - Encoding::Brotli => uncompress_brotli(&data[..]), + Encoding::Gzip => uncompress_gzip(data), + Encoding::Deflate => uncompress_zlib(data), + Encoding::Brotli => uncompress_brotli(data), } } } diff --git a/packages/hurl/src/runner/core.rs b/packages/hurl/src/runner/core.rs index 79838d8d5..4d0e0e389 100644 --- a/packages/hurl/src/runner/core.rs +++ b/packages/hurl/src/runner/core.rs @@ -121,7 +121,7 @@ pub enum RunnerError { InvalidJson { value: String, }, - InvalidURL(String), + InvalidUrl(String), HttpConnection { url: String, @@ -133,7 +133,7 @@ pub enum RunnerError { Timeout, TooManyRedirect, CouldNotParseResponse, - SSLCertificate(String), + SslCertificate(String), UnsupportedContentEncoding(String), CouldNotUncompressResponse(String), diff --git a/packages/hurl/src/runner/entry.rs b/packages/hurl/src/runner/entry.rs index 6b0995b67..774c52ac7 100644 --- a/packages/hurl/src/runner/entry.rs +++ b/packages/hurl/src/runner/entry.rs @@ -109,10 +109,10 @@ pub fn run( HttpError::Timeout => RunnerError::Timeout, HttpError::TooManyRedirect => RunnerError::TooManyRedirect, HttpError::CouldNotParseResponse => RunnerError::CouldNotParseResponse, - HttpError::SSLCertificate(description) => RunnerError::SSLCertificate( + HttpError::SslCertificate(description) => RunnerError::SslCertificate( description.unwrap_or_else(|| "SSL certificate problem".to_string()), ), - HttpError::InvalidUrl => RunnerError::InvalidURL(http_request.url.clone()), + HttpError::InvalidUrl => RunnerError::InvalidUrl(http_request.url.clone()), HttpError::StatuslineIsMissing => RunnerError::HttpConnection { message: "status line is missing".to_string(), url: http_request.url.clone(), diff --git a/packages/hurl/src/runner/error.rs b/packages/hurl/src/runner/error.rs index 9c6bf8253..4d03599a7 100644 --- a/packages/hurl/src/runner/error.rs +++ b/packages/hurl/src/runner/error.rs @@ -15,7 +15,7 @@ impl Error for runner::Error { fn description(&self) -> String { match &self.inner { - RunnerError::InvalidURL(..) => "Invalid url".to_string(), + RunnerError::InvalidUrl(..) => "Invalid url".to_string(), RunnerError::TemplateVariableNotDefined { .. } => "Undefined Variable".to_string(), RunnerError::VariableNotDefined { .. } => "Undefined Variable".to_string(), RunnerError::HttpConnection { .. } => "Http Connection".to_string(), @@ -25,7 +25,7 @@ impl Error for runner::Error { RunnerError::Timeout => "Http Connection".to_string(), RunnerError::TooManyRedirect => "Http Connection".to_string(), RunnerError::CouldNotParseResponse => "Http Connection".to_string(), - RunnerError::SSLCertificate { .. } => "SSL Certificate".to_string(), + RunnerError::SslCertificate { .. } => "SSL Certificate".to_string(), RunnerError::PredicateValue { .. } => "Assert - Predicate Value Failed".to_string(), RunnerError::InvalidRegex {} => "Invalid regex".to_string(), RunnerError::FileReadAccess { .. } => "File ReadAccess".to_string(), @@ -54,7 +54,7 @@ impl Error for runner::Error { fn fixme(&self) -> String { match &self.inner { - RunnerError::InvalidURL(url) => format!("Invalid url <{}>", url), + RunnerError::InvalidUrl(url) => format!("Invalid url <{}>", url), RunnerError::TemplateVariableNotDefined { name } => { format!("You must set the variable {}", name) } @@ -67,7 +67,7 @@ impl Error for runner::Error { RunnerError::Timeout => "Timeout has been reached".to_string(), RunnerError::TooManyRedirect => "Too many redirect".to_string(), RunnerError::CouldNotParseResponse => "Could not parse response".to_string(), - RunnerError::SSLCertificate(description) => description.clone(), + RunnerError::SslCertificate(description) => description.clone(), RunnerError::AssertVersion { actual, .. } => format!("actual value is <{}>", actual), RunnerError::AssertStatus { actual, .. } => format!("actual value is <{}>", actual), RunnerError::PredicateValue(value) => { diff --git a/packages/hurl/src/runner/query.rs b/packages/hurl/src/runner/query.rs index 3de60698e..c637ec103 100644 --- a/packages/hurl/src/runner/query.rs +++ b/packages/hurl/src/runner/query.rs @@ -99,7 +99,7 @@ pub fn eval_query( }; match result { Ok(value) => Ok(Some(value)), - Err(xpath::XpathError::InvalidXML {}) => Err(Error { + Err(xpath::XpathError::InvalidXml {}) => Err(Error { source_info: query.source_info, inner: RunnerError::QueryInvalidXml, assert: false, diff --git a/packages/hurl/src/runner/xpath.rs b/packages/hurl/src/runner/xpath.rs index b65069cc3..131bcbaa2 100644 --- a/packages/hurl/src/runner/xpath.rs +++ b/packages/hurl/src/runner/xpath.rs @@ -25,7 +25,7 @@ use super::value::Value; #[derive(Clone, Debug, PartialEq, Eq)] pub enum XpathError { - InvalidXML, + InvalidXml, InvalidHtml, Eval, Unsupported, @@ -36,12 +36,12 @@ pub fn eval_xml(xml: String, expr: String) -> Result { match parser.parse_string(xml) { Ok(doc) => { if doc.get_root_element() == None { - Err(XpathError::InvalidXML {}) + Err(XpathError::InvalidXml {}) } else { eval(doc, expr) } } - Err(_) => Err(XpathError::InvalidXML {}), + Err(_) => Err(XpathError::InvalidXml {}), } } @@ -151,7 +151,7 @@ mod tests { eval_xml(String::from("??"), String::from("//person")) .err() .unwrap(), - XpathError::InvalidXML + XpathError::InvalidXml ); } diff --git a/packages/hurl/tests/libcurl.rs b/packages/hurl/tests/libcurl.rs index 422c57323..dd6bcd24a 100644 --- a/packages/hurl/tests/libcurl.rs +++ b/packages/hurl/tests/libcurl.rs @@ -542,7 +542,7 @@ fn test_error_ssl() { } else { "SSL certificate problem: self signed certificate".to_string() }; - assert_eq!(error, HttpError::SSLCertificate(Some(message))); + assert_eq!(error, HttpError::SslCertificate(Some(message))); } #[test] diff --git a/packages/hurl_core/src/parser/template.rs b/packages/hurl_core/src/parser/template.rs index 5ff37bd7c..189601e2b 100644 --- a/packages/hurl_core/src/parser/template.rs +++ b/packages/hurl_core/src/parser/template.rs @@ -34,7 +34,7 @@ pub fn templatize(encoded_string: EncodedString) -> ParseResult<'static, Vec) { /// Similar to the standard read_to_string() /// But remove any existing BOM -pub fn read_to_string(filename: &str) -> Result { +pub fn read_to_string(filename: &str) -> Result { let mut f = match File::open(&filename) { Ok(f) => f, Err(e) => { - return Err(CLIError { + return Err(CliError { message: e.to_string(), }) } @@ -42,19 +42,19 @@ pub fn read_to_string(filename: &str) -> Result { let metadata = fs::metadata(&filename).expect("unable to read metadata"); let mut buffer = vec![0; metadata.len() as usize]; if let Err(e) = f.read(&mut buffer) { - return Err(CLIError { + return Err(CliError { message: e.to_string(), }); } string_from_utf8(buffer) } -pub fn string_from_utf8(buffer: Vec) -> Result { +pub fn string_from_utf8(buffer: Vec) -> Result { let mut buffer = buffer; strip_bom(&mut buffer); match String::from_utf8(buffer) { Ok(s) => Ok(s), - Err(e) => Err(CLIError { + Err(e) => Err(CliError { message: e.to_string(), }), } @@ -94,7 +94,7 @@ pub mod tests { ); assert_eq!( string_from_utf8(vec![0xef]).err().unwrap(), - CLIError { + CliError { message: "incomplete utf-8 byte sequence from index 0".to_string() } ); diff --git a/packages/hurlfmt/src/cli/mod.rs b/packages/hurlfmt/src/cli/mod.rs index 879f02db0..fc8dfffc2 100644 --- a/packages/hurlfmt/src/cli/mod.rs +++ b/packages/hurlfmt/src/cli/mod.rs @@ -26,6 +26,6 @@ mod fs; mod logger; #[derive(Clone, Debug, PartialEq, Eq)] -pub struct CLIError { +pub struct CliError { pub message: String, } diff --git a/packages/hurlfmt/src/format/json.rs b/packages/hurlfmt/src/format/json.rs index 46d46cc18..dbcd55acc 100644 --- a/packages/hurlfmt/src/format/json.rs +++ b/packages/hurlfmt/src/format/json.rs @@ -38,8 +38,7 @@ impl ToJson for HurlFile { impl ToJson for Entry { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("request".to_string(), self.request.to_json())); + let mut attributes = vec![("request".to_string(), self.request.to_json())]; if let Some(response) = self.response.clone() { attributes.push(("response".to_string(), response.to_json())); } @@ -49,12 +48,13 @@ impl ToJson for Entry { impl ToJson for Request { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(( - "method".to_string(), - JValue::String(self.method.to_string()), - )); - attributes.push(("url".to_string(), JValue::String(self.url.to_string()))); + let mut attributes = vec![ + ( + "method".to_string(), + JValue::String(self.method.to_string()), + ), + ("url".to_string(), JValue::String(self.url.to_string())), + ]; add_headers(&mut attributes, self.headers.clone()); if !self.clone().querystring_params().is_empty() { @@ -181,9 +181,10 @@ fn get_json_version(version_value: VersionValue) -> Option { impl ToJson for KeyValue { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("name".to_string(), JValue::String(self.key.value.clone()))); - attributes.push(("value".to_string(), JValue::String(self.value.to_string()))); + let attributes = vec![ + ("name".to_string(), JValue::String(self.key.value.clone())), + ("value".to_string(), JValue::String(self.value.to_string())), + ]; JValue::Object(attributes) } } @@ -199,12 +200,13 @@ impl ToJson for MultipartParam { impl ToJson for FileParam { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("name".to_string(), JValue::String(self.key.value.clone()))); - attributes.push(( - "filename".to_string(), - JValue::String(self.value.filename.value.clone()), - )); + let mut attributes = vec![ + ("name".to_string(), JValue::String(self.key.value.clone())), + ( + "filename".to_string(), + JValue::String(self.value.filename.value.clone()), + ), + ]; if let Some(content_type) = self.value.content_type.clone() { attributes.push(("content_type".to_string(), JValue::String(content_type))); } @@ -214,21 +216,23 @@ impl ToJson for FileParam { impl ToJson for Cookie { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("name".to_string(), JValue::String(self.name.value.clone()))); - attributes.push(( - "value".to_string(), - JValue::String(self.value.value.clone()), - )); + let attributes = vec![ + ("name".to_string(), JValue::String(self.name.value.clone())), + ( + "value".to_string(), + JValue::String(self.value.value.clone()), + ), + ]; JValue::Object(attributes) } } impl ToJson for Capture { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("name".to_string(), JValue::String(self.name.value.clone()))); - attributes.push(("query".to_string(), self.query.to_json())); + let mut attributes = vec![ + ("name".to_string(), JValue::String(self.name.value.clone())), + ("query".to_string(), self.query.to_json()), + ]; if let Some(subquery) = self.subquery.clone() { attributes.push(("subquery".to_string(), subquery.to_json())); } @@ -238,17 +242,16 @@ impl ToJson for Capture { impl ToJson for Assert { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("query".to_string(), self.query.to_json())); - attributes.push(("predicate".to_string(), self.predicate.to_json())); + let attributes = vec![ + ("query".to_string(), self.query.to_json()), + ("predicate".to_string(), self.predicate.to_json()), + ]; JValue::Object(attributes) } } impl ToJson for Query { fn to_json(&self) -> JValue { - let mut attributes = vec![]; - attributes.push(("type".to_string(), self.value.to_json())); self.value.to_json() } } diff --git a/packages/hurlfmt/src/format/token.rs b/packages/hurlfmt/src/format/token.rs index 76444ec9d..dec9de423 100644 --- a/packages/hurlfmt/src/format/token.rs +++ b/packages/hurlfmt/src/format/token.rs @@ -154,8 +154,7 @@ impl Tokenizable for Status { impl Tokenizable for Version { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::Version(format!( + vec![Token::Version(format!( "HTTP/{}", match self.value { VersionValue::Version1 => String::from("1.0"), @@ -163,8 +162,7 @@ impl Tokenizable for Version { VersionValue::Version2 => String::from("2"), VersionValue::VersionAny => String::from("*"), } - ))); - tokens + ))] } } @@ -340,8 +338,7 @@ impl Tokenizable for FileParam { impl Tokenizable for FileValue { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::Keyword("file,".to_string())); + let mut tokens: Vec = vec![Token::Keyword("file,".to_string())]; tokens.append(&mut self.space0.tokenize()); tokens.append(&mut self.filename.tokenize()); tokens.append(&mut self.space1.tokenize()); @@ -377,9 +374,7 @@ impl Tokenizable for Cookie { impl Tokenizable for CookieValue { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::Value(self.clone().value)); - tokens + vec![Token::Value(self.clone().value)] } } @@ -486,8 +481,7 @@ impl Tokenizable for CookiePath { impl Tokenizable for CookieAttribute { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::CodeDelimiter("[".to_string())); + let mut tokens: Vec = vec![Token::CodeDelimiter("[".to_string())]; add_tokens(&mut tokens, self.space0.tokenize()); tokens.push(Token::String(self.name.value())); add_tokens(&mut tokens, self.space1.tokenize()); @@ -720,9 +714,7 @@ impl Tokenizable for TemplateElement { fn tokenize(&self) -> Vec { match self { TemplateElement::String { encoded, .. } => { - let mut tokens: Vec = vec![]; - tokens.push(Token::String(encoded.to_string())); - tokens + vec![Token::String(encoded.to_string())] } TemplateElement::Expression(value) => { let mut tokens: Vec = vec![]; @@ -735,8 +727,7 @@ impl Tokenizable for TemplateElement { impl Tokenizable for Expr { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::CodeDelimiter(String::from("{{"))); + let mut tokens: Vec = vec![Token::CodeDelimiter(String::from("{{"))]; add_tokens(&mut tokens, self.space0.tokenize()); tokens.push(Token::CodeVariable(self.variable.name.clone())); add_tokens(&mut tokens, self.space1.tokenize()); @@ -769,9 +760,7 @@ impl Tokenizable for Whitespace { impl Tokenizable for Comment { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::Comment(format!("#{}", self.clone().value))); - tokens + vec![Token::Comment(format!("#{}", self.clone().value))] } } @@ -831,8 +820,7 @@ impl Tokenizable for JsonValue { impl Tokenizable for JsonListElement { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::Whitespace(self.space0.clone())); + let mut tokens: Vec = vec![Token::Whitespace(self.space0.clone())]; tokens.append(&mut self.value.tokenize()); tokens.push(Token::Whitespace(self.space1.clone())); tokens @@ -841,8 +829,7 @@ impl Tokenizable for JsonListElement { impl Tokenizable for JsonObjectElement { fn tokenize(&self) -> Vec { - let mut tokens: Vec = vec![]; - tokens.push(Token::Whitespace(self.space0.clone())); + let mut tokens: Vec = vec![Token::Whitespace(self.space0.clone())]; tokens.push(Token::Quote("\"".to_string())); tokens.push(Token::String(self.name.clone())); tokens.push(Token::Quote("\"".to_string()));