New database storage

This commit is contained in:
Wojciech Kozlowski 2024-03-02 16:18:50 +01:00
parent bb060770b4
commit e6969bfc52
5 changed files with 86 additions and 16 deletions

View File

@ -72,7 +72,11 @@ mod tests {
use mockall::predicate;
use crate::core::{
collection::{artist::Artist, Collection},
collection::{
album::{AlbumDate, AlbumMonth},
artist::Artist,
Collection,
},
testmod::FULL_COLLECTION,
};
@ -82,7 +86,14 @@ mod tests {
fn expected() -> Collection {
let mut expected = FULL_COLLECTION.to_owned();
for artist in expected.iter_mut() {
artist.albums.clear();
for album in artist.albums.iter_mut() {
album.date = AlbumDate {
year: 0,
month: AlbumMonth::None,
day: 0,
};
album.tracks.clear();
}
}
expected
}

View File

@ -1,5 +1,5 @@
pub static DATABASE_JSON: &str = "{\
\"V20240210\":\
\"V20240302\":\
[\
{\
\"name\":\"Album_Artist A\",\
@ -8,7 +8,11 @@ pub static DATABASE_JSON: &str = "{\
\"properties\":{\
\"MusicButler\":[\"https://www.musicbutler.io/artist-page/000000000\"],\
\"Qobuz\":[\"https://www.qobuz.com/nl-nl/interpreter/artist-a/download-streaming-albums\"]\
}\
},\
\"albums\":[\
{\"title\":\"album_title a.a\",\"seq\":0},\
{\"title\":\"album_title a.b\",\"seq\":0}\
]\
},\
{\
\"name\":\"Album_Artist B\",\
@ -21,19 +25,33 @@ pub static DATABASE_JSON: &str = "{\
\"https://www.musicbutler.io/artist-page/111111112\"\
],\
\"Qobuz\":[\"https://www.qobuz.com/nl-nl/interpreter/artist-b/download-streaming-albums\"]\
}\
},\
\"albums\":[\
{\"title\":\"album_title b.a\",\"seq\":0},\
{\"title\":\"album_title b.b\",\"seq\":0},\
{\"title\":\"album_title b.c\",\"seq\":0},\
{\"title\":\"album_title b.d\",\"seq\":0}\
]\
},\
{\
\"name\":\"The Album_Artist C\",\
\"sort\":\"Album_Artist C, The\",\
\"musicbrainz\":\"https://musicbrainz.org/artist/11111111-1111-1111-1111-111111111111\",\
\"properties\":{}\
\"properties\":{},\
\"albums\":[\
{\"title\":\"album_title c.a\",\"seq\":0},\
{\"title\":\"album_title c.b\",\"seq\":0}\
]\
},\
{\
\"name\":\"Album_Artist D\",\
\"sort\":null,\
\"musicbrainz\":null,\
\"properties\":{}\
\"properties\":{},\
\"albums\":[\
{\"title\":\"album_title d.a\",\"seq\":0},\
{\"title\":\"album_title d.b\",\"seq\":0}\
]\
}\
]\
}";

View File

@ -2,12 +2,13 @@ use std::collections::HashMap;
use serde::Deserialize;
use crate::{
collection::artist::{ArtistId, MusicBrainz},
core::{
collection::{artist::Artist, Collection},
database::{serde::Database, LoadError},
use crate::core::{
collection::{
album::{Album, AlbumDate, AlbumId, AlbumMonth, AlbumSeq},
artist::{Artist, ArtistId, MusicBrainz},
Collection,
},
database::{serde::Database, LoadError},
};
pub type DeserializeDatabase = Database<DeserializeArtist>;
@ -18,6 +19,13 @@ pub struct DeserializeArtist {
sort: Option<String>,
musicbrainz: Option<String>,
properties: HashMap<String, Vec<String>>,
albums: Vec<DeserializeAlbum>,
}
#[derive(Debug, Deserialize)]
pub struct DeserializeAlbum {
title: String,
seq: u8,
}
impl TryFrom<DeserializeDatabase> for Collection {
@ -25,7 +33,7 @@ impl TryFrom<DeserializeDatabase> for Collection {
fn try_from(database: DeserializeDatabase) -> Result<Self, Self::Error> {
match database {
Database::V20240210(collection) => collection
Database::V20240302(collection) | Database::V20240210(collection) => collection
.into_iter()
.map(|artist| artist.try_into())
.collect(),
@ -42,7 +50,22 @@ impl TryFrom<DeserializeArtist> for Artist {
sort: artist.sort.map(ArtistId::new),
musicbrainz: artist.musicbrainz.map(MusicBrainz::new).transpose()?,
properties: artist.properties,
albums: vec![],
albums: artist.albums.into_iter().map(Into::into).collect(),
})
}
}
impl From<DeserializeAlbum> for Album {
fn from(album: DeserializeAlbum) -> Self {
Album {
id: AlbumId { title: album.title },
date: AlbumDate {
year: 0,
month: AlbumMonth::None,
day: 0,
},
seq: AlbumSeq(album.seq),
tracks: vec![],
}
}
}

View File

@ -7,5 +7,6 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub enum Database<ARTIST> {
V20240302(Vec<ARTIST>),
V20240210(Vec<ARTIST>),
}

View File

@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use serde::Serialize;
use crate::core::{
collection::{artist::Artist, Collection},
collection::{album::Album, artist::Artist, Collection},
database::serde::Database,
};
@ -15,11 +15,18 @@ pub struct SerializeArtist<'a> {
sort: Option<&'a str>,
musicbrainz: Option<&'a str>,
properties: BTreeMap<&'a str, &'a Vec<String>>,
albums: Vec<SerializeAlbum<'a>>,
}
#[derive(Debug, Serialize)]
pub struct SerializeAlbum<'a> {
title: &'a str,
seq: u8,
}
impl<'a> From<&'a Collection> for SerializeDatabase<'a> {
fn from(collection: &'a Collection) -> Self {
Database::V20240210(collection.iter().map(|artist| artist.into()).collect())
Database::V20240302(collection.iter().map(|artist| artist.into()).collect())
}
}
@ -34,6 +41,16 @@ impl<'a> From<&'a Artist> for SerializeArtist<'a> {
.iter()
.map(|(k, v)| (k.as_ref(), v))
.collect(),
albums: artist.albums.iter().map(Into::into).collect(),
}
}
}
impl<'a> From<&'a Album> for SerializeAlbum<'a> {
fn from(album: &'a Album) -> Self {
SerializeAlbum {
title: &album.id.title,
seq: album.seq.0,
}
}
}