ankit 0.1.0

Complete async Rust client for the AnkiConnect API
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//! Type-safe query builder for Anki search syntax.
//!
//! This module provides a fluent API for constructing Anki search queries,
//! replacing error-prone string concatenation with compile-time checked methods.
//!
//! # Example
//!
//! ```
//! use ankit::QueryBuilder;
//!
//! // Build a query for due cards in a deck, excluding suspended
//! let query = QueryBuilder::new()
//!     .deck("Japanese")
//!     .is_due()
//!     .not_suspended()
//!     .build();
//!
//! assert_eq!(query, "deck:Japanese is:due -is:suspended");
//! ```
//!
//! # Complex Queries
//!
//! ```
//! use ankit::QueryBuilder;
//!
//! // Find leeches (high lapses) with low ease
//! let query = QueryBuilder::new()
//!     .deck("Vocabulary")
//!     .lapses_gte(5)
//!     .ease_lt(2.1)
//!     .not_suspended()
//!     .build();
//!
//! // Search in specific fields
//! let query = QueryBuilder::new()
//!     .field("Front", "mangiare")
//!     .build();
//!
//! // OR conditions
//! let query = QueryBuilder::new()
//!     .deck("Italian")
//!     .or(|q| q.tag("verb").tag("noun"))
//!     .build();
//! ```

/// A builder for constructing Anki search queries.
///
/// Provides a type-safe, fluent API for building queries instead of
/// manually constructing query strings.
#[derive(Debug, Clone, Default)]
#[must_use = "QueryBuilder does nothing until .build() is called"]
pub struct QueryBuilder {
    parts: Vec<String>,
}

impl QueryBuilder {
    /// Create a new empty query builder.
    pub fn new() -> Self {
        Self { parts: Vec::new() }
    }

    // ========================================================================
    // Location
    // ========================================================================

    /// Filter by deck name.
    ///
    /// Supports hierarchical decks with `::` separator.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().deck("Japanese").build();
    /// assert_eq!(q, "deck:Japanese");
    ///
    /// let q = QueryBuilder::new().deck("Languages::Italian").build();
    /// assert_eq!(q, "deck:Languages::Italian");
    ///
    /// // Spaces are quoted automatically
    /// let q = QueryBuilder::new().deck("My Deck").build();
    /// assert_eq!(q, "deck:\"My Deck\"");
    /// ```
    pub fn deck(mut self, name: &str) -> Self {
        self.parts.push(format!("deck:{}", quote_if_needed(name)));
        self
    }

    /// Filter by note type (model) name.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().note_type("Basic").build();
    /// assert_eq!(q, "note:Basic");
    /// ```
    pub fn note_type(mut self, model: &str) -> Self {
        self.parts.push(format!("note:{}", quote_if_needed(model)));
        self
    }

    /// Filter by card template ordinal (1-indexed).
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().card_template(2).build();
    /// assert_eq!(q, "card:2");
    /// ```
    pub fn card_template(mut self, ordinal: i32) -> Self {
        self.parts.push(format!("card:{}", ordinal));
        self
    }

    // ========================================================================
    // Card State
    // ========================================================================

    /// Filter for cards that are due for review.
    pub fn is_due(mut self) -> Self {
        self.parts.push("is:due".to_string());
        self
    }

    /// Filter for new cards (never reviewed).
    pub fn is_new(mut self) -> Self {
        self.parts.push("is:new".to_string());
        self
    }

    /// Filter for review cards.
    pub fn is_review(mut self) -> Self {
        self.parts.push("is:review".to_string());
        self
    }

    /// Filter for cards in learning phase.
    pub fn is_learn(mut self) -> Self {
        self.parts.push("is:learn".to_string());
        self
    }

    /// Filter for suspended cards.
    pub fn is_suspended(mut self) -> Self {
        self.parts.push("is:suspended".to_string());
        self
    }

    /// Filter for buried cards.
    pub fn is_buried(mut self) -> Self {
        self.parts.push("is:buried".to_string());
        self
    }

    /// Exclude suspended cards.
    pub fn not_suspended(mut self) -> Self {
        self.parts.push("-is:suspended".to_string());
        self
    }

    /// Exclude buried cards.
    pub fn not_buried(mut self) -> Self {
        self.parts.push("-is:buried".to_string());
        self
    }

    // ========================================================================
    // Tags
    // ========================================================================

    /// Filter by tag.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().tag("vocabulary").build();
    /// assert_eq!(q, "tag:vocabulary");
    /// ```
    pub fn tag(mut self, tag: &str) -> Self {
        self.parts.push(format!("tag:{}", quote_if_needed(tag)));
        self
    }

    /// Exclude notes with a specific tag.
    pub fn without_tag(mut self, tag: &str) -> Self {
        self.parts.push(format!("-tag:{}", quote_if_needed(tag)));
        self
    }

    /// Filter for notes without any tags.
    pub fn untagged(mut self) -> Self {
        self.parts.push("tag:none".to_string());
        self
    }

    // ========================================================================
    // Properties
    // ========================================================================

    /// Filter for cards with interval greater than N days.
    pub fn interval_gt(mut self, days: i32) -> Self {
        self.parts.push(format!("prop:ivl>{}", days));
        self
    }

    /// Filter for cards with interval less than N days.
    pub fn interval_lt(mut self, days: i32) -> Self {
        self.parts.push(format!("prop:ivl<{}", days));
        self
    }

    /// Filter for cards with interval equal to N days.
    pub fn interval_eq(mut self, days: i32) -> Self {
        self.parts.push(format!("prop:ivl={}", days));
        self
    }

    /// Filter for cards with ease factor greater than value.
    ///
    /// Ease is expressed as a decimal (e.g., 2.5 = 250%).
    pub fn ease_gt(mut self, ease: f32) -> Self {
        self.parts.push(format!("prop:ease>{:.2}", ease));
        self
    }

    /// Filter for cards with ease factor less than value.
    ///
    /// Ease is expressed as a decimal (e.g., 2.5 = 250%).
    pub fn ease_lt(mut self, ease: f32) -> Self {
        self.parts.push(format!("prop:ease<{:.2}", ease));
        self
    }

    /// Filter for cards with lapses greater than or equal to N.
    ///
    /// Useful for finding leeches.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// // Find potential leeches
    /// let q = QueryBuilder::new().lapses_gte(8).build();
    /// assert_eq!(q, "prop:lapses>=8");
    /// ```
    pub fn lapses_gte(mut self, n: i32) -> Self {
        self.parts.push(format!("prop:lapses>={}", n));
        self
    }

    /// Filter for cards with exactly N lapses.
    pub fn lapses_eq(mut self, n: i32) -> Self {
        self.parts.push(format!("prop:lapses={}", n));
        self
    }

    /// Filter for cards with reps greater than or equal to N.
    pub fn reps_gte(mut self, n: i32) -> Self {
        self.parts.push(format!("prop:reps>={}", n));
        self
    }

    /// Filter for cards due within N days.
    ///
    /// Use 0 for due today, negative for overdue.
    pub fn due_in_days(mut self, days: i32) -> Self {
        self.parts.push(format!("prop:due={}", days));
        self
    }

    /// Filter for cards due before N days from now.
    pub fn due_before_days(mut self, days: i32) -> Self {
        self.parts.push(format!("prop:due<{}", days));
        self
    }

    // ========================================================================
    // Time-based
    // ========================================================================

    /// Filter for cards added within the last N days.
    pub fn added_within_days(mut self, days: i32) -> Self {
        self.parts.push(format!("added:{}", days));
        self
    }

    /// Filter for cards reviewed/rated within the last N days.
    pub fn rated_within_days(mut self, days: i32) -> Self {
        self.parts.push(format!("rated:{}", days));
        self
    }

