diff options
Diffstat (limited to 'src/query.rs')
| -rw-r--r-- | src/query.rs | 335 |
1 files changed, 335 insertions, 0 deletions
diff --git a/src/query.rs b/src/query.rs new file mode 100644 index 0000000..2b53ae6 --- /dev/null +++ b/src/query.rs @@ -0,0 +1,335 @@ +//! The query language. +//! +//! A query is one string. Words such as `tag:X` are filters on the +//! metadata and the graph; everything else is passed on to the full +//! text index. See the README for the list. + +use crate::graph::Graph; +use crate::note::Note; +use anyhow::{Result, bail}; +use std::collections::HashSet; + +/// A pattern for tags and kinds: `*` (anything), `abc*` (prefix) or `abc`. +#[derive(Debug, PartialEq)] +pub enum Pat { + Any, + Prefix(String), + Exact(String), +} + +impl Pat { + fn new(v: &str) -> Pat { + match v.strip_suffix('*') { + Some("") => Pat::Any, + Some(p) => Pat::Prefix(p.to_string()), + None => Pat::Exact(v.to_string()), + } + } + + fn matches(&self, s: &str) -> bool { + match self { + Pat::Any => true, + Pat::Prefix(p) => s.starts_with(p.as_str()), + Pat::Exact(e) => s == e, + } + } +} + +/// A range of dates, compared as prefixes: `2025` is the whole year. +#[derive(Debug, PartialEq)] +pub struct DateRange { + pub from: Option<String>, + pub to: Option<String>, +} + +impl DateRange { + fn contains(&self, date: &str) -> bool { + let cut = |n: usize| date.get(..n).unwrap_or(date); + self.from.as_ref().is_none_or(|a| cut(a.len()) >= a.as_str()) + && self.to.as_ref().is_none_or(|b| cut(b.len()) <= b.as_str()) + } +} + +#[derive(Debug, Default, PartialEq)] +pub struct Query { + pub tags: Vec<Pat>, + pub no_tags: Vec<Pat>, + pub kinds: Vec<Pat>, + pub no_kinds: Vec<Pat>, + pub files: Vec<String>, + pub no_files: Vec<String>, + pub from: Vec<String>, + pub to: Vec<String>, + pub parent: Vec<String>, + pub thread: Vec<String>, + pub dates: Vec<DateRange>, + pub inbox: bool, + /// `like:X` and `-like:X`: note IDs or free text, for the embeddings. + pub like: Vec<String>, + pub unlike: Vec<String>, + /// What is left for the full text index. + pub words: String, +} + +/// Split on white space, but keep double-quoted stretches together. +/// The quotes stay in the tokens. +fn tokens(q: &str) -> Vec<String> { + let mut out = Vec::new(); + let mut cur = String::new(); + let mut quoted = false; + for c in q.chars() { + match c { + '"' => { + quoted = !quoted; + cur.push(c); + } + c if c.is_whitespace() && !quoted => { + if !cur.is_empty() { + out.push(std::mem::take(&mut cur)); + } + } + c => cur.push(c), + } + } + if !cur.is_empty() { + out.push(cur); + } + out +} + +fn unquote(v: &str) -> &str { + v.strip_prefix('"') + .map(|r| r.strip_suffix('"').unwrap_or(r)) + .unwrap_or(v) +} + +impl Query { + pub fn parse(q: &str) -> Result<Query> { + let mut out = Query::default(); + let mut words: Vec<String> = Vec::new(); + for tok in tokens(q) { + let (neg, rest) = match tok.strip_prefix('-') { + Some(r) if !r.is_empty() => (true, r), + _ => (false, tok.as_str()), + }; + let (key, value) = match rest.split_once(':') { + Some((k, v)) if !k.starts_with('"') => (k, v), + _ => { + words.push(tok); + continue; + } + }; + let known = matches!( + key, + "tag" | "kind" | "file" | "from" | "to" | "parent" | "thread" | "date" | "is" | "like" + ); + if !known { + // `title:foo` and friends belong to the text index. + words.push(tok); + continue; + } + let value = unquote(value); + if value.is_empty() { + bail!("empty value for {}:", key); + } + if neg && !matches!(key, "tag" | "kind" | "file" | "like") { + bail!("cannot negate {}:", key); + } + match key { + "tag" if neg => out.no_tags.push(Pat::new(value)), + "tag" => out.tags.push(Pat::new(value)), + "kind" if neg => out.no_kinds.push(Pat::new(value)), + "kind" => out.kinds.push(Pat::new(value)), + "file" if neg => out.no_files.push(value.to_string()), + "file" => out.files.push(value.to_string()), + "like" if neg => out.unlike.push(value.to_string()), + "like" => out.like.push(value.to_string()), + "from" => out.from.push(value.to_string()), + "to" => out.to.push(value.to_string()), + "parent" => out.parent.push(value.to_string()), + "thread" => out.thread.push(value.to_string()), + "date" => { + let range = match value.split_once("..") { + Some((a, b)) => DateRange { + from: (!a.is_empty()).then(|| a.to_string()), + to: (!b.is_empty()).then(|| b.to_string()), + }, + None => DateRange { + from: Some(value.to_string()), + to: Some(value.to_string()), + }, + }; + out.dates.push(range); + } + "is" => match value { + "inbox" => out.inbox = true, + _ => bail!("unknown is:{}", value), + }, + _ => unreachable!(), + } + } + out.words = words.join(" "); + Ok(out) + } + + /// Resolve the parts that need the graph, and return a matcher. + pub fn matcher<'a>(&'a self, g: &'a Graph, root: &'a str) -> Matcher<'a> { + let ids = |v: Vec<&Note>| -> HashSet<String> { + v.into_iter().filter_map(|n| n.id.clone()).collect() + }; + let mut sets: Vec<HashSet<String>> = Vec::new(); + for id in &self.from { + sets.push(ids(g.outlinks_of(id))); + } + for id in &self.to { + sets.push(ids(g.backlinks_of(id))); + } + for id in &self.parent { + sets.push(ids(g.children_of(id))); + } + for id in &self.thread { + sets.push(thread_of(g, id)); + } + Matcher { + q: self, + g, + root, + sets, + } + } +} + +/// All the notes in the thread of `id`: the root above it and every +/// descendant of that root. +fn thread_of(g: &Graph, id: &str) -> HashSet<String> { + let mut out = HashSet::new(); + if g.get(id).is_none() { + return out; + } + let mut root = id.to_string(); + let mut seen = HashSet::new(); + while seen.insert(root.clone()) { + match g.parent_of(&root).and_then(|p| p.id.clone()) { + Some(p) => root = p, + None => break, + } + } + let mut stack = vec![root]; + while let Some(cur) = stack.pop() { + if out.insert(cur.clone()) { + for c in g.children_of(&cur) { + if let Some(cid) = &c.id { + stack.push(cid.clone()); + } + } + } + } + out +} + +pub struct Matcher<'a> { + q: &'a Query, + g: &'a Graph, + root: &'a str, + sets: Vec<HashSet<String>>, +} + +impl Matcher<'_> { + pub fn pass(&self, note: &Note) -> bool { + let id = match ¬e.id { + Some(i) => i, + None => return false, + }; + let q = self.q; + let any_tag = |p: &Pat| note.tags.iter().any(|t| p.matches(t)); + let rel = note + .path + .strip_prefix(self.root) + .unwrap_or(¬e.path) + .trim_start_matches('/'); + q.tags.iter().all(|p| match p { + Pat::Any => !note.tags.is_empty(), + _ => any_tag(p), + }) && q.no_tags.iter().all(|p| match p { + Pat::Any => note.tags.is_empty(), + _ => !any_tag(p), + }) && q + .kinds + .iter() + .all(|p| note.kind.as_deref().is_some_and(|k| p.matches(k))) + && q.no_kinds.iter().all(|p| match p { + Pat::Any => note.kind.is_none(), + _ => !note.kind.as_deref().is_some_and(|k| p.matches(k)), + }) + && q.files.iter().all(|f| rel.contains(f.as_str())) + && q.no_files.iter().all(|f| !rel.contains(f.as_str())) + && self.sets.iter().all(|s| s.contains(id)) + && q.dates + .iter() + .all(|r| note.date.as_deref().is_some_and(|d| r.contains(d))) + && (!q.inbox || (!self.g.is_connected(note) && note.tags.is_empty())) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn filters_are_split_from_words() { + let q = Query::parse("tag:math -tag:old kind:idea \"exact phrase\" word title:foo").unwrap(); + assert_eq!(q.tags, vec![Pat::Exact("math".into())]); + assert_eq!(q.no_tags, vec![Pat::Exact("old".into())]); + assert_eq!(q.kinds, vec![Pat::Exact("idea".into())]); + assert_eq!(q.words, "\"exact phrase\" word title:foo"); + } + + #[test] + fn star_patterns() { + let q = Query::parse("tag:* -tag:alg* kind:*").unwrap(); + assert_eq!(q.tags, vec![Pat::Any]); + assert_eq!(q.no_tags, vec![Pat::Prefix("alg".into())]); + assert_eq!(q.kinds, vec![Pat::Any]); + assert_eq!(q.words, ""); + } + + #[test] + fn dates_and_quoted_values() { + let q = Query::parse("date:2025-03..2025-06 date:..2024 date:2020 file:\"my notes\"").unwrap(); + assert_eq!(q.dates.len(), 3); + assert!(q.dates[0].contains("2025-04-01T10:00:00")); + assert!(!q.dates[0].contains("2025-07-01")); + assert!(q.dates[0].contains("2025-06-30")); + assert!(q.dates[1].contains("2023-01-01")); + assert!(!q.dates[1].contains("2025-01-01")); + assert!(q.dates[2].contains("2020-05-14T23:21:08+08:00")); + assert_eq!(q.files, vec!["my notes"]); + } + + #[test] + fn errors() { + assert!(Query::parse("tag:").is_err()); + assert!(Query::parse("-from:x").is_err()); + assert!(Query::parse("is:weird").is_err()); + // A colon in an ordinary word is fine. + assert_eq!(Query::parse("a:b").unwrap().words, "a:b"); + // A lone dash is a word. + assert_eq!(Query::parse("- x").unwrap().words, "- x"); + } + + #[test] + fn like_filters() { + let q = Query::parse("like:abc-1 -like:\"moduli of curves\" tag:x word").unwrap(); + assert_eq!(q.like, vec!["abc-1"]); + assert_eq!(q.unlike, vec!["moduli of curves"]); + assert_eq!(q.words, "word"); + } + + #[test] + fn ids_with_angle_brackets() { + let q = Query::parse("thread:<a.b@c> to:abc-1 is:inbox").unwrap(); + assert_eq!(q.thread, vec!["<a.b@c>"]); + assert_eq!(q.to, vec!["abc-1"]); + assert!(q.inbox); + } +} |
