Refactor filters
Clean up some comments and refactor the code to improve readability and to get rid of "ugly" unwraps.
This commit is contained in:
parent
d92e8029f2
commit
b4b46ebd58
1 changed files with 22 additions and 16 deletions
|
@ -72,10 +72,6 @@ impl JsonFilter {
|
|||
);
|
||||
|
||||
let regex = Regex::new(&self.regex).map_err(WebhookeyError::Regex)?;
|
||||
// let value = self.get_string(data)?;
|
||||
|
||||
// if let Some(value) = self.get_string() {data.pointer(&self.pointer) {
|
||||
// let value = get_string();
|
||||
|
||||
if let Some(value) = data.pointer(&self.pointer) {
|
||||
if regex.is_match(&self.get_string(&value)?) {
|
||||
|
@ -121,33 +117,43 @@ impl FilterType {
|
|||
pub fn evaluate(&self, data: &serde_json::Value) -> Result<bool, WebhookeyError> {
|
||||
match self {
|
||||
FilterType::And(filters) => {
|
||||
let (results, errors): (Vec<_>, Vec<_>) = filters
|
||||
let (mut results, mut errors) = (Vec::new(), Vec::new());
|
||||
|
||||
filters
|
||||
.iter()
|
||||
.map(|filter| filter.evaluate(data))
|
||||
.partition(Result::is_ok);
|
||||
.for_each(|item| match item {
|
||||
Ok(o) => results.push(o),
|
||||
Err(e) => errors.push(e),
|
||||
});
|
||||
|
||||
if errors.is_empty() {
|
||||
Ok(results.iter().all(|r| *r.as_ref().unwrap())) // should never fail
|
||||
Ok(results.iter().all(|r| *r))
|
||||
} else {
|
||||
errors.iter().for_each(|e| {
|
||||
error!("Could not evaluate Filter: {}", e.as_ref().unwrap_err())
|
||||
});
|
||||
errors
|
||||
.iter()
|
||||
.for_each(|e| error!("Could not evaluate Filter: {}", e));
|
||||
|
||||
Err(WebhookeyError::InvalidFilter)
|
||||
}
|
||||
}
|
||||
FilterType::Or(filters) => {
|
||||
let (results, errors): (Vec<_>, Vec<_>) = filters
|
||||
let (mut results, mut errors) = (Vec::new(), Vec::new());
|
||||
|
||||
filters
|
||||
.iter()
|
||||
.map(|filter| filter.evaluate(data))
|
||||
.partition(Result::is_ok);
|
||||
.for_each(|item| match item {
|
||||
Ok(o) => results.push(o),
|
||||
Err(e) => errors.push(e),
|
||||
});
|
||||
|
||||
if errors.is_empty() {
|
||||
Ok(results.iter().any(|r| *r.as_ref().unwrap())) // should never fail
|
||||
Ok(results.iter().any(|r| *r))
|
||||
} else {
|
||||
errors.iter().for_each(|e| {
|
||||
error!("Could not evaluate Filter: {}", e.as_ref().unwrap_err())
|
||||
});
|
||||
errors
|
||||
.iter()
|
||||
.for_each(|e| error!("Could not evaluate Filter: {}", e));
|
||||
|
||||
Err(WebhookeyError::InvalidFilter)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue