From aaa1580c4998e898cc6c66d8362ac74045f8a6f6 Mon Sep 17 00:00:00 2001 From: Artur Meski Date: Sun, 22 Mar 2026 12:38:57 +0000 Subject: [PATCH] Genre priorities for album covers section --- src/db.rs | 6 +++--- src/report.rs | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/db.rs b/src/db.rs index c50f44c..63bf838 100644 --- a/src/db.rs +++ b/src/db.rs @@ -594,8 +594,8 @@ pub fn top_genres(conn: &Connection, period: &str, limit: i64) -> Result std::cmp::Ordering::Greater, (false, true) => std::cmp::Ordering::Less, @@ -743,7 +743,7 @@ pub fn uncached_albums(conn: &Connection) -> Result> { /// Single-word genres ("rock", "electronic", "ambient") are broad descriptors /// that appear as secondary tags on many artists. Multi-word genres ("prog /// rock", "alternative metal") are more specific and should rank first. -fn is_deprioritised(genre: &str) -> bool { +pub fn is_deprioritised_genre(genre: &str) -> bool { canonical_genre_key(genre).split_whitespace().count() < 2 } diff --git a/src/report.rs b/src/report.rs index 4f401b0..448f1cd 100644 --- a/src/report.rs +++ b/src/report.rs @@ -1539,16 +1539,23 @@ fn html_attr_escape(s: &str) -> String { html_escape(s).replace('"', """) } -/// Split a comma-separated genre list and keep only the first `max_labels` -/// cleaned labels. +/// Split a comma-separated genre list and return the top `max_labels` labels, +/// applying the same deprioritisation rule as `top_genres`: multi-word genres +/// rank before single-word broad descriptors ("rock", "electronic", etc.). fn top_genre_labels(genre_csv: &str, max_labels: usize) -> Vec { - genre_csv + let mut labels: Vec = genre_csv .split(',') .map(str::trim) .filter(|s| !s.is_empty()) - .take(max_labels) .map(|s| s.to_string()) - .collect() + .collect(); + + // Stable sort so that multi-word genres always precede single-word ones, + // preserving the original MusicBrainz order within each group. + labels.sort_by_key(|g| db::is_deprioritised_genre(g)); + + labels.truncate(max_labels); + labels } // --------------------------------------------------------------------------- @@ -1610,6 +1617,9 @@ mod tests { #[test] fn test_top_genre_labels_caps_to_three() { + // "alternative rock" and "industrial" are multi-word → rank first. + // "electronic" and "darkwave" are single-word → deprioritised. + // With limit 3 we get both multi-word genres + one single-word. let labels = top_genre_labels("alternative rock, electronic, industrial, darkwave", 3); assert_eq!( labels,