From 6dacdd3742db5d0951c0c71593d895585d239ed9 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Thu, 30 Mar 2023 23:54:11 +0900 Subject: [PATCH] Simplify album id --- src/database/json.rs | 18 ++++++---- src/lib.rs | 11 +++++-- src/library/beets.rs | 51 +++++++++++++---------------- tests/files/database_json_test.json | 2 +- 4 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/database/json.rs b/src/database/json.rs index 9d9f939..98bdb74 100644 --- a/src/database/json.rs +++ b/src/database/json.rs @@ -62,16 +62,18 @@ mod tests { use super::*; - use crate::{Album, Track}; + use crate::{Album, AlbumId, Track}; const TEST_FILENAME: &str = "tests/files/database_json_test.json"; fn test_data() -> Vec { vec![ Album { - artist: String::from("Artist A"), - year: 1998, - title: String::from("Release group A"), + id: AlbumId { + artist: String::from("Artist A"), + year: 1998, + title: String::from("Release group A"), + }, tracks: vec![ Track { number: 1, @@ -91,9 +93,11 @@ mod tests { ], }, Album { - artist: String::from("Artist B"), - year: 2008, - title: String::from("Release group B"), + id: AlbumId { + artist: String::from("Artist B"), + year: 2008, + title: String::from("Release group B"), + }, tracks: vec![Track { number: 1, title: String::from("Track B.1"), diff --git a/src/lib.rs b/src/lib.rs index ba9f299..bbe5a8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,11 +17,16 @@ pub struct Track { pub artist: Vec, } -/// An album is a collection of tracks that were released together. -#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)] -pub struct Album { +#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] +pub struct AlbumId { pub artist: String, pub year: u32, 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, } diff --git a/src/library/beets.rs b/src/library/beets.rs index d45599d..6d19e8c 100644 --- a/src/library/beets.rs +++ b/src/library/beets.rs @@ -1,6 +1,6 @@ use std::{collections::HashSet, fmt::Display, process::Command}; -use crate::{Album, Track}; +use crate::{Album, AlbumId, Track}; use super::{Error, Library, Query, QueryOption}; @@ -78,19 +78,6 @@ pub struct Beets { executor: Box, } -#[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 { () => { "-*^-" @@ -150,16 +137,18 @@ impl Beets { if album_ids.contains(&aid) { // 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); } else { let album_artist = aid.artist.to_string(); let album_title = aid.title.to_string(); album_ids.insert(aid); albums.push(Album { - artist: album_artist, - year: album_year, - title: album_title, + id: AlbumId { + artist: album_artist, + year: album_year, + title: album_title, + }, tracks: vec![track], }); } @@ -227,9 +216,11 @@ mod tests { fn test_data() -> Vec { vec![ Album { - artist: "album_artist.a".to_string(), - year: 1998, - title: "album_title.a".to_string(), + id: AlbumId { + artist: "album_artist.a".to_string(), + year: 1998, + title: "album_title.a".to_string(), + }, tracks: vec![ Track { number: 1, @@ -249,9 +240,11 @@ mod tests { ], }, Album { - artist: "album_artist.b".to_string(), - year: 2003, - title: "album_title.b".to_string(), + id: AlbumId { + artist: "album_artist.b".to_string(), + year: 2003, + title: "album_title.b".to_string(), + }, tracks: vec![ Track { number: 1, @@ -269,9 +262,9 @@ mod tests { } fn album_to_beets_string(album: &Album) -> Vec { - let album_artist = &album.artist; - let album_year = &album.year; - let album_title = &album.title; + let album_artist = &album.id.artist; + let album_year = &album.id.year; + let album_title = &album.id.title; let mut strings = vec![]; for track in album.tracks.iter() { @@ -372,8 +365,8 @@ mod tests { #[test] fn test_list_album_title_year_clash() { let mut expected = test_data(); - expected[1].year = expected[0].year; - expected[1].title = expected[0].title.clone(); + expected[1].id.year = expected[0].id.year; + expected[1].id.title = expected[0].id.title.clone(); let mut output = vec![]; for album in expected.iter() { diff --git a/tests/files/database_json_test.json b/tests/files/database_json_test.json index 557e3c8..afbc9fb 100644 --- a/tests/files/database_json_test.json +++ b/tests/files/database_json_test.json @@ -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"]}]}] \ No newline at end of file +[{"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"]}]}] \ No newline at end of file