Simplify filter
This commit is contained in:
parent
68d50ad986
commit
f5644690a4
@ -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<Vec<AlbumField>>,
|
||||
pub except: Vec<Vec<AlbumField>>,
|
||||
pub include: Vec<AlbumField>,
|
||||
pub except: Vec<AlbumField>,
|
||||
}
|
||||
|
||||
#[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<Vec<AlbumField>>, 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<AlbumField>, 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),
|
||||
]],
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
]],
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user