diff options
Diffstat (limited to 'src/barbfile.rs')
-rw-r--r-- | src/barbfile.rs | 123 |
1 files changed, 58 insertions, 65 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs index e5ef916..884dcee 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -8,9 +8,6 @@ use std::str::FromStr; use std::string::ToString; use std::{error::Error, fmt}; -#[cfg(feature = "jq")] -use jq_rs; - #[derive(Debug)] pub struct BarbParseError {} @@ -27,11 +24,11 @@ impl fmt::Display for BarbParseError { } pub enum Method { - GET, - PUT, - POST, - PATCH, - DELETE, + Get, + Put, + Post, + Patch, + Delete, } impl FromStr for Method { @@ -39,11 +36,11 @@ impl FromStr for Method { fn from_str(s: &str) -> Result<Self, Self::Err> { match s { - "GET" => Ok(Self::GET), - "PUT" => Ok(Self::PUT), - "POST" => Ok(Self::POST), - "PATCH" => Ok(Self::PATCH), - "DELETE" => Ok(Self::DELETE), + "GET" => Ok(Self::Get), + "PUT" => Ok(Self::Put), + "POST" => Ok(Self::Post), + "PATCH" => Ok(Self::Patch), + "DELETE" => Ok(Self::Delete), _ => Err(BarbParseError {}), } } @@ -52,22 +49,18 @@ impl FromStr for Method { impl ToString for Method { fn to_string(&self) -> String { match self { - Self::GET => String::from("GET"), - Self::PUT => String::from("PUT"), - Self::POST => String::from("POST"), - Self::PATCH => String::from("PATCH"), - Self::DELETE => String::from("DELETE"), + Self::Get => String::from("GET"), + Self::Put => String::from("PUT"), + Self::Post => String::from("POST"), + Self::Patch => String::from("PATCH"), + Self::Delete => String::from("DELETE"), } } } impl Method { pub fn takes_body(&self) -> bool { - match self { - Method::GET => false, - Method::DELETE => false, - _ => true, - } + !matches!(self, Method::Get | Method::Delete) } } @@ -94,8 +87,8 @@ impl Header { } enum FilterType { - JQ, - PATH, + Jq, + Path, } pub struct BarbFilter { @@ -118,8 +111,8 @@ impl FromStr for BarbFilter { }, String::from(&groups["filter"]), match &groups["type"] { - "|" => FilterType::JQ, - _ => FilterType::PATH, + "|" => FilterType::Jq, + _ => FilterType::Path, }, )) } @@ -144,7 +137,7 @@ impl BarbFilter { pub fn from_path(filter: String) -> BarbFilter { BarbFilter { name: None, - filter_type: FilterType::PATH, + filter_type: FilterType::Path, filter, } } @@ -159,23 +152,23 @@ impl BarbFilter { } #[cfg(feature = "jq")] - fn apply_jq(&self, body: &String) -> Result<String, String> { - if let FilterType::JQ = self.filter_type { + fn apply_jq(&self, body: &str) -> Result<String, String> { + if let FilterType::Path = self.filter_type { return Err(String::from("Incorrect filter type")); } - jq_rs::run(self.filter.as_str(), body.as_str()) + jq_rs::run(self.filter.as_str(), body) .map_err(|x| x.to_string()) .map(|x| String::from(x.trim().trim_matches('"'))) } - fn apply_path(&self, body: &String) -> Result<String, String> { - if let FilterType::JQ = self.filter_type { + fn apply_path(&self, body: &str) -> Result<String, String> { + if let FilterType::Jq = self.filter_type { return Err(String::from("Incorrect filter type")); } - let json: Value = serde_json::from_str(body.as_str()) - .map_err(|_| String::from("Failed to decode body"))?; + let json: Value = + serde_json::from_str(body).map_err(|_| String::from("Failed to decode body"))?; let path = &json.path(self.filter.as_str())?; Ok(match path { Value::Array(val) => match val.len() { @@ -190,11 +183,12 @@ impl BarbFilter { .to_string()) } - pub fn apply(&self, body: &String) -> Result<String, String> { + pub fn apply(&self, body: &str) -> Result<String, String> { match self.filter_type { #[cfg(feature = "jq")] - FilterType::JQ => self.apply_jq(body), - FilterType::PATH => self.apply_path(body), + FilterType::Jq => self.apply_jq(body), + FilterType::Path => self.apply_path(body), + #[cfg(not(feature = "jq"))] _ => Ok(body.to_string()), } } @@ -264,7 +258,7 @@ impl BarbFile { if !dep_path.is_absolute() { let my_path = Path::new(&self.file_name) .parent() - .or(Some(Path::new("")))?; + .or_else(|| Some(Path::new("")))?; return Some(String::from(my_path.join(dep_path).to_str()?)); } @@ -296,7 +290,7 @@ fn decode_url_line(line: &str) -> Result<(Method, String), BarbParseError> { let mut components = line[1..].split('^'); let meth = components.next().ok_or(BarbParseError {})?; let url = components.next().ok_or(BarbParseError {})?; - return Ok((Method::from_str(meth)?, String::from(url))); + Ok((Method::from_str(meth)?, String::from(url))) } fn decode_header(line: &str) -> Result<Header, BarbParseError> { @@ -333,24 +327,23 @@ impl FromStr for BarbFile { let mut dependency: Option<String> = None; for line in &mut lines { - if line == "" { + if line.is_empty() { // End of header. break; } - if let Some(_) = line.find(':') { + if line.find(':').is_some() { headers.push(decode_header(line).map_err(|_| BarbParseError {})?); } if let Some('>') = line.chars().nth(1) { - dependency = line.get(2..).map(|x| String::from(x)); + dependency = line.get(2..).map(String::from); } if BarbFilter::is_match(String::from(line)) { - match BarbFilter::from_str(line) { - Ok(filter) => filters.push(filter), - Err(_) => (), - }; + if let Ok(filter) = BarbFilter::from_str(line) { + filters.push(filter); + } } } @@ -359,7 +352,7 @@ impl FromStr for BarbFile { Ok(BarbFile { file_name: String::from(""), preamble: BarbPreamble::new(method, url, headers, filters, dependency), - body: if body == "" { None } else { Some(body) }, + body: if body.is_empty() { None } else { Some(body) }, }) } } @@ -370,29 +363,29 @@ mod tests { #[test] fn test_method_from_str() { - assert!(matches!(Method::from_str("GET").unwrap(), Method::GET)); - assert!(matches!(Method::from_str("PUT").unwrap(), Method::PUT)); - assert!(matches!(Method::from_str("POST").unwrap(), Method::POST)); - assert!(matches!(Method::from_str("PATCH").unwrap(), Method::PATCH)); + assert!(matches!(Method::from_str("GET").unwrap(), Method::Get)); + assert!(matches!(Method::from_str("PUT").unwrap(), Method::Put)); + assert!(matches!(Method::from_str("POST").unwrap(), Method::Post)); + assert!(matches!(Method::from_str("PATCH").unwrap(), Method::Patch)); assert!(matches!( Method::from_str("DELETE").unwrap(), - Method::DELETE + Method::Delete )); } #[test] fn test_method_takes_body() { - assert!(!Method::GET.takes_body()); - assert!(Method::PUT.takes_body()); - assert!(Method::POST.takes_body()); - assert!(Method::PATCH.takes_body()); - assert!(!Method::DELETE.takes_body()); + assert!(!Method::Get.takes_body()); + assert!(Method::Put.takes_body()); + assert!(Method::Post.takes_body()); + assert!(Method::Patch.takes_body()); + assert!(!Method::Delete.takes_body()); } #[test] fn test_decode_url_line() { let (method, url) = decode_url_line("#GET^http://blahblah").unwrap(); - assert!(matches!(method, Method::GET)); + assert!(matches!(method, Method::Get)); assert_eq!(url, "http://blahblah"); } @@ -408,7 +401,7 @@ mod tests { let barbfile = BarbFile::from_str("#GET^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n") .unwrap(); - assert!(matches!(barbfile.preamble.method, Method::GET)); + assert!(matches!(barbfile.preamble.method, Method::Get)); assert_eq!(barbfile.preamble.url, "https://blah.com/api/blah"); assert_eq!( barbfile.preamble.filters[0].filter(), @@ -425,7 +418,7 @@ mod tests { let barbfile = BarbFile::from_str("#POST^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n\n{\"key\":\"value\"}\n") .unwrap(); - assert!(matches!(barbfile.preamble.method, Method::POST)); + assert!(matches!(barbfile.preamble.method, Method::Post)); assert_eq!(barbfile.preamble.url, "https://blah.com/api/blah"); assert_eq!( barbfile.preamble.filters[0].filter(), @@ -442,7 +435,7 @@ mod tests { let filter = BarbFilter::from_str("#FOO|.bar.foo").unwrap(); assert_eq!(filter.name, Some(String::from("FOO"))); assert_eq!(filter.filter, String::from(".bar.foo")); - assert!(matches!(filter.filter_type, FilterType::JQ)); + assert!(matches!(filter.filter_type, FilterType::Jq)); } #[test] @@ -450,7 +443,7 @@ mod tests { let filter = BarbFilter::from_str("#|.bar.foo").unwrap(); assert_eq!(filter.name, None); assert_eq!(filter.filter, String::from(".bar.foo")); - assert!(matches!(filter.filter_type, FilterType::JQ)); + assert!(matches!(filter.filter_type, FilterType::Jq)); } #[test] @@ -458,7 +451,7 @@ mod tests { let filter = BarbFilter::from_str("#FOO$$.bar.foo").unwrap(); assert_eq!(filter.name, Some(String::from("FOO"))); assert_eq!(filter.filter, String::from("$.bar.foo")); - assert!(matches!(filter.filter_type, FilterType::PATH)); + assert!(matches!(filter.filter_type, FilterType::Path)); } #[test] @@ -466,7 +459,7 @@ mod tests { let filter = BarbFilter::from_str("#$$.bar.foo").unwrap(); assert_eq!(filter.name, None); assert_eq!(filter.filter, String::from("$.bar.foo")); - assert!(matches!(filter.filter_type, FilterType::PATH)); + assert!(matches!(filter.filter_type, FilterType::Path)); } #[cfg(feature = "jq")] |