LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
LRC-LIB-SMARTCOLL: yes/no rule values read Yes / No and mean what they say
Nathaniel Ledford7d3c61c56b3b
diff --git a/crates/catalog/src/rules.rs b/crates/catalog/src/rules.rsindex 95c0c0ac3..6b430608c 100644--- a/crates/catalog/src/rules.rs+++ b/crates/catalog/src/rules.rs@@ -146,6 +146,31 @@ pub fn field_group(field: &str) -> Option<&'static str> {FIELD_GROUPS.iter().find(|g| g.1.contains(&field)).map(|g| g.0)}+/// What a yes/no rule's value means: `true`, `"yes"`, `1` or no value is yes; `false`, `"no"`, `0`+/// is no (strings in any case); `None` for anything else, which matches nothing.+pub fn bool_value(value: &Value) -> Option<bool> {+ match value {+ Value::Bool(b) => Some(*b),+ Value::Null => Some(true),+ Value::Number(n) => match n.as_u64() {+ Some(1) => Some(true),+ Some(0) => Some(false),+ _ => None,+ },+ Value::String(s) => match s.trim().to_lowercase().as_str() {+ "true" | "yes" | "1" => Some(true),+ "false" | "no" | "0" => Some(false),+ _ => None,+ },+ _ => None,+ }+}++/// The display label of a yes/no value.+pub fn bool_label(b: bool) -> &'static str {+ if b { "Yes" } else { "No" }+}+/// The display label of choice `id` of `field` ("Public Domain" for `publicDomain`); `None` when/// `field` isn't a choice field or has no such choice.pub fn choice_label(field: &str, id: &str) -> Option<&'static str> {@@ -377,6 +402,7 @@ impl Rule {let m = &p.meta;let want = lower(value);let text = |have: &str| text_op(op, have, &want);+ let yes = |have: bool| bool_value(value).is_some_and(|b| have == b);match field {"rating" => num_op(op, Some(p.rating as f64), value),"flag" => {@@ -390,12 +416,12 @@ impl Rule {(l == want || named) == (op == "is")}"kind" => (format!("{:?}", p.kind).to_lowercase() == want) == (op == "is"),- "edited" => p.is_edited() == value.as_bool().unwrap_or(true),- "hasGps" => m.gps.is_some() == value.as_bool().unwrap_or(true),- "virtualCopy" => p.copy_of.is_some() == value.as_bool().unwrap_or(true),+ "edited" => yes(p.is_edited()),+ "hasGps" => yes(m.gps.is_some()),+ "virtualCopy" => yes(p.copy_of.is_some()),"copyName" => text(p.copy_name.as_deref().unwrap_or("")),- "stacked" => cat.stack_of(p.id).is_some() == value.as_bool().unwrap_or(true),- "cropped" => p.is_cropped() == value.as_bool().unwrap_or(true),+ "stacked" => yes(cat.stack_of(p.id).is_some()),+ "cropped" => yes(p.is_cropped()),"treatment" => {let t = if p.develop.treatment == lightcraft_develop::Treatment::Bw { "monochrome" } else { "color" };(t == want) == (op == "is")@@ -471,7 +497,7 @@ impl Rule {"aspect" => (aspect(p.shown_size()) == Some(want.as_str())) == (op == "is"),"megapixels" => num_op(op, Some(p.width as f64 * p.height as f64 / 1e6), value),"sharpness" => num_op(op, p.analysis.map(|a| a.sharpness as f64), value),- "bestOfGroup" => p.analysis.is_some_and(|a| a.best || a.group.is_none()) == value.as_bool().unwrap_or(true),+ "bestOfGroup" => yes(p.analysis.is_some_and(|a| a.best || a.group.is_none())),"album" => {let id = number(value).map(|v| crate::AlbumId(v as u64));id.is_some_and(|a| cat.album(a).is_some_and(|al| !al.is_smart()) && cat.album_contains(a, p)) == (op == "is")@@ -508,9 +534,10 @@ impl RuleSet {for r in &self.rules {match r {Rule::Group { group } => out.extend(group.problems()),- Rule::Field { field, op, .. } => match field_kind(field) {+ Rule::Field { field, op, value } => match field_kind(field) {None => out.push(format!("unknown field `{field}`")),Some(k) if !ops_for(k).iter().any(|o| o.0 == op) => out.push(format!("`{field}` has no operator `{op}`")),+ Some(Kind::Bool) if bool_value(value).is_none() => out.push(format!("`{field}` is yes or no, not {value}")),_ => {}},}@@ -534,6 +561,9 @@ impl RuleSet {let label = field_label(field).unwrap_or(field).to_lowercase();let op = field_kind(field).and_then(|k| ops_for(k).iter().find(|o| o.0 == op)).map_or(op.as_str(), |o| o.1);let v = match value {+ _ if field_kind(field) == Some(Kind::Bool) => {+ bool_value(value).map_or_else(|| value.to_string(), |b| bool_label(b).to_lowercase())+ }Value::String(s) => choice_label(field, s).map_or_else(|| s.clone(), str::to_lowercase),Value::Null => String::new(),Value::Object(o) => format!(@@ -872,6 +902,35 @@ mod tests {assert!(r.matches(&p, &Catalog::new()), "ids still match");}+ /// Yes/no fields read "Yes" / "No", and a value means the same to matching and to every+ /// summary: `false`, `"false"`, `"no"` and `0` are no; `true`, `"yes"`, `1` and a missing value+ /// are yes. Anything else is reported, not silently taken as yes.+ #[test]+ fn yes_no_values() {+ for v in [json!(true), json!("true"), json!("Yes"), json!(1), json!(null)] {+ assert_eq!(bool_value(&v), Some(true), "{v}");+ }+ for v in [json!(false), json!("false"), json!(" NO "), json!(0)] {+ assert_eq!(bool_value(&v), Some(false), "{v}");+ }+ for v in [json!("maybe"), json!(2), json!([true]), json!({"x": 1})] {+ assert_eq!(bool_value(&v), None, "{v}");+ }+ assert_eq!((bool_label(true), bool_label(false)), ("Yes", "No"));+ let cat = Catalog::new();+ let p = photo(1); // unedited+ assert!(rs(json!({"rules": [{"field": "edited", "op": "is", "value": "false"}]})).matches(&p, &cat), "\"false\" means no");+ assert!(+ !rs(json!({"rules": [{"field": "edited", "op": "is", "value": "false"}, {"field": "hasGps", "op": "is", "value": "yes"}]}))+ .matches(&p, &cat)+ );+ assert!(!rs(json!({"rules": [{"field": "edited", "op": "is", "value": "maybe"}]})).matches(&p, &cat), "an unreadable value matches nothing");+ let r = rs(json!({"rules": [{"field": "edited", "op": "is", "value": true}, {"field": "cropped", "op": "is", "value": "false"}]}));+ assert_eq!(r.describe(), "has edits is yes and cropped is no");+ let bad = rs(json!({"rules": [{"field": "edited", "op": "is", "value": "maybe"}]}));+ assert_eq!(bad.problems(), vec!["`edited` is yes or no, not \"maybe\"".to_string()]);+ }+/// The field menu shows every rule field once: at the top level or in exactly one group,/// and [`FIELDS`] lists them in menu order.#[test]diff --git a/crates/engine/src/tests_library.rs b/crates/engine/src/tests_library.rsindex f3650803d..7719193ee 100644--- a/crates/engine/src/tests_library.rs+++ b/crates/engine/src/tests_library.rs@@ -265,6 +265,20 @@ fn smart_album_rule_sets() {&serde_json::json!({"name": "Bad", "rules": {"ruleSet": {"rules": [{"field": "rating", "op": "contains", "value": 1}]}}}),);assert!(bad.is_err(), "operators are checked");+ // yes/no values: "false" means no, anything unreadable is an error rather than a silent yes+ let maybe = s.execute(+ "album.createSmart",+ &serde_json::json!({"name": "Maybe", "rules": {"ruleSet": {"rules": [{"field": "edited", "op": "is", "value": "maybe"}]}}}),+ );+ assert!(maybe.is_err_and(|e| e.to_string().contains("yes or no")), "unreadable yes/no value");+ let r = s+ .execute(+ "album.createSmart",+ &serde_json::json!({"name": "Unedited", "rules": {"ruleSet": {"rules": [{"field": "edited", "op": "is", "value": "false"}]}}}),+ )+ .unwrap();+ let unedited = s.catalog.photos().filter(|p| !p.deleted && !p.is_edited()).count();+ assert_eq!(r["count"].as_u64(), Some(unedited as u64));let r = s.execute("album.createSmart",diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rsindex 2d869ecd8..0f08e244b 100644--- a/crates/ui-egui/src/i18n.rs+++ b/crates/ui-egui/src/i18n.rs@@ -328,6 +328,15 @@ fn choice_text_in<'a>(language: Locale, field: &str, id: &'a str) -> &'a str {lightcraft_catalog::rules::choice_label(field, id).map_or(id, |label| tr_in(language, label))}+/// What people read for a smart-album yes/no value ("Ja" / "Nein" in German).+pub fn bool_text(b: bool) -> &'static str {+ bool_text_in(language(), b)+}++fn bool_text_in(language: Locale, b: bool) -> &'static str {+ tr_in(language, lightcraft_catalog::rules::bool_label(b))+}+/// A rule summary for display, keeping free-text rule values verbatim.pub fn rules_label(rules: &lightcraft_catalog::RuleSet) -> String {fn describe(rules: &lightcraft_catalog::RuleSet, depth: usize) -> String {@@ -348,7 +357,7 @@ pub fn rules_label(rules: &lightcraft_catalog::RuleSet) -> String {let operator = ops_for(*kind).iter().find(|entry| entry.0 == op).map_or(op.as_str(), |entry| entry.1);let text = match kind {Kind::Choice(_) => value.as_str().map_or_else(|| value.to_string(), |id| choice_text(field, id).to_string()),- Kind::Bool => tr(if value.as_bool() == Some(true) { "true" } else { "false" }).to_string(),+ Kind::Bool => lightcraft_catalog::rules::bool_value(value).map_or_else(|| value.to_string(), |b| bool_text(b).to_string()),_ if matches!(op.as_str(), "inLast" | "notInLast") => format!("{} {}",value.get("n").unwrap_or(&serde_json::Value::Null),@@ -649,6 +658,14 @@ mod tests {assert_eq!(choice_text_in(Locale::ZhHans, "treatment", "color"), "彩色", "in colour, not the colour noun");assert_eq!(choice_text_in(Locale::Ru, "treatment", "color"), "Цветное");assert_eq!(choice_text_in(Locale::Es, "label", "none"), "Sin etiqueta");+ // yes/no fields read Yes / No, never true / false+ assert_eq!((bool_text_in(Locale::En, true), bool_text_in(Locale::En, false)), ("Yes", "No"));+ assert_eq!(bool_text_in(Locale::De, false), "Nein");+ for language in Locale::ALL.iter().filter(|language| **language != Locale::En) {+ for label in [lightcraft_catalog::rules::bool_label(true), lightcraft_catalog::rules::bool_label(false)] {+ assert!(language.catalog().contains_key(label), "{} lacks {label:?}", language.code());+ }+ }for language in Locale::ALL {for (field, _, kind) in FIELDS {let Kind::Choice(choices) = kind else { continue };@@ -750,6 +767,13 @@ mod tests {}],};assert!(rules_label(&rules).ends_with(" Gemeinfrei"), "{}", rules_label(&rules));+ for value in [serde_json::json!(false), serde_json::json!("false")] {+ let rules = lightcraft_catalog::RuleSet {+ mode: lightcraft_catalog::Match::All,+ rules: vec![lightcraft_catalog::Rule::Field { field: "edited".into(), op: "is".into(), value }],+ };+ assert!(rules_label(&rules).ends_with(" Nein"), "{}", rules_label(&rules));+ }let filter = lightcraft_catalog::Filter {labels: vec![lightcraft_catalog::ColorLabel::Red, lightcraft_catalog::ColorLabel::Blue],..Default::default()diff --git a/crates/ui-egui/src/panels/rules_editor.rs b/crates/ui-egui/src/panels/rules_editor.rsindex 697f71253..1ee1872db 100644--- a/crates/ui-egui/src/panels/rules_editor.rs+++ b/crates/ui-egui/src/panels/rules_editor.rs@@ -2,7 +2,7 @@//! value), with nested groups. Field and operator lists come from `lightcraft_catalog::rules`.use egui::RichText;-use lightcraft_catalog::rules::{FIELD_GROUPS, Kind, TOP_LEVEL_FIELDS, field_kind, field_label, ops_for};+use lightcraft_catalog::rules::{FIELD_GROUPS, Kind, TOP_LEVEL_FIELDS, bool_value, field_kind, field_label, ops_for};use lightcraft_catalog::{Match, Rule, RuleSet};use serde_json::{Value, json};@@ -131,14 +131,12 @@ fn value_editor(ui: &mut egui::Ui, field: &str, op: &str, v: &mut Value, salt: &);}(Some(Kind::Bool), _) => {- let mut b = v.as_bool().unwrap_or(true);- egui::ComboBox::from_id_salt(format!("{salt}-bool"))- .width(60.0)- .selected_text(crate::i18n::tr(if b { "true" } else { "false" }))- .show_ui(ui, |ui| {- ui.selectable_value(&mut b, true, crate::i18n::tr("true"));- ui.selectable_value(&mut b, false, crate::i18n::tr("false"));- });+ // the catalog's reading of the value ("false" is no), stored back as a plain boolean+ let mut b = bool_value(v).unwrap_or(true);+ egui::ComboBox::from_id_salt(format!("{salt}-bool")).width(60.0).selected_text(crate::i18n::bool_text(b)).show_ui(ui, |ui| {+ ui.selectable_value(&mut b, true, crate::i18n::bool_text(true));+ ui.selectable_value(&mut b, false, crate::i18n::bool_text(false));+ });*v = json!(b);}(Some(Kind::Date), _) => text_value(ui, v, 120.0, "2026-04 or 2026-04-12", salt),