From f5644690a4bc573217a303b766d794ff41fcca68 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sat, 4 Jan 2025 20:13:22 +0100 Subject: [PATCH] Simplify filter --- src/core/musichoard/filter.rs | 24 ++++++++---------------- src/main.rs | 8 ++++---- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/core/musichoard/filter.rs b/src/core/musichoard/filter.rs index b77afd9..203a95a 100644 --- a/src/core/musichoard/filter.rs +++ b/src/core/musichoard/filter.rs @@ -3,8 +3,8 @@ use crate::core::collection::album::{Album, AlbumOwnership, AlbumPrimaryType, Al /// Filter for a specifying subsets of the entire collection (e.g., for display). #[derive(Debug, Default)] pub struct CollectionFilter { - pub include: Vec>, - pub except: Vec>, + pub include: Vec, + pub except: Vec, } #[derive(Debug)] @@ -16,19 +16,11 @@ pub enum AlbumField { impl CollectionFilter { pub fn filter_album(&self, album: &Album) -> bool { - let include = Self::filter_and(&self.include, album); - let except = Self::filter_and(&self.except, album); + let include = Self::filter_or(&self.include, album); + let except = Self::filter_or(&self.except, album); include && !except } - fn filter_and(group: &Vec>, album: &Album) -> bool { - let mut filter = !group.is_empty(); - for group in group.iter() { - filter = filter && Self::filter_or(group, album); - } - filter - } - fn filter_or(group: &Vec, album: &Album) -> bool { let mut filter = false; for field in group.iter() { @@ -60,18 +52,18 @@ mod tests { fn test_filter() -> CollectionFilter { CollectionFilter { - include: vec![vec![ + include: vec![ AlbumField::PrimaryType(None), AlbumField::PrimaryType(Some(AlbumPrimaryType::Ep)), AlbumField::PrimaryType(Some(AlbumPrimaryType::Album)), - ]], - except: vec![vec![ + ], + except: vec![ AlbumField::Ownership(AlbumOwnership::None), AlbumField::SecondaryType(AlbumSecondaryType::Compilation), AlbumField::SecondaryType(AlbumSecondaryType::Soundtrack), AlbumField::SecondaryType(AlbumSecondaryType::Live), AlbumField::SecondaryType(AlbumSecondaryType::Demo), - ]], + ], } } diff --git a/src/main.rs b/src/main.rs index 2e32dc5..760a268 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,12 +73,12 @@ struct DbOpt { fn default_filter() -> CollectionFilter { CollectionFilter { - include: vec![vec![ + include: vec![ AlbumField::PrimaryType(None), AlbumField::PrimaryType(Some(AlbumPrimaryType::Ep)), AlbumField::PrimaryType(Some(AlbumPrimaryType::Album)), - ]], - except: vec![vec![ + ], + except: vec![ AlbumField::SecondaryType(AlbumSecondaryType::Compilation), AlbumField::SecondaryType(AlbumSecondaryType::Soundtrack), AlbumField::SecondaryType(AlbumSecondaryType::Spokenword), @@ -92,7 +92,7 @@ fn default_filter() -> CollectionFilter { AlbumField::SecondaryType(AlbumSecondaryType::Demo), AlbumField::SecondaryType(AlbumSecondaryType::FieldRecording), AlbumField::Ownership(AlbumOwnership::None), - ]], + ], } }