Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LRC-LIB-SMARTCOLL: loops through a smart album's album filter count too

Nathaniel Ledford1c5e51faf987
diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rs
index 15914442a..0e73e2587 100644
--- a/crates/catalog/src/lib.rs
+++ b/crates/catalog/src/lib.rs
@@ -341,13 +341,7 @@ impl Catalog {
if !seen.insert(a) {
continue;
}
- let tested = self
- .albums
- .get(&a)
- .and_then(|al| al.smart.as_ref())
- .and_then(|f| f.rule_set.as_ref())
- .map(rules::RuleSet::albums_tested)
- .unwrap_or_default();
+ let tested = self.albums.get(&a).and_then(|al| al.smart.as_deref()).map(Filter::albums_tested).unwrap_or_default();
if tested.contains(&to) {
return true;
}
@@ -360,9 +354,25 @@ impl Catalog {
/// `RuleSet::upgrade`): typically a rule testing an album that has since been deleted. Empty
/// for a sound smart album, a plain album or no album.
pub fn smart_album_problems(&self, id: AlbumId) -> Vec<rules::Problem> {
- let Some(mut rules) = self.albums.get(&id).and_then(|a| a.smart.as_ref()).and_then(|f| f.rule_set.clone()) else { return Vec::new() };
- rules.upgrade();
- rules.check_for(self, Some(id))
+ let Some(filter) = self.albums.get(&id).and_then(|a| a.smart.as_deref()) else { return Vec::new() };
+ let mut out: Vec<rules::Problem> = self.album_filter_problem(filter, Some(id)).into_iter().collect();
+ if let Some(mut rules) = filter.rule_set.clone() {
+ rules.upgrade();
+ out.extend(rules.check_for(self, Some(id)));
+ }
+ out
+ }
+
+ /// A loop through `filter`'s own album field (not its rules): smart album `owner` filtered to
+ /// itself, or to an album that leads back to it.
+ pub fn album_filter_problem(&self, filter: &Filter, owner: Option<AlbumId>) -> Option<rules::Problem> {
+ let (a, owner) = (filter.album?, owner?);
+ (a == owner || self.album_reaches(a, owner)).then(|| rules::Problem {
+ path: Vec::new(),
+ field: Some("album".into()),
+ issue: rules::Issue::AlbumLoop,
+ message: format!("its album filter (album {}) would make this album include itself", a.0),
+ })
}
/// The photos of an album: the stored list, or a smart album's current matches (id order).
diff --git a/crates/catalog/src/query.rs b/crates/catalog/src/query.rs
index 14a369507..d3c4e308d 100644
--- a/crates/catalog/src/query.rs
+++ b/crates/catalog/src/query.rs
@@ -221,6 +221,13 @@ impl Filter {
if v.is_empty() { "all photos".into() } else { v.join(", ") }
}
+ /// The albums this filter tests: its album field and its rules' Album rules.
+ pub fn albums_tested(&self) -> Vec<crate::AlbumId> {
+ let mut out: Vec<crate::AlbumId> = self.album.into_iter().collect();
+ out.extend(self.rule_set.as_ref().map(crate::RuleSet::albums_tested).unwrap_or_default());
+ out
+ }
+
/// Whether matches depend on the clock (only "in the last…" rules do).
pub fn depends_on_now(&self) -> bool {
self.rule_set.as_ref().is_some_and(crate::RuleSet::depends_on_now)
diff --git a/crates/catalog/src/rules.rs b/crates/catalog/src/rules.rs
index 1fda4b0bb..5af6de2d8 100644
--- a/crates/catalog/src/rules.rs
+++ b/crates/catalog/src/rules.rs
@@ -164,7 +164,8 @@ impl std::fmt::Display for Problem {
/// `rule 2.1: rating is 0–5, not 9` (counting from 1, as people do).
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let at: Vec<String> = self.path.iter().map(|i| (i.saturating_add(1)).to_string()).collect();
- write!(f, "rule {}: {}", at.join("."), self.message)
+ // a problem with the filter around the rules (its album field) has no rule to point at
+ if at.is_empty() { write!(f, "{}", self.message) } else { write!(f, "rule {}: {}", at.join("."), self.message) }
}
}
diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs
index 571958049..09cf98f4b 100644
--- a/crates/catalog/src/tests.rs
+++ b/crates/catalog/src/tests.rs
@@ -587,3 +587,25 @@ fn chains_of_smart_albums_stay_linear() {
assert_eq!(c.album_count(AlbumId(26)), 20);
assert!(start.elapsed() < std::time::Duration::from_secs(2), "took {:?}", start.elapsed());
}
+
+/// A smart album's own album filter (not only its rules) counts in loops: A filtered to "in A" is
+/// reported, and so is A filtered to B while B's rules test A.
+#[test]
+fn an_album_filter_counts_in_loops() {
+ let mut c = Catalog::new();
+ let smart = |id: u64, f: Filter| Album { smart: Some(Box::new(f)), ..Album::new(AlbumId(id), "S") };
+ c.apply(Op::AddAlbum { album: smart(1, Filter { album: Some(AlbumId(1)), ..Default::default() }) }).unwrap();
+ let loops = |c: &Catalog, id: u64| c.smart_album_problems(AlbumId(id)).iter().any(|p| p.issue == crate::rules::Issue::AlbumLoop);
+ assert!(loops(&c, 1), "filtered to itself");
+ let tests_3: Filter = serde_json::from_value(serde_json::json!({"ruleSet": {"rules": [{"field": "album", "op": "is", "value": 3}]}})).unwrap();
+ c.apply(Op::AddAlbum { album: smart(2, Filter { album: Some(AlbumId(3)), ..Default::default() }) }).unwrap();
+ c.apply(Op::AddAlbum {
+ album: smart(3, serde_json::from_value(serde_json::json!({"ruleSet": {"rules": [{"field": "album", "op": "is", "value": 2}]}})).unwrap()),
+ })
+ .unwrap();
+ assert!(c.album_reaches(AlbumId(3), AlbumId(2)) && c.album_reaches(AlbumId(2), AlbumId(3)));
+ assert!(loops(&c, 2) && loops(&c, 3));
+ let _ = tests_3;
+ let shown: Vec<String> = c.smart_album_problems(AlbumId(1)).iter().map(ToString::to_string).collect();
+ assert!(shown.iter().all(|s| !s.starts_with("rule :")), "{shown:?}");
+}
diff --git a/crates/engine/src/cmd/library.rs b/crates/engine/src/cmd/library.rs
index 50e8c1bbe..e18bb91e9 100644
--- a/crates/engine/src/cmd/library.rs
+++ b/crates/engine/src/cmd/library.rs
@@ -871,9 +871,10 @@ pub fn specs() -> Vec<CommandSpec> {
.as_ref()
.map(|r| r.check_for(&s.catalog, Some(id)))
.unwrap_or_default()
- .iter()
+ .into_iter()
+ .chain(s.catalog.album_filter_problem(&view, Some(id)))
.filter(|p| p.issue == lightcraft_catalog::rules::Issue::AlbumLoop)
- .map(ToString::to_string)
+ .map(|p| p.to_string())
.collect();
if !loops.is_empty() {
return Err(bad("album.setRules", loops.join("; ")));
@@ -1332,7 +1333,11 @@ fn merge_rules(
// only rules this change sets are checked: one that stopped checking since (its album was
// deleted) doesn't block editing the album's other settings
let sets_rules = patch.get("ruleSet").is_some_and(|r| !r.is_null());
- let problems = f.rule_set.as_ref().filter(|_| sets_rules).map(|r| r.check_for(cat, owner)).unwrap_or_default();
+ let mut problems = f.rule_set.as_ref().filter(|_| sets_rules).map(|r| r.check_for(cat, owner)).unwrap_or_default();
+ // and a loop through the album field the change sets
+ if patch.get("album").is_some_and(|a| !a.is_null()) {
+ problems.extend(cat.album_filter_problem(&f, owner));
+ }
if !problems.is_empty() {
return Err(bad(c, problems.iter().map(ToString::to_string).collect::<Vec<_>>().join("; ")));
}
diff --git a/crates/engine/src/tests_library.rs b/crates/engine/src/tests_library.rs
index 9475ae21e..fd07fa00e 100644
--- a/crates/engine/src/tests_library.rs
+++ b/crates/engine/src/tests_library.rs
@@ -660,3 +660,13 @@ fn a_stale_rule_doesnt_block_other_changes() {
assert!(s.execute("library.filter", &bad).is_err());
assert!(s.execute("album.setRules", &serde_json::json!({"id": smart, "rules": bad})).is_err());
}
+
+/// album.setRules refuses a loop through the album filter as well as through the rules.
+#[test]
+fn an_album_filter_loop_is_refused() {
+ let mut s = crate::Session::with_demo();
+ let a = s.execute("album.createSmart", &serde_json::json!({"name": "A", "rules": {"rating": 2}})).unwrap()["id"].as_u64().unwrap();
+ let r = s.execute("album.setRules", &serde_json::json!({"id": a, "rules": {"album": a}}));
+ assert!(r.is_err_and(|e| e.to_string().contains("include itself")), "refused");
+ assert_eq!(s.catalog.album(lightcraft_catalog::AlbumId(a)).unwrap().smart.as_ref().and_then(|f| f.album), None);
+}
diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs
index 4ed092e51..af6045e51 100644
--- a/crates/ui-egui/src/i18n.rs
+++ b/crates/ui-egui/src/i18n.rs
@@ -352,6 +352,10 @@ fn problem_text_in(language: Locale, problem: &lightcraft_catalog::rules::Proble
fn problem_line_in(language: Locale, problem: &lightcraft_catalog::rules::Problem) -> String {
let at: Vec<String> = problem.path.iter().map(|i| i.saturating_add(1).to_string()).collect();
+ // a problem with the album filter around the rules has no rule to point at
+ if at.is_empty() {
+ return problem_text_in(language, problem);
+ }
format!("#{} {}", at.join("."), problem_text_in(language, problem))
}
@@ -761,6 +765,10 @@ mod tests {
assert_eq!(problem_line_in(Locale::En, &p[0]), "#2 Title: needs something to look for (or use “is empty”)");
// Japanese and Chinese join with a full-width colon, as their catalogs do
assert_eq!(problem_text_in(Locale::Ja, &p[0]), "タイトル:検索する語句が必要です(または「が空」を使用)");
+ // a problem with the album filter itself has no rule position to show
+ let filter_loop =
+ lightcraft_catalog::rules::Problem { path: Vec::new(), field: Some("album".into()), issue: Issue::AlbumLoop, message: String::new() };
+ assert!(problem_line_in(Locale::En, &filter_loop).starts_with("Album: "), "{}", problem_line_in(Locale::En, &filter_loop));
let g = problems(serde_json::json!([{"group": {"rules": []}}]));
assert_eq!(problem_text_in(Locale::En, &g[0]), "This group is empty: add a rule or remove it.");
// the issues, the operators their hints name and the album picker's words, in every language