diff options
Diffstat (limited to 'src/executor.rs')
-rw-r--r-- | src/executor.rs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/executor.rs b/src/executor.rs index 6b1bc5a..b296c65 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -2,7 +2,6 @@ use crate::barbfile::{BarbFile, BarbFilter}; use crate::output::BarbOutput; use regex::Regex; use std::collections::HashMap; -use ureq; use ureq::Error as UreqError; pub struct Context { @@ -28,21 +27,21 @@ impl Context { } #[cfg(test)] - fn key_str(&self, key: &String) -> String { - // Use for reference / utility? + fn key_str(&self, key: &str) -> String { format!("{{{}}}", key) } - pub fn substitute(&self, string: &String) -> String { - let mut buffer = string.clone(); + pub fn substitute(&self, string: &str) -> String { + let mut buffer = string.to_string(); let re = Regex::new(r"\{(?P<key>[A-Za-z0-9_]+)(?::-(?P<value>.*))?\}").unwrap(); + let empty_string = String::from(""); for var in re.captures_iter(string) { let key = String::from(&var["key"]); let value = String::from( var.name("value") .map(|m| m.as_str()) - .or(Some(&String::from(""))) + .or(Some(&empty_string)) .unwrap(), ); @@ -75,7 +74,7 @@ impl Executor { ) -> ureq::Request { let mut req = ureq::request( bfile.method_as_string().as_str(), - self.context.substitute(&bfile.url()).as_str(), + self.context.substitute(bfile.url()).as_str(), ); let mut final_headers: HashMap<String, String> = HashMap::new(); @@ -104,8 +103,8 @@ impl Executor { arg_filter: &Option<String>, ) -> Result<String, String> { if let Some(filter) = arg_filter { - return Ok(BarbFilter::from_path(filter.to_string()).apply(&body)?); - } else if bfile.filters().len() > 0 { + return BarbFilter::from_path(filter.to_string()).apply(&body); + } else if !bfile.filters().is_empty() { let mut end_body: String = body.clone(); for filter in bfile.filters().iter() { if let Some(name) = filter.name() { @@ -116,7 +115,7 @@ impl Executor { } return Ok(end_body); } - Ok(String::from(body)) + Ok(body) } fn run(&self, bfile: &BarbFile, req: ureq::Request) -> Result<ureq::Response, String> { @@ -131,20 +130,20 @@ impl Executor { match resp { Ok(resp) => Ok(resp), Err(UreqError::Status(_, resp)) => Ok(resp), - Err(UreqError::Transport(transp)) => Err(String::from(transp.to_string())), + Err(UreqError::Transport(transp)) => Err(transp.to_string()), } } pub fn execute_dep(&mut self, bfile: &BarbFile, output: &BarbOutput) -> Result<(), String> { - let req = self.make_req(&bfile, vec![], output); + let req = self.make_req(bfile, vec![], output); let method = String::from(req.method()); let url = String::from(req.url()); - let response = self.run(&bfile, req)?; + let response = self.run(bfile, req)?; output.req_dep(method, url, response.status(), response.status_text()); output.end_req(); - self.apply_filters(&bfile, response.into_string().unwrap(), &None)?; + self.apply_filters(bfile, response.into_string().unwrap(), &None)?; Ok(()) } @@ -157,12 +156,12 @@ impl Executor { skip_filters: bool, headers: Vec<(String, String)>, ) -> Result<(), String> { - let req = self.make_req(&bfile, headers, output); + let req = self.make_req(bfile, headers, output); output.req(String::from(req.method()), String::from(req.url())); output.end_req(); - let response = self.run(&bfile, req)?; + let response = self.run(bfile, req)?; output.status(response.status(), response.status_text()); for header_name in response.headers_names() { @@ -174,7 +173,7 @@ impl Executor { output.end_resp_hdr(); if !skip_filters { - output.body(self.apply_filters(&bfile, response.into_string().unwrap(), filter)?); + output.body(self.apply_filters(bfile, response.into_string().unwrap(), filter)?); } else { output.body(response.into_string().unwrap()); } |