web search done right
Parse site block rules into 'HostRankings' instead. (#170)
Mikkel Denker7583aa426ba8
diff --git a/crates/core/src/query/optic.rs b/crates/core/src/query/optic.rsindex 7c7920e3..e51e6907 100644--- a/crates/core/src/query/optic.rs+++ b/crates/core/src/query/optic.rs@@ -1720,6 +1720,20 @@ mod tests {.webpages;assert_eq!(res.len(), 1);assert_eq!(res[0].url, "https://example.com/test");++ let res = searcher+ .search(&SearchQuery {+ query: "example".to_string(),+ optic: Some(+ Optic::parse("Rule { Matches { Site(\"|example.com|\") }, Action(Discard) }")+ .unwrap(),+ ),+ ..Default::default()+ })+ .unwrap()+ .webpages;+ assert_eq!(res.len(), 1);+ assert_eq!(res[0].url, "https://another-example.com/");}#[test]diff --git a/crates/core/src/ranking/mod.rs b/crates/core/src/ranking/mod.rsindex de5d75ac..6082f789 100644--- a/crates/core/src/ranking/mod.rs+++ b/crates/core/src/ranking/mod.rs@@ -745,6 +745,19 @@ mod tests {let result = searcher.search(&SearchQuery {query: "test".to_string(),+ optic: Some(Optic {+ rankings: vec![+ RankingCoeff {+ target: RankingTarget::Signal("url_slashes".to_string()),+ value: 100_000.0,+ },+ RankingCoeff {+ target: RankingTarget::Signal("url_digits".to_string()),+ value: 100_000.0,+ },+ ],+ ..Default::default()+ }),..Default::default()}).expect("Search failed");diff --git a/crates/optics/src/lib.rs b/crates/optics/src/lib.rsindex 09bb2d55..06be67d7 100644--- a/crates/optics/src/lib.rs+++ b/crates/optics/src/lib.rs@@ -77,9 +77,18 @@ impl TryFrom<RawOptic> for Optic {fn try_from(raw: RawOptic) -> Result<Self> {let mut rules = Vec::new();+ let mut blocked = Vec::new();for rule in raw.rules {- rules.push(Rule::try_from(rule)?);+ let rule = Rule::try_from(rule)?;++ let blocked_sites = rule.as_blocked_sites();++ if blocked_sites.is_empty() {+ rules.push(rule);+ } else {+ blocked.extend(blocked_sites);+ }}let mut liked_hosts = Vec::new();@@ -99,7 +108,7 @@ impl TryFrom<RawOptic> for Optic {host_rankings: HostRankings {liked: liked_hosts,disliked: disliked_hosts,- blocked: Vec::new(), // blocked hosts are handled by `$discard` syntax.+ blocked,},})}@@ -349,6 +358,42 @@ pub struct Rule {/// What action to take if the rule matches.pub action: Action,}+impl Rule {+ /// If the rule is on the form `Rule { Matches { Site("|...|") }*, Action(Discard) }`, return the sites to block.+ /// If the rule is not on this exact form, return an empty vector instead.+ fn as_blocked_sites(&self) -> Vec<String> {+ let mut res = Vec::new();++ if self.action == Action::Discard {+ for matching in &self.matches {+ if matching.len() != 1 {+ return Vec::new();+ }++ let matching = &matching[0];++ if matching.pattern.len() != 3 {+ return Vec::new();+ }++ if matching.location == MatchLocation::Site+ && matching.pattern[0] == PatternPart::Anchor+ && matching.pattern[2] == PatternPart::Anchor+ {+ if let PatternPart::Raw(site) = &matching.pattern[1] {+ res.push(site.clone());+ } else {+ return Vec::new();+ }+ } else {+ return Vec::new();+ }+ }+ }++ res+ }+}impl Display for Rule {fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {