LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge main into feat/1449-people-search; people and quotes go through text_matches (#2099)
Arekatla Nishanth Chowdary3a6e7d0b9bc5
diff --cc crates/catalog/src/query.rsindex f98a083ef,7625c9ab6..a94421369--- a/crates/catalog/src/query.rs+++ b/crates/catalog/src/query.rs@@@ -135,46 -135,15 +135,46 @@@ fn shuffle_rank(seed: u64, id: PhotoIdmix64(mix64(seed) ^ id.0)}+ /// What [`Filter::prepare`] works out once for a pass over many photos.+ pub(crate) struct Prepared {+ root: Option<String>,+ only: Option<std::collections::HashSet<PhotoId>>,+ }++/// The tokens of a search text: split at whitespace, except inside double quotes, so+/// `person:"Jane Doe"` is one token (issue #1449). An unclosed quote runs to the end of the text; a+/// backslash is an ordinary character. One pass over the text, nothing allocated.+fn search_tokens(text: &str) -> impl Iterator<Item = &str> {+ let mut rest = text;+ std::iter::from_fn(move || {+ rest = rest.trim_start();+ if rest.is_empty() {+ return None;+ }+ let mut quoted = false;+ let end = rest+ .char_indices()+ .find(|&(_, c)| {+ quoted ^= c == '"';+ !quoted && c.is_whitespace()+ })+ .map_or(rest.len(), |(i, _)| i);+ let (tok, tail) = rest.split_at(end);+ rest = tail;+ Some(tok)+ })+}++/// `s` without the double quotes around it (a missing closing quote is allowed).+fn unquote(s: &str) -> &str {+ s.strip_prefix('"').map_or(s, |inner| inner.strip_suffix('"').unwrap_or(inner))+}+- /// What [`Filter::prepare`] works out once for a pass over many photos.- pub(crate) struct Prepared {- root: Option<String>,- only: Option<std::collections::HashSet<PhotoId>>,- }-fn token_matches(p: &Photo, tok: &str) -> bool {let t = tok.to_lowercase();- if let Some((field, val)) = t.split_once(':') {+ // a token that starts with a quote is a phrase, even with a colon in it- if let Some((field, val)) = t.split_once(':').filter(|_| !t.starts_with('"')) {- let val = unquote(val);++ if let Some((field, quoted)) = t.split_once(':').filter(|_| !t.starts_with('"')) {++ let val = unquote(quoted);let num = |s: &str| -> Option<(char, f64)> {let (op, rest) = match s.chars().next()? {c @ ('>' | '<' | '=') => (c, &s[1..]),@@@ -204,14 -180,18 +211,22 @@@"date" => p.captured.as_deref().is_some_and(|c| c.starts_with(val)),"copy" | "virtual" => (val == "true" || val == "yes") == p.copy_of.is_some(),"name" | "file" => p.file_name.to_lowercase().contains(val),- _ => false,- // Not a known field: the colon is part of a plain word ("10:30", "Re:").++ // Not a known field: the colon is part of a plain word ("10:30", "Re:"), its value unquoted++ // (`re:"a b"` is the text `re:a b`).++ _ if val.len() != quoted.len() => text_matches(p, &format!("{field}:{val}")),+ _ => text_matches(p, &t),};}- let t = unquote(&t);- text_matches(p, &t)++ text_matches(p, unquote(&t))+ }++ /// Whether the lower-cased word `t` is in the file name, title, caption, camera, lens, location,-/// format or a keyword of `p`.++/// format, a keyword or the name of a person (a named face) of `p`.+ fn text_matches(p: &Photo, t: &str) -> bool {let hay = [&p.file_name, &p.meta.title, &p.meta.caption, &p.meta.camera, &p.meta.lens, &p.meta.location, &p.format];- hay.iter().any(|h| h.to_lowercase().contains(t)) || p.meta.keywords.iter().any(|k| k.to_lowercase().contains(t))+ hay.iter().any(|h| h.to_lowercase().contains(t))+ || p.meta.keywords.iter().any(|k| k.to_lowercase().contains(t))+ || p.people().iter().any(|n| n.to_lowercase().contains(t))}/// Whether `p` has a named face region called `name` (case-insensitive).diff --cc crates/catalog/src/tests.rsindex 401d62020,a32b5acf5..f8b72dad8--- a/crates/catalog/src/tests.rs+++ b/crates/catalog/src/tests.rs@@@ -794,68 -836,6 +836,71 @@@ fn cmp_name_ignores_case_without_allocaassert_eq!(cmp_name("a", "ab"), Less);}+/// Search Photos finds named people in free text, and a quoted value keeps a name of several words+/// together: `person:"Jane Doe"` (issue #1449). Malformed quotes narrow the result or change nothing.+#[test]+fn search_text_finds_people_and_quoted_values() {+ use lightcraft_meta::{Rect, Region, RegionKind};+ let region = |name: &str, kind: RegionKind| Region {+ rect: Rect { x0: 0.4, y0: 0.4, x1: 0.6, y1: 0.6 },+ kind,+ name: Some(name.to_string()),+ description: None,+ };+ let mut c = Catalog::new();+ let mut add = |name: &str, regions: Vec<Region>, keywords: &[&str]| {+ let id = c.alloc_photo_id();+ let mut p = Photo::new(id, Source::Demo { scene: 1 }, name, "JPEG", 6000, 4000, "2026-09-30T10:00:00");+ p.meta.regions = regions;+ p.meta.keywords = keywords.iter().map(|k| k.to_string()).collect();+ c.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap();+ id+ };+ let jane = add("a.jpg", vec![region("Jane Doe", RegionKind::Face), region("Rex", RegionKind::Pet)], &[]);+ let both = add("b.jpg", vec![region("Jane Roe", RegionKind::Face), region("John Doe", RegionKind::Face)], &[]);+ let zoe = add("c.jpg", vec![region("Zoë Ångström", RegionKind::Face)], &["New York"]);- let none = add("d.jpg", vec![], &["york minster"]);++ let none = add("lunch 10:30 am.jpg", vec![], &["york minster"]);+ let q = |text: &str| {+ let mut ids = c.query(&Filter { text: text.into(), ..Default::default() }, &Sort::default());+ ids.sort();+ ids+ };++ // free text: a person's name, any case, any part of it; pets are not people+ assert_eq!(q("jane"), vec![jane, both]);+ assert_eq!(q("DOE"), vec![jane, both]);+ assert_eq!(q("ångström"), vec![zoe]);+ assert!(q("rex").is_empty(), "a pet is not a person");+ // words are still ANDed: b.jpg has a Jane and a Doe, a quoted phrase asks for them together+ assert_eq!(q("jane doe"), vec![jane, both]);+ assert_eq!(q("\"jane doe\""), vec![jane]);++ // fielded: the exact name, of several words when quoted+ assert_eq!(q("person:\"Jane Doe\""), vec![jane]);+ assert_eq!(q("who:\"jane roe\""), vec![both]);+ assert_eq!(q("person:\"Zoë Ångström\""), vec![zoe]);+ assert!(q("person:jane").is_empty(), "person: is the whole name, as before");+ assert!(q("person:Jane Doe").is_empty(), "unquoted, the second word is its own token, as before");+ assert_eq!(q("keyword:\"new york\""), vec![zoe], "other fields take quoted values too");+ assert_eq!(q("person:\"Jane Doe\" a.jpg"), vec![jane], "tokens after a quoted one still count");++ // malformed input never widens the match and never panics+ assert_eq!(q("person:\"Jane Doe"), vec![jane], "an unclosed quote runs to the end");+ assert!(q("person:\"Jane Doe a.jpg").is_empty(), "…and takes the rest of the text with it");+ assert!(q("person:\"\"").is_empty());+ assert!(q("person:").is_empty());+ assert_eq!(q("\""), vec![jane, both, zoe, none], "a lone quote is an empty phrase, like an empty search");+ assert_eq!(q("\"\" jane"), vec![jane, both]);+ assert!(q("\"jane:doe\"").is_empty(), "a quoted phrase is free text even with a colon");++ // an unknown field is plain text, its quoted value unquoted (#2099): `10:"30 am"` is the text `10:30 am`++ assert_eq!(q("10:\"30 am\""), vec![none]);++ assert!(q("10:\"30 pm\"").is_empty());+ assert!(q("jane\\\"").is_empty(), "a backslash is an ordinary character");+ let long = format!("person:\"{}", "é ".repeat(50_000));+ assert!(q(&long).is_empty());+ assert_eq!(q(" \t jane \u{3000} "), vec![jane, both], "any whitespace separates tokens");+}+/// What the catalog remembers of its latest ops: which photos each changed and what of them, as long as that can be/// said photo by photo.#[test]