From ea3121a9859dc54c1b8241c5ce01a74d64cde0b6 Mon Sep 17 00:00:00 2001 From: finga Date: Tue, 13 Apr 2021 16:55:13 +0200 Subject: [PATCH] Fix matching numbers against the regex Still, `Null`, `Bool`, `Array` and `Object` types are unmatchable. Work TBD... --- src/main.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1d2d6db..01ee2e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -189,6 +189,15 @@ fn replace_parameter(input: &str, headers: &HeaderMap, data: &serde_json::Value) Ok(result.join("")) } +fn number_to_string(number: &serde_json::Number) -> String { + if number.is_f64() { + return number.as_f64().unwrap().to_string(); + } else if number.is_i64() { + return number.as_i64().unwrap().to_string(); + } + number.as_u64().unwrap().to_string() +} + fn filter_match( hook_name: &str, hook: &Hook, @@ -202,26 +211,27 @@ fn filter_match( let regex = Regex::new(&filter.regex)?; if let Some(value) = data.pointer(&filter.pointer) { - if let Some(value) = value.as_str() { - if regex.is_match(value) { - debug!("Filter `{}` of hook `{}` matched", filter_name, hook_name); + let value = match &value { + serde_json::Value::Null => unimplemented!(), + serde_json::Value::Bool(_bool) => unimplemented!(), + serde_json::Value::Number(number) => number_to_string(number), + serde_json::Value::String(string) => string.as_str().to_string(), + serde_json::Value::Array(_array) => unimplemented!(), + serde_json::Value::Object(_object) => unimplemented!(), + }; - return Ok(Some(replace_parameter( - &hook.command.to_string(), - &request.headers(), - data, - )?)); - } - } else { - bail!( - "Could not parse pointer in hook `{}` from filter `{}`", - hook_name, - filter_name - ); + if regex.is_match(&value) { + debug!("Filter `{}` of hook `{}` matched", filter_name, hook_name); + + return Ok(Some(replace_parameter( + &hook.command.to_string(), + &request.headers(), + data, + )?)); } } - trace!( + debug!( "Filter `{}` of hook `{}` did not match", filter_name, hook_name