    /// Filter for cards edited within the last N days.
    pub fn edited_within_days(mut self, days: i32) -> Self {
        self.parts.push(format!("edited:{}", days));
        self
    }

    /// Filter for cards first reviewed within the last N days.
    pub fn introduced_within_days(mut self, days: i32) -> Self {
        self.parts.push(format!("introduced:{}", days));
        self
    }

    // ========================================================================
    // Content Search
    // ========================================================================

    /// Search for text in any field.
    ///
    /// The text is matched as an exact phrase.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().contains("to eat").build();
    /// assert_eq!(q, "\"to eat\"");
    /// ```
    pub fn contains(mut self, text: &str) -> Self {
        self.parts.push(format!("\"{}\"", escape_quotes(text)));
        self
    }

    /// Search for a word in any field.
    ///
    /// Unlike `contains`, this matches word boundaries.
    pub fn word(mut self, word: &str) -> Self {
        self.parts.push(quote_if_needed(word));
        self
    }

    /// Search for text in a specific field.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().field("Front", "hello").build();
    /// assert_eq!(q, "Front:hello");
    ///
    /// let q = QueryBuilder::new().field("Front", "hello world").build();
    /// assert_eq!(q, "Front:\"hello world\"");
    /// ```
    pub fn field(mut self, field_name: &str, text: &str) -> Self {
        self.parts
            .push(format!("{}:{}", field_name, quote_if_needed(text)));
        self
    }

    /// Search with regex in a specific field.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().field_regex("Front", r"^to\s+").build();
    /// assert_eq!(q, r"Front:re:^to\s+");
    /// ```
    pub fn field_regex(mut self, field_name: &str, pattern: &str) -> Self {
        self.parts.push(format!("{}:re:{}", field_name, pattern));
        self
    }

    /// Search with wildcard in a specific field.
    ///
    /// Use `*` for any sequence, `_` for single character.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().field_wildcard("Front", "*tion").build();
    /// assert_eq!(q, "Front:*tion");
    /// ```
    pub fn field_wildcard(mut self, field_name: &str, pattern: &str) -> Self {
        self.parts.push(format!("{}:{}", field_name, pattern));
        self
    }

    /// Filter for notes where a field is empty.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new().field_empty("Example").build();
    /// assert_eq!(q, "Example:");
    /// ```
    pub fn field_empty(mut self, field_name: &str) -> Self {
        self.parts.push(format!("{}:", field_name));
        self
    }

    // ========================================================================
    // Flags
    // ========================================================================

    /// Filter by flag color.
    ///
    /// Flag values: 0 (no flag), 1 (red), 2 (orange), 3 (green), 4 (blue), 5 (pink), 6 (turquoise), 7 (purple).
    pub fn flag(mut self, flag: i32) -> Self {
        self.parts.push(format!("flag:{}", flag));
        self
    }

    /// Filter for cards with any flag.
    pub fn has_flag(mut self) -> Self {
        self.parts.push("-flag:0".to_string());
        self
    }

    /// Filter for cards without any flag.
    pub fn no_flag(mut self) -> Self {
        self.parts.push("flag:0".to_string());
        self
    }

    // ========================================================================
    // Combinators
    // ========================================================================

    /// Combine conditions with OR.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new()
    ///     .deck("Italian")
    ///     .or(|q| q.tag("verb").tag("noun"))
    ///     .build();
    ///
    /// assert_eq!(q, "deck:Italian (tag:verb OR tag:noun)");
    /// ```
    pub fn or<F>(mut self, f: F) -> Self
    where
        F: FnOnce(OrBuilder) -> OrBuilder,
    {
        let or_builder = f(OrBuilder::new());
        let or_query = or_builder.build();
        if !or_query.is_empty() {
            self.parts.push(format!("({})", or_query));
        }
        self
    }

