Resolve "Local collection trait and beets implementation" #9

Merged
wojtek merged 12 commits from 4---local-collection-trait-and-beets-implementation into main 2023-03-31 14:24:54 +02:00
4 changed files with 42 additions and 40 deletions
Showing only changes of commit 6dacdd3742 - Show all commits

View File

@ -62,16 +62,18 @@ mod tests {
use super::*; use super::*;
use crate::{Album, Track}; use crate::{Album, AlbumId, Track};
const TEST_FILENAME: &str = "tests/files/database_json_test.json"; const TEST_FILENAME: &str = "tests/files/database_json_test.json";
fn test_data() -> Vec<Album> { fn test_data() -> Vec<Album> {
vec![ vec![
Album { Album {
artist: String::from("Artist A"), id: AlbumId {
year: 1998, artist: String::from("Artist A"),
title: String::from("Release group A"), year: 1998,
title: String::from("Release group A"),
},
tracks: vec![ tracks: vec![
Track { Track {
number: 1, number: 1,
@ -91,9 +93,11 @@ mod tests {
], ],
}, },
Album { Album {
artist: String::from("Artist B"), id: AlbumId {
year: 2008, artist: String::from("Artist B"),
title: String::from("Release group B"), year: 2008,
title: String::from("Release group B"),
},
tracks: vec![Track { tracks: vec![Track {
number: 1, number: 1,
title: String::from("Track B.1"), title: String::from("Track B.1"),

View File

@ -17,11 +17,16 @@ pub struct Track {
pub artist: Vec<String>, pub artist: Vec<String>,
} }
/// An album is a collection of tracks that were released together. #[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)] pub struct AlbumId {
pub struct Album {
pub artist: String, pub artist: String,
pub year: u32, pub year: u32,
pub title: String, pub title: String,
}
/// An album is a collection of tracks that were released together.
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct Album {
pub id: AlbumId,
pub tracks: Vec<Track>, pub tracks: Vec<Track>,
} }

View File

@ -1,6 +1,6 @@
use std::{collections::HashSet, fmt::Display, process::Command}; use std::{collections::HashSet, fmt::Display, process::Command};
use crate::{Album, Track}; use crate::{Album, AlbumId, Track};
use super::{Error, Library, Query, QueryOption}; use super::{Error, Library, Query, QueryOption};
@ -78,19 +78,6 @@ pub struct Beets {
executor: Box<dyn BeetsExecutor>, executor: Box<dyn BeetsExecutor>,
} }
#[derive(Debug, Hash, Eq, PartialEq)]
struct AlbumId {
artist: String,
year: u32,
title: String,
}
impl AlbumId {
fn matches(&self, album: &Album) -> bool {
(self.artist == album.artist) && (self.year == album.year) && (self.title == album.title)
}
}
macro_rules! separator { macro_rules! separator {
() => { () => {
"-*^-" "-*^-"
@ -150,16 +137,18 @@ impl Beets {
if album_ids.contains(&aid) { if album_ids.contains(&aid) {
// Beets returns results in order so we look from the back. // Beets returns results in order so we look from the back.
let album = albums.iter_mut().rev().find(|a| aid.matches(a)).unwrap(); let album = albums.iter_mut().rev().find(|a| a.id == aid).unwrap();
album.tracks.push(track); album.tracks.push(track);
} else { } else {
let album_artist = aid.artist.to_string(); let album_artist = aid.artist.to_string();
let album_title = aid.title.to_string(); let album_title = aid.title.to_string();
album_ids.insert(aid); album_ids.insert(aid);
albums.push(Album { albums.push(Album {
artist: album_artist, id: AlbumId {
year: album_year, artist: album_artist,
title: album_title, year: album_year,
title: album_title,
},
tracks: vec![track], tracks: vec![track],
}); });
} }
@ -227,9 +216,11 @@ mod tests {
fn test_data() -> Vec<Album> { fn test_data() -> Vec<Album> {
vec![ vec![
Album { Album {
artist: "album_artist.a".to_string(), id: AlbumId {
year: 1998, artist: "album_artist.a".to_string(),
title: "album_title.a".to_string(), year: 1998,
title: "album_title.a".to_string(),
},
tracks: vec![ tracks: vec![
Track { Track {
number: 1, number: 1,
@ -249,9 +240,11 @@ mod tests {
], ],
}, },
Album { Album {
artist: "album_artist.b".to_string(), id: AlbumId {
year: 2003, artist: "album_artist.b".to_string(),
title: "album_title.b".to_string(), year: 2003,
title: "album_title.b".to_string(),
},
tracks: vec![ tracks: vec![
Track { Track {
number: 1, number: 1,
@ -269,9 +262,9 @@ mod tests {
} }
fn album_to_beets_string(album: &Album) -> Vec<String> { fn album_to_beets_string(album: &Album) -> Vec<String> {
let album_artist = &album.artist; let album_artist = &album.id.artist;
let album_year = &album.year; let album_year = &album.id.year;
let album_title = &album.title; let album_title = &album.id.title;
let mut strings = vec![]; let mut strings = vec![];
for track in album.tracks.iter() { for track in album.tracks.iter() {
@ -372,8 +365,8 @@ mod tests {
#[test] #[test]
fn test_list_album_title_year_clash() { fn test_list_album_title_year_clash() {
let mut expected = test_data(); let mut expected = test_data();
expected[1].year = expected[0].year; expected[1].id.year = expected[0].id.year;
expected[1].title = expected[0].title.clone(); expected[1].id.title = expected[0].id.title.clone();
let mut output = vec![]; let mut output = vec![];
for album in expected.iter() { for album in expected.iter() {

View File

@ -1 +1 @@
[{"artist":"Artist A","year":1998,"title":"Release group A","tracks":[{"number":1,"title":"Track A.1","artist":["Artist A.A"]},{"number":2,"title":"Track A.2","artist":["Artist A.A"]},{"number":3,"title":"Track A.3","artist":["Artist A.A","Artist A.B"]}]},{"artist":"Artist B","year":2008,"title":"Release group B","tracks":[{"number":1,"title":"Track B.1","artist":["Artist B.A"]}]}] [{"id":{"artist":"Artist A","year":1998,"title":"Release group A"},"tracks":[{"number":1,"title":"Track A.1","artist":["Artist A.A"]},{"number":2,"title":"Track A.2","artist":["Artist A.A"]},{"number":3,"title":"Track A.3","artist":["Artist A.A","Artist A.B"]}]},{"id":{"artist":"Artist B","year":2008,"title":"Release group B"},"tracks":[{"number":1,"title":"Track B.1","artist":["Artist B.A"]}]}]