fond-store 1.0.0

SQLite persistence, migrations, and FTS5 search for fond recipe manager
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
use rusqlite::params;
use serde::Serialize;

use crate::db::FondDb;
use crate::error::StoreError;

/// A pantry item record from the database.
#[derive(Debug, Clone, Serialize)]
pub struct PantryItem {
    pub id: i64,
    pub name: String,
    pub present: bool,
    pub quantity: Option<String>,
    pub unit: Option<String>,
    pub expiry: Option<String>,
    pub par_level: Option<String>,
}

/// Per-ingredient coverage detail for `pantry check`.
#[derive(Debug, Clone, Serialize)]
pub struct IngredientCoverage {
    pub ingredient: String,
    pub matched: bool,
    pub matched_pantry_item: Option<String>,
    pub optional: bool,
}

/// Coverage result for a recipe's pantry check.
#[derive(Debug, Clone, Serialize)]
pub struct PantryCoverage {
    pub recipe_slug: String,
    pub recipe_title: String,
    pub total_ingredients: usize,
    pub matched_count: usize,
    pub missing_count: usize,
    pub coverage_pct: f64,
    pub ingredients: Vec<IngredientCoverage>,
}

/// Repository for pantry persistence operations.
pub struct PantryRepository<'a> {
    db: &'a FondDb,
}

impl<'a> PantryRepository<'a> {
    pub fn new(db: &'a FondDb) -> Self {
        Self { db }
    }

    /// Add items to the pantry (mark as present).
    ///
    /// If an item already exists, it is re-marked as present without
    /// overwriting optional metadata (quantity, unit, expiry, par_level).
    pub fn add_items(&self, names: &[&str]) -> Result<Vec<String>, StoreError> {
        let conn = self.db.conn();
        let mut added = Vec::new();

        for name in names {
            let trimmed = name.trim();
            if trimmed.is_empty() {
                continue;
            }

            conn.execute(
                "INSERT INTO pantry_items (name, present)
                 VALUES (?1, 1)
                 ON CONFLICT(name) DO UPDATE SET
                   present = 1,
                   updated_at = datetime('now')",
                params![trimmed],
            )?;

            added.push(trimmed.to_string());
        }

        Ok(added)
    }

    /// Remove items from the pantry (mark as absent, not delete).
    ///
    /// Preserves the row and any optional metadata for easy re-add.
    pub fn remove_items(&self, names: &[&str]) -> Result<Vec<String>, StoreError> {
        let conn = self.db.conn();
        let mut removed = Vec::new();

        for name in names {
            let trimmed = name.trim();
            if trimmed.is_empty() {
                continue;
            }

            let rows = conn.execute(
                "UPDATE pantry_items SET present = 0, updated_at = datetime('now')
                 WHERE name = ?1 COLLATE NOCASE AND present = 1",
                params![trimmed],
            )?;

            if rows > 0 {
                removed.push(trimmed.to_string());
            }
        }

        Ok(removed)
    }

    /// List pantry items.
    ///
    /// If `show_all` is false, only present items are returned.
    pub fn list_items(&self, show_all: bool) -> Result<Vec<PantryItem>, StoreError> {
        let conn = self.db.conn();

        let sql = if show_all {
            "SELECT id, name, present, quantity, unit, expiry, par_level
             FROM pantry_items ORDER BY name COLLATE NOCASE"
        } else {
            "SELECT id, name, present, quantity, unit, expiry, par_level
             FROM pantry_items WHERE present = 1 ORDER BY name COLLATE NOCASE"
        };

        let mut stmt = conn.prepare(sql)?;
        let items = stmt
            .query_map([], |row| {
                Ok(PantryItem {
                    id: row.get(0)?,
                    name: row.get(1)?,
                    present: row.get::<_, i32>(2)? != 0,
                    quantity: row.get(3)?,
                    unit: row.get(4)?,
                    expiry: row.get(5)?,
                    par_level: row.get(6)?,
                })
            })?
            .collect::<Result<Vec<_>, _>>()?;

        Ok(items)
    }

    /// Check pantry coverage for a recipe.
    ///
    /// Returns per-ingredient match status and overall coverage percentage.
    /// Uses bidirectional word-boundary matching for fuzzy ingredient matching.
    pub fn check_coverage(&self, recipe_slug: &str) -> Result<Option<PantryCoverage>, StoreError> {
        let conn = self.db.conn();

        // Get recipe info
        let recipe_row: Option<(i64, String)> = conn
            .query_row(
                "SELECT id, title FROM recipes WHERE slug = ?1",
                params![recipe_slug],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .ok();

        let Some((recipe_id, recipe_title)) = recipe_row else {
            return Ok(None);
        };

        // Get recipe ingredients
        let mut ing_stmt = conn.prepare(
            "SELECT name, optional FROM recipe_ingredients
             WHERE recipe_id = ?1 ORDER BY sort_order",
        )?;
        let recipe_ingredients: Vec<(String, bool)> = ing_stmt
            .query_map(params![recipe_id], |row| {
                Ok((row.get::<_, String>(0)?, row.get::<_, i32>(1)? != 0))
            })?
            .collect::<Result<Vec<_>, _>>()?;

        // Get present pantry items
        let mut pantry_stmt = conn.prepare("SELECT name FROM pantry_items WHERE present = 1")?;
        let pantry_names: Vec<String> = pantry_stmt
            .query_map([], |row| row.get::<_, String>(0))?
            .collect::<Result<Vec<_>, _>>()?;

        // Check each ingredient against the pantry
        let mut ingredients = Vec::new();
        let mut matched_count = 0;

        for (ing_name, optional) in &recipe_ingredients {
            let (matched, matched_item) = find_pantry_match(ing_name, &pantry_names);
            if matched {
                matched_count += 1;
            }
            ingredients.push(IngredientCoverage {
                ingredient: ing_name.clone(),
                matched,
                matched_pantry_item: matched_item,
                optional: *optional,
            });
        }

        let total = recipe_ingredients.len();
        let coverage_pct = if total > 0 {
            (matched_count as f64 / total as f64) * 100.0
        } else {
            100.0
        };

        Ok(Some(PantryCoverage {
            recipe_slug: recipe_slug.to_string(),
            recipe_title,
            total_ingredients: total,
            matched_count,
            missing_count: total - matched_count,
            coverage_pct,
            ingredients,
        }))
    }
}

/// Prep modifiers to strip from ingredient names before matching.
const PREP_MODIFIERS: &[&str] = &[
    "diced",
    "minced",
    "chopped",
    "sliced",
    "grated",
    "shredded",
    "crushed",
    "ground",
    "cubed",
    "halved",
    "quartered",
    "julienned",
    "peeled",
    "deveined",
    "trimmed",
    "pitted",
    "seeded",
    "cored",
    "melted",
    "softened",
    "frozen",
    "thawed",
    "dried",
    "fresh",
    "finely",
    "roughly",
    "thinly",
    "coarsely",
    "cut into strips",
    "cut into pieces",
    "cut into cubes",
    "to taste",
    "for garnish",
    "for serving",
    "large",
    "medium",
    "small",
    "whole",
];

/// Normalize an ingredient or pantry item name for fuzzy matching.
///
/// Lowercases, strips prep modifiers, collapses whitespace, trims.
pub(crate) fn normalize_for_matching(name: &str) -> String {
    let mut s = name.to_lowercase();

    // Remove common prep modifiers
    for modifier in PREP_MODIFIERS {
        // Replace modifier preceded/followed by word boundary or comma
        s = s.replace(modifier, " ");
    }

    // Remove punctuation that isn't useful for matching
    s = s
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c.is_whitespace() {
                c
            } else {
                ' '
            }
        })
        .collect();

    // Collapse whitespace and trim
    let words: Vec<&str> = s.split_whitespace().collect();
    words.join(" ")
}

/// Split a normalized name into words.
pub(crate) fn to_words(s: &str) -> Vec<&str> {
    s.split_whitespace().collect()
}

/// Check if all words of `phrase` appear in `text` as a contiguous subsequence
/// (word-boundary aware phrase matching).
pub(crate) fn phrase_matches(phrase_words: &[&str], text_words: &[&str]) -> bool {
    if phrase_words.is_empty() {
        return false;
    }
    if phrase_words.len() > text_words.len() {
        return false;
    }

    'outer: for start in 0..=(text_words.len() - phrase_words.len()) {
        for (i, pw) in phrase_words.iter().enumerate() {
            if text_words[start + i] != *pw {
                continue 'outer;
            }
        }
        return true;
    }
    false
}

