Add a filtering tool to only show only certain release group types #252
@ -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));
|
||||
}
|
||||
}
|
||||
|
18
src/main.rs
18
src/main.rs
@ -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,9 +78,7 @@ fn default_filter() -> CollectionFilter {
|
||||
AlbumField::PrimaryType(Some(AlbumPrimaryType::Ep)),
|
||||
AlbumField::PrimaryType(Some(AlbumPrimaryType::Album)),
|
||||
]],
|
||||
exclude: vec![
|
||||
vec![AlbumField::Ownership(AlbumOwnership::None)],
|
||||
vec![
|
||||
except: vec![vec![
|
||||
AlbumField::SecondaryType(AlbumSecondaryType::Compilation),
|
||||
AlbumField::SecondaryType(AlbumSecondaryType::Soundtrack),
|
||||
AlbumField::SecondaryType(AlbumSecondaryType::Spokenword),
|
||||
@ -89,8 +91,8 @@ fn default_filter() -> CollectionFilter {
|
||||
AlbumField::SecondaryType(AlbumSecondaryType::MixtapeStreet),
|
||||
AlbumField::SecondaryType(AlbumSecondaryType::Demo),
|
||||
AlbumField::SecondaryType(AlbumSecondaryType::FieldRecording),
|
||||
],
|
||||
],
|
||||
AlbumField::Ownership(AlbumOwnership::None),
|
||||
]],
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user