Filter ranked search by public source and excluded text before pagination
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
//! Leading source and exclusion filters in normalized search text.
|
||||
use crate::{index::IndexedDocument, PublicName};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Scope<'a> {
|
||||
source: Option<PublicName>,
|
||||
excluded: Vec<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> Scope<'a> {
|
||||
pub fn parse(mut text: &'a str) -> Option<(Self, &'a str)> {
|
||||
let mut scope = Self::default();
|
||||
loop {
|
||||
let (token, rest) = text.split_once(' ').unwrap_or((text, ""));
|
||||
if let Some(source) = token.strip_prefix("from:") {
|
||||
let name = PublicName::new(source)?;
|
||||
if scope.source.as_ref().is_some_and(|s| s != &name) {
|
||||
return None;
|
||||
}
|
||||
scope.source = Some(name);
|
||||
} else if let Some(fragment) = token.strip_prefix("exclude:") {
|
||||
if fragment.is_empty() {
|
||||
return None;
|
||||
}
|
||||
scope.excluded.push(fragment);
|
||||
} else {
|
||||
return Some((scope, text));
|
||||
}
|
||||
text = rest;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn accepts(&self, entry: &IndexedDocument) -> bool {
|
||||
self.source
|
||||
.as_ref()
|
||||
.is_none_or(|s| s == &entry.document.username)
|
||||
&& !self
|
||||
.excluded
|
||||
.iter()
|
||||
.any(|f| entry.title.contains(f) || entry.body.contains(f))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user