diff options
author | Guillaume Pasquet <dev@etenil.net> | 2022-02-27 22:46:50 +0000 |
---|---|---|
committer | Guillaume Pasquet <dev@etenil.net> | 2022-02-27 23:01:17 +0000 |
commit | 8e3fde02d8878ba1734305394c63eb841440a327 (patch) | |
tree | d0dc799bc5b6b6b00efa6e5dcf7bf915d873c0f8 | |
parent | 8a5c6db3159cdc9f350f9083ae7d190c050b76be (diff) |
Fix #9: Extract filter output for reuse in vars
-rw-r--r-- | src/barbfile.rs | 7 | ||||
-rw-r--r-- | src/executor.rs | 50 | ||||
-rw-r--r-- | test_api/test_api_extract_var.barb | 2 | ||||
-rw-r--r-- | test_api/test_api_get_use_var.barb | 2 |
4 files changed, 36 insertions, 25 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs index 35b0224..91ffd0a 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -2,6 +2,7 @@ use regex::Regex; use std::str::FromStr; use std::string::ToString; use std::{error::Error, fmt}; +use jq_rs; #[derive(Debug)] pub struct BarbParseError {} @@ -126,6 +127,12 @@ impl BarbFilter { pub fn filter(&self) -> &String { &self.filter } + + pub fn apply(&self, body: &String) -> Result<String, String> { + jq_rs::run(self.filter.as_str(), body.as_str()) + .map_err(|x| x.to_string()) + .map(|x| String::from(x.trim().trim_matches('"'))) + } } struct BarbPreamble { diff --git a/src/executor.rs b/src/executor.rs index 03477e3..15704c2 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -1,8 +1,6 @@ use crate::barbfile::{BarbFile, BarbFilter}; use crate::output::BarbOutput; -use jq_rs; - use std::collections::HashMap; use ureq; use ureq::Error as UreqError; @@ -54,26 +52,6 @@ impl Context { } } - -fn apply_filter(filter: &BarbFilter, body: &String) -> Result<String, String> { - jq_rs::run(filter.filter().as_str(), body.as_str()).map_err(|x| x.to_string()) -} - -fn apply_filters(bfile: &BarbFile, body: String, arg_filter: &Option<String>) -> Result<String, String> { - if let Some(filter) = arg_filter { - return Ok(apply_filter(&BarbFilter::new(None, filter.to_string()), &body)?); - } else if bfile.filters().len() > 0 { - return Ok(bfile - .filters() - .iter() - .map(|filter| apply_filter(filter, &body)) - .last() - .unwrap().unwrap()); - } - Ok(String::from(body)) -} - - pub struct Executor { context: Context, } @@ -104,6 +82,28 @@ impl Executor { req } + fn apply_filters( + &mut self, + bfile: &BarbFile, + body: String, + arg_filter: &Option<String>, + ) -> Result<String, String> { + if let Some(filter) = arg_filter { + return Ok(BarbFilter::new(None, filter.to_string()).apply(&body)?); + } else if bfile.filters().len() > 0 { + let mut end_body: String = body.clone(); + for filter in bfile.filters().iter() { + if let Some(name) = filter.name() { + self.context.vars.insert(name.clone(), filter.apply(&body)?); + } else { + end_body = filter.apply(&body)?; + } + } + return Ok(end_body); + } + Ok(String::from(body)) + } + fn run(&self, bfile: &BarbFile, req: ureq::Request) -> Result<ureq::Response, String> { let resp = match bfile.method().takes_body() { true => match bfile.body() { @@ -124,7 +124,7 @@ impl Executor { &mut self, file_name: &String, output: &BarbOutput, - filter: &Option<String> + filter: &Option<String>, ) -> Result<(), String> { let bfile = BarbFile::from_str( fs::read_to_string(file_name.as_str()) @@ -144,10 +144,10 @@ impl Executor { } output.end_resp_hdr(); - output.body(apply_filters( + output.body(self.apply_filters( &bfile, response.into_string().unwrap(), - filter + filter, )?); Ok(()) diff --git a/test_api/test_api_extract_var.barb b/test_api/test_api_extract_var.barb new file mode 100644 index 0000000..2a0ea90 --- /dev/null +++ b/test_api/test_api_extract_var.barb @@ -0,0 +1,2 @@ +#GET^http://localhost:8080/ +#FOO|.status diff --git a/test_api/test_api_get_use_var.barb b/test_api/test_api_get_use_var.barb new file mode 100644 index 0000000..c0cea1a --- /dev/null +++ b/test_api/test_api_get_use_var.barb @@ -0,0 +1,2 @@ +#GET^http://localhost:8080/ +#Foo: {FOO} |