Simplify album id

This commit is contained in:
Wojciech Kozlowski 2023-03-30 23:54:11 +09:00
parent 9ab00fb1de
commit 6dacdd3742
4 changed files with 42 additions and 40 deletions

View File

@ -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<Album> {
vec![
Album {
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 {
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"),

View File

@ -17,11 +17,16 @@ pub struct Track {
pub artist: Vec<String>,
}
/// 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<Track>,
}

View File

@ -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<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 {
() => {
"-*^-"
@ -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 {
id: AlbumId {
artist: album_artist,
year: album_year,
title: album_title,
},
tracks: vec![track],
});
}
@ -227,9 +216,11 @@ mod tests {
fn test_data() -> Vec<Album> {
vec![
Album {
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 {
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<String> {
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() {

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"]}]}]