aboutsummaryrefslogtreecommitdiff
path: root/src/query.rs
blob: 2b53ae64b4e6726889d6a2a5f0aef5e4a22d79b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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 &note.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(&note.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);
    }
}