/// Find the best pantry match for a recipe ingredient.
///
/// Uses bidirectional word-boundary phrase matching:
/// - Pantry "olive oil" matches recipe "extra-virgin olive oil" (pantry ⊂ recipe)
/// - Pantry "chicken thighs" matches recipe "chicken" (recipe ⊂ pantry)
/// - Exact match always wins
///
/// Returns (matched, Option<matched_pantry_item_name>).
pub(crate) fn find_pantry_match(
    ingredient_name: &str,
    pantry_names: &[String],
) -> (bool, Option<String>) {
    let norm_ing = normalize_for_matching(ingredient_name);
    let ing_words = to_words(&norm_ing);

    if ing_words.is_empty() {
        return (false, None);
    }

    // Try exact match first
    for pantry_name in pantry_names {
        let norm_pantry = normalize_for_matching(pantry_name);
        if norm_pantry == norm_ing {
            return (true, Some(pantry_name.clone()));
        }
    }

    // Try phrase matching (bidirectional)
    for pantry_name in pantry_names {
        let norm_pantry = normalize_for_matching(pantry_name);
        let pantry_words = to_words(&norm_pantry);

        if pantry_words.is_empty() {
            continue;
        }

        // pantry phrase appears in ingredient (e.g., "olive oil" in "extra-virgin olive oil")
        if phrase_matches(&pantry_words, &ing_words) {
            return (true, Some(pantry_name.clone()));
        }

        // ingredient phrase appears in pantry (e.g., "chicken" in "chicken thighs")
        if phrase_matches(&ing_words, &pantry_words) {
            return (true, Some(pantry_name.clone()));
        }
    }

    (false, None)
}

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

    // --- normalize_for_matching ---

    #[test]
    fn normalize_basic() {
        assert_eq!(normalize_for_matching("Olive Oil"), "olive oil");
    }

    #[test]
    fn normalize_strips_prep() {
        assert_eq!(normalize_for_matching("garlic, minced"), "garlic");
    }

    #[test]
    fn normalize_strips_multiple_prep() {
        assert_eq!(
            normalize_for_matching("chicken thighs, diced and trimmed"),
            "chicken thighs and"
        );
    }

    #[test]
    fn normalize_collapses_whitespace() {
        assert_eq!(
            normalize_for_matching("  extra   virgin   olive   oil  "),
            "extra virgin olive oil"
        );
    }

    // --- phrase_matches ---

    #[test]
    fn phrase_exact_match() {
        assert!(phrase_matches(&["olive", "oil"], &["olive", "oil"]));
    }

    #[test]
    fn phrase_subset_match() {
        assert!(phrase_matches(
            &["olive", "oil"],
            &["extra", "virgin", "olive", "oil"]
        ));
    }

    #[test]
    fn phrase_no_match() {
        assert!(!phrase_matches(&["olive", "oil"], &["coconut", "oil"]));
    }

    #[test]
    fn phrase_single_word() {
        assert!(phrase_matches(&["chicken"], &["chicken", "thighs"]));
    }

    #[test]
    fn phrase_empty() {
        assert!(!phrase_matches(&[], &["chicken"]));
    }

    // --- find_pantry_match ---

    #[test]
    fn match_exact() {
        let pantry = vec!["olive oil".to_string()];
        let (matched, item) = find_pantry_match("olive oil", &pantry);
        assert!(matched);
        assert_eq!(item.as_deref(), Some("olive oil"));
    }

    #[test]
    fn match_pantry_subset_of_ingredient() {
        let pantry = vec!["olive oil".to_string()];
        let (matched, item) = find_pantry_match("extra-virgin olive oil", &pantry);
        assert!(matched);
        assert_eq!(item.as_deref(), Some("olive oil"));
    }

    #[test]
    fn match_ingredient_subset_of_pantry() {
        let pantry = vec!["chicken thighs".to_string()];
        let (matched, _) = find_pantry_match("chicken", &pantry);
        assert!(matched);
    }

    #[test]
    fn no_match_unrelated() {
        let pantry = vec!["olive oil".to_string()];
        let (matched, _) = find_pantry_match("butter", &pantry);
        assert!(!matched);
    }

    #[test]
    fn no_false_positive_ham_graham() {
        // "ham" should NOT match "graham crackers" — word boundary matching
        let pantry = vec!["ham".to_string()];
        let (matched, _) = find_pantry_match("graham crackers", &pantry);
        assert!(!matched, "ham should not match graham crackers");
    }

    #[test]
    fn match_with_prep_modifiers() {
        let pantry = vec!["garlic".to_string()];
        let (matched, _) = find_pantry_match("garlic, minced", &pantry);
        assert!(matched);
    }

    #[test]
    fn match_case_insensitive() {
        let pantry = vec!["Flour".to_string()];
        let (matched, _) = find_pantry_match("all-purpose flour", &pantry);
        assert!(matched);
    }

    #[test]
    fn match_absent_item_not_in_list() {
        // Only present items are passed to find_pantry_match,
        // so this just tests empty pantry
        let pantry: Vec<String> = vec![];
        let (matched, _) = find_pantry_match("flour", &pantry);
        assert!(!matched);
    }
}