    /// Negate a condition.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new()
    ///     .deck("Test")
    ///     .not(|q| q.tag("exclude"))
    ///     .build();
    ///
    /// assert_eq!(q, "deck:Test -tag:exclude");
    /// ```
    pub fn not<F>(mut self, f: F) -> Self
    where
        F: FnOnce(QueryBuilder) -> QueryBuilder,
    {
        let inner = f(QueryBuilder::new());
        for part in inner.parts {
            if let Some(stripped) = part.strip_prefix('-') {
                // Double negative - remove the dash
                self.parts.push(stripped.to_string());
            } else {
                self.parts.push(format!("-{}", part));
            }
        }
        self
    }

    /// Add a raw query string.
    ///
    /// Use this as an escape hatch for query syntax not covered by the builder.
    ///
    /// # Example
    ///
    /// ```
    /// use ankit::QueryBuilder;
    ///
    /// let q = QueryBuilder::new()
    ///     .deck("Test")
    ///     .raw("prop:pos>5")
    ///     .build();
    ///
    /// assert_eq!(q, "deck:Test prop:pos>5");
    /// ```
    pub fn raw(mut self, query: &str) -> Self {
        self.parts.push(query.to_string());
        self
    }

    // ========================================================================
    // Terminal
    // ========================================================================

    /// Build the final query string.
    pub fn build(self) -> String {
        self.parts.join(" ")
    }
}

/// Builder for OR conditions.
#[derive(Debug, Clone, Default)]
pub struct OrBuilder {
    parts: Vec<String>,
}

impl OrBuilder {
    fn new() -> Self {
        Self { parts: Vec::new() }
    }

    /// Add a tag to the OR group.
    pub fn tag(mut self, tag: &str) -> Self {
        self.parts.push(format!("tag:{}", quote_if_needed(tag)));
        self
    }

    /// Add a deck to the OR group.
    pub fn deck(mut self, name: &str) -> Self {
        self.parts.push(format!("deck:{}", quote_if_needed(name)));
        self
    }

    /// Add a note type to the OR group.
    pub fn note_type(mut self, model: &str) -> Self {
        self.parts.push(format!("note:{}", quote_if_needed(model)));
        self
    }

    /// Add a field search to the OR group.
    pub fn field(mut self, field_name: &str, text: &str) -> Self {
        self.parts
            .push(format!("{}:{}", field_name, quote_if_needed(text)));
        self
    }

    /// Add a raw query to the OR group.
    pub fn raw(mut self, query: &str) -> Self {
        self.parts.push(query.to_string());
        self
    }

    /// Add is:new to the OR group.
    pub fn is_new(mut self) -> Self {
        self.parts.push("is:new".to_string());
        self
    }

    /// Add is:due to the OR group.
    pub fn is_due(mut self) -> Self {
        self.parts.push("is:due".to_string());
        self
    }

    /// Add is:review to the OR group.
    pub fn is_review(mut self) -> Self {
        self.parts.push("is:review".to_string());
        self
    }

    /// Add is:learn to the OR group.
    pub fn is_learn(mut self) -> Self {
        self.parts.push("is:learn".to_string());
        self
    }

    fn build(self) -> String {
        self.parts.join(" OR ")
    }
}

/// Quote a value if it contains spaces or special characters.
fn quote_if_needed(s: &str) -> String {
    if s.contains(' ') || s.contains('"') || s.contains('(') || s.contains(')') {
        format!("\"{}\"", escape_quotes(s))
    } else {
        s.to_string()
    }
}

/// Escape double quotes in a string.
fn escape_quotes(s: &str) -> String {
    s.replace('"', "\\\"")
}

