This commit is contained in:
Wojciech Kozlowski 2025-01-04 17:50:05 +01:00
parent eac27d3b17
commit 9e7f0d8092
2 changed files with 121 additions and 23 deletions

View File

@ -4,7 +4,7 @@ use crate::core::collection::album::{Album, AlbumOwnership, AlbumPrimaryType, Al
#[derive(Debug, Default)]
pub struct CollectionFilter {
pub include: Vec<Vec<AlbumField>>,
pub exclude: Vec<Vec<AlbumField>>,
pub except: Vec<Vec<AlbumField>>,
}
#[derive(Debug)]
@ -17,8 +17,8 @@ pub enum AlbumField {
impl CollectionFilter {
pub fn filter_album(&self, album: &Album) -> bool {
let include = Self::filter_and(&self.include, album);
let exclude = Self::filter_and(&self.exclude, album);
include && !exclude
let except = Self::filter_and(&self.except, album);
include && !except
}
fn filter_and(group: &Vec<Vec<AlbumField>>, album: &Album) -> bool {
@ -48,3 +48,99 @@ impl CollectionFilter {
}
}
}
#[cfg(test)]
mod tests {
use crate::collection::{
album::AlbumId,
track::{Track, TrackFormat, TrackId, TrackNum, TrackQuality},
};
use super::*;
fn test_filter() -> CollectionFilter {
CollectionFilter {
include: vec![vec![
AlbumField::PrimaryType(None),
AlbumField::PrimaryType(Some(AlbumPrimaryType::Ep)),
AlbumField::PrimaryType(Some(AlbumPrimaryType::Album)),
]],
except: vec![vec![
AlbumField::SecondaryType(AlbumSecondaryType::Compilation),
AlbumField::SecondaryType(AlbumSecondaryType::Soundtrack),
AlbumField::SecondaryType(AlbumSecondaryType::Live),
AlbumField::SecondaryType(AlbumSecondaryType::Demo),
AlbumField::Ownership(AlbumOwnership::None),
]],
}
}
fn test_track() -> Track {
Track {
id: TrackId {
title: String::from("Track"),
},
number: TrackNum(1),
artist: vec![String::from("Artist")],
quality: TrackQuality {
format: TrackFormat::Mp3,
bitrate: 320,
},
}
}
fn test_album() -> Album {
let mut album = Album::new(AlbumId::new("An Album"));
album.tracks.push(test_track());
album
}
#[test]
fn filter_primary_type() {
let filter = test_filter();
let mut album = test_album();
album.meta.info.primary_type = None;
assert!(filter.filter_album(&album));
album.meta.info.primary_type = Some(AlbumPrimaryType::Ep);
assert!(filter.filter_album(&album));
album.meta.info.primary_type = Some(AlbumPrimaryType::Album);
assert!(filter.filter_album(&album));
album.meta.info.primary_type = Some(AlbumPrimaryType::Broadcast);
assert!(!filter.filter_album(&album));
album.meta.info.primary_type = Some(AlbumPrimaryType::Other);
assert!(!filter.filter_album(&album));
}
#[test]
fn filter_secondary_type() {
let filter = test_filter()
}
#[test]
fn filter_ownership() {
let filter = test_filter();
let mut album = Album::new(AlbumId::new("An Album"));
album.tracks.clear();
assert!(!filter.filter_album(&album));
album.tracks.push(test_track());
assert_eq!(
album.get_ownership(),
AlbumOwnership::Owned(TrackFormat::Mp3)
);
assert!(filter.filter_album(&album));
album.tracks[0].quality.format = TrackFormat::Flac;
assert_eq!(
album.get_ownership(),
AlbumOwnership::Owned(TrackFormat::Flac)
);
assert!(filter.filter_album(&album));
}
}

View File

@ -6,17 +6,21 @@ use ratatui::{backend::CrosstermBackend, Terminal};
use structopt::StructOpt;
use musichoard::{
collection::album::{AlbumOwnership, AlbumPrimaryType, AlbumSecondaryType}, external::{
collection::album::{AlbumOwnership, AlbumPrimaryType, AlbumSecondaryType},
external::{
database::json::{backend::JsonDatabaseFileBackend, JsonDatabase},
library::beets::{
executor::{ssh::BeetsLibrarySshExecutor, BeetsLibraryProcessExecutor},
BeetsLibrary,
},
musicbrainz::{api::MusicBrainzClient, http::MusicBrainzHttp},
}, filter::{AlbumField, CollectionFilter}, interface::{
},
filter::{AlbumField, CollectionFilter},
interface::{
database::{IDatabase, NullDatabase},
library::{ILibrary, NullLibrary},
}, IMusicHoardBase, MusicHoardBuilder, NoDatabase, NoLibrary
},
IMusicHoardBase, MusicHoardBuilder, NoDatabase, NoLibrary,
};
use tui::{
@ -74,23 +78,21 @@ fn default_filter() -> CollectionFilter {
AlbumField::PrimaryType(Some(AlbumPrimaryType::Ep)),
AlbumField::PrimaryType(Some(AlbumPrimaryType::Album)),
]],
exclude: vec![
vec![AlbumField::Ownership(AlbumOwnership::None)],
vec![
AlbumField::SecondaryType(AlbumSecondaryType::Compilation),
AlbumField::SecondaryType(AlbumSecondaryType::Soundtrack),
AlbumField::SecondaryType(AlbumSecondaryType::Spokenword),
AlbumField::SecondaryType(AlbumSecondaryType::Interview),
AlbumField::SecondaryType(AlbumSecondaryType::Audiobook),
AlbumField::SecondaryType(AlbumSecondaryType::AudioDrama),
AlbumField::SecondaryType(AlbumSecondaryType::Live),
AlbumField::SecondaryType(AlbumSecondaryType::Remix),
AlbumField::SecondaryType(AlbumSecondaryType::DjMix),
AlbumField::SecondaryType(AlbumSecondaryType::MixtapeStreet),
AlbumField::SecondaryType(AlbumSecondaryType::Demo),
AlbumField::SecondaryType(AlbumSecondaryType::FieldRecording),
],
],
except: vec![vec![
AlbumField::SecondaryType(AlbumSecondaryType::Compilation),
AlbumField::SecondaryType(AlbumSecondaryType::Soundtrack),
AlbumField::SecondaryType(AlbumSecondaryType::Spokenword),
AlbumField::SecondaryType(AlbumSecondaryType::Interview),
AlbumField::SecondaryType(AlbumSecondaryType::Audiobook),
AlbumField::SecondaryType(AlbumSecondaryType::AudioDrama),
AlbumField::SecondaryType(AlbumSecondaryType::Live),
AlbumField::SecondaryType(AlbumSecondaryType::Remix),
AlbumField::SecondaryType(AlbumSecondaryType::DjMix),
AlbumField::SecondaryType(AlbumSecondaryType::MixtapeStreet),
AlbumField::SecondaryType(AlbumSecondaryType::Demo),
AlbumField::SecondaryType(AlbumSecondaryType::FieldRecording),
AlbumField::Ownership(AlbumOwnership::None),
]],
}
}