added SimilarityFilter to filters

This commit is contained in:
epi
2020-12-26 13:46:20 -06:00
parent c777ab4f67
commit d530329478
2 changed files with 32 additions and 0 deletions

View File

@@ -41,6 +41,7 @@ regex = "1"
crossterm = "0.18"
rlimit = "0.5"
ctrlc = "3.1"
strsim = "0.10"
[dev-dependencies]
tempfile = "3.1"

View File

@@ -4,6 +4,7 @@ use crate::FeroxResponse;
use regex::Regex;
use std::any::Any;
use std::fmt::Debug;
use strsim::normalized_levenshtein;
// references:
// https://dev.to/magnusstrale/rust-trait-objects-in-a-vector-non-trivial-4co5
@@ -282,6 +283,36 @@ impl PartialEq for RegexFilter {
}
}
/// Simple implementor of FeroxFilter; used to filter out responses based on the similarity of a
/// Response body with a known response; specified using --filter-similar-to
#[derive(Default, Debug, PartialEq)]
pub struct SimilarityFilter {
/// Response's body to be used for comparison for similarity
pub text: String,
/// Percentage of similarity at which a page is determined to be a near-duplicate of another
pub threshold: f64,
}
/// implementation of FeroxFilter for SimilarityFilter
impl FeroxFilter for SimilarityFilter {
/// Check `FeroxResponse::text` against what was requested from the site passed in via
/// --filter-similar-to
fn should_filter_response(&self, response: &FeroxResponse) -> bool {
(normalized_levenshtein(&self.text, &response.text) - self.threshold).abs() <= 0.00001
}
/// Compare one SizeFilter to another
fn box_eq(&self, other: &dyn Any) -> bool {
other.downcast_ref::<Self>().map_or(false, |a| self == a)
}
/// Return self as Any for dynamic dispatch purposes
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;