impl std::fmt::Display for QueryBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.parts.join(" "))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_query() {
        let q = QueryBuilder::new().build();
        assert_eq!(q, "");
    }

    #[test]
    fn test_deck() {
        let q = QueryBuilder::new().deck("Japanese").build();
        assert_eq!(q, "deck:Japanese");
    }

    #[test]
    fn test_deck_with_spaces() {
        let q = QueryBuilder::new().deck("My Deck").build();
        assert_eq!(q, "deck:\"My Deck\"");
    }

    #[test]
    fn test_hierarchical_deck() {
        let q = QueryBuilder::new()
            .deck("Languages::Italian::Verbs")
            .build();
        assert_eq!(q, "deck:Languages::Italian::Verbs");
    }

    #[test]
    fn test_card_states() {
        let q = QueryBuilder::new().is_due().is_new().build();
        assert_eq!(q, "is:due is:new");

        let q = QueryBuilder::new().not_suspended().not_buried().build();
        assert_eq!(q, "-is:suspended -is:buried");
    }

    #[test]
    fn test_tags() {
        let q = QueryBuilder::new().tag("vocab").without_tag("hard").build();
        assert_eq!(q, "tag:vocab -tag:hard");
    }

    #[test]
    fn test_properties() {
        let q = QueryBuilder::new()
            .lapses_gte(5)
            .ease_lt(2.1)
            .interval_gt(30)
            .build();
        assert_eq!(q, "prop:lapses>=5 prop:ease<2.10 prop:ivl>30");
    }

    #[test]
    fn test_time_filters() {
        let q = QueryBuilder::new()
            .added_within_days(7)
            .rated_within_days(1)
            .build();
        assert_eq!(q, "added:7 rated:1");
    }

    #[test]
    fn test_content_search() {
        let q = QueryBuilder::new().contains("to eat").build();
        assert_eq!(q, "\"to eat\"");

        let q = QueryBuilder::new().field("Front", "hello").build();
        assert_eq!(q, "Front:hello");

        let q = QueryBuilder::new().field("Front", "hello world").build();
        assert_eq!(q, "Front:\"hello world\"");
    }

    #[test]
    fn test_field_regex() {
        let q = QueryBuilder::new().field_regex("Front", r"^to\s+").build();
        assert_eq!(q, r"Front:re:^to\s+");
    }

    #[test]
    fn test_field_empty() {
        let q = QueryBuilder::new().field_empty("Example").build();
        assert_eq!(q, "Example:");
    }

    #[test]
    fn test_or_combinator() {
        let q = QueryBuilder::new()
            .deck("Italian")
            .or(|q| q.tag("verb").tag("noun"))
            .build();
        assert_eq!(q, "deck:Italian (tag:verb OR tag:noun)");
    }

    #[test]
    fn test_not_combinator() {
        let q = QueryBuilder::new()
            .deck("Test")
            .not(|q| q.tag("exclude"))
            .build();
        assert_eq!(q, "deck:Test -tag:exclude");
    }

    #[test]
    fn test_complex_query() {
        let q = QueryBuilder::new()
            .deck("Japanese")
            .is_due()
            .not_suspended()
            .lapses_gte(3)
            .or(|q| q.tag("verb").tag("noun").tag("adjective"))
            .build();
        assert_eq!(
            q,
            "deck:Japanese is:due -is:suspended prop:lapses>=3 (tag:verb OR tag:noun OR tag:adjective)"
        );
    }

    #[test]
    fn test_raw_escape_hatch() {
        let q = QueryBuilder::new().deck("Test").raw("prop:pos>5").build();
        assert_eq!(q, "deck:Test prop:pos>5");
    }

    #[test]
    fn test_display() {
        let q = QueryBuilder::new().deck("Test").is_due();
        assert_eq!(format!("{}", q), "deck:Test is:due");
    }

    #[test]
    fn test_flags() {
        let q = QueryBuilder::new().flag(1).build();
        assert_eq!(q, "flag:1");

        let q = QueryBuilder::new().has_flag().build();
        assert_eq!(q, "-flag:0");

        let q = QueryBuilder::new().no_flag().build();
        assert_eq!(q, "flag:0");
    }
}