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 mockall::predicate;
use crate::core::{ use crate::core::{
collection::{artist::Artist, Collection}, collection::{
album::{AlbumDate, AlbumMonth},
artist::Artist,
Collection,
},
testmod::FULL_COLLECTION, testmod::FULL_COLLECTION,
}; };
@ -82,7 +86,14 @@ mod tests {
fn expected() -> Collection { fn expected() -> Collection {
let mut expected = FULL_COLLECTION.to_owned(); let mut expected = FULL_COLLECTION.to_owned();
for artist in expected.iter_mut() { 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 expected
} }

View File

@ -1,5 +1,5 @@
pub static DATABASE_JSON: &str = "{\ pub static DATABASE_JSON: &str = "{\
\"V20240210\":\ \"V20240302\":\
[\ [\
{\ {\
\"name\":\"Album_Artist A\",\ \"name\":\"Album_Artist A\",\
@ -8,7 +8,11 @@ pub static DATABASE_JSON: &str = "{\
\"properties\":{\ \"properties\":{\
\"MusicButler\":[\"https://www.musicbutler.io/artist-page/000000000\"],\ \"MusicButler\":[\"https://www.musicbutler.io/artist-page/000000000\"],\
\"Qobuz\":[\"https://www.qobuz.com/nl-nl/interpreter/artist-a/download-streaming-albums\"]\ \"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\",\ \"name\":\"Album_Artist B\",\
@ -21,19 +25,33 @@ pub static DATABASE_JSON: &str = "{\
\"https://www.musicbutler.io/artist-page/111111112\"\ \"https://www.musicbutler.io/artist-page/111111112\"\
],\ ],\
\"Qobuz\":[\"https://www.qobuz.com/nl-nl/interpreter/artist-b/download-streaming-albums\"]\ \"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\",\ \"name\":\"The Album_Artist C\",\
\"sort\":\"Album_Artist C, The\",\ \"sort\":\"Album_Artist C, The\",\
\"musicbrainz\":\"https://musicbrainz.org/artist/11111111-1111-1111-1111-111111111111\",\ \"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\",\ \"name\":\"Album_Artist D\",\
\"sort\":null,\ \"sort\":null,\
\"musicbrainz\":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 serde::Deserialize;
use crate::{ use crate::core::{
collection::artist::{ArtistId, MusicBrainz}, collection::{
core::{ album::{Album, AlbumDate, AlbumId, AlbumMonth, AlbumSeq},
collection::{artist::Artist, Collection}, artist::{Artist, ArtistId, MusicBrainz},
database::{serde::Database, LoadError}, Collection,
}, },
database::{serde::Database, LoadError},
}; };
pub type DeserializeDatabase = Database<DeserializeArtist>; pub type DeserializeDatabase = Database<DeserializeArtist>;
@ -18,6 +19,13 @@ pub struct DeserializeArtist {
sort: Option<String>, sort: Option<String>,
musicbrainz: Option<String>, musicbrainz: Option<String>,
properties: HashMap<String, Vec<String>>, properties: HashMap<String, Vec<String>>,
albums: Vec<DeserializeAlbum>,
}
#[derive(Debug, Deserialize)]
pub struct DeserializeAlbum {
title: String,
seq: u8,
} }
impl TryFrom<DeserializeDatabase> for Collection { impl TryFrom<DeserializeDatabase> for Collection {
@ -25,7 +33,7 @@ impl TryFrom<DeserializeDatabase> for Collection {
fn try_from(database: DeserializeDatabase) -> Result<Self, Self::Error> { fn try_from(database: DeserializeDatabase) -> Result<Self, Self::Error> {
match database { match database {
Database::V20240210(collection) => collection Database::V20240302(collection) | Database::V20240210(collection) => collection
.into_iter() .into_iter()
.map(|artist| artist.try_into()) .map(|artist| artist.try_into())
.collect(), .collect(),
@ -42,7 +50,22 @@ impl TryFrom<DeserializeArtist> for Artist {
sort: artist.sort.map(ArtistId::new), sort: artist.sort.map(ArtistId::new),
musicbrainz: artist.musicbrainz.map(MusicBrainz::new).transpose()?, musicbrainz: artist.musicbrainz.map(MusicBrainz::new).transpose()?,
properties: artist.properties, 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)] #[derive(Debug, Serialize, Deserialize)]
pub enum Database<ARTIST> { pub enum Database<ARTIST> {
V20240302(Vec<ARTIST>),
V20240210(Vec<ARTIST>), V20240210(Vec<ARTIST>),
} }

View File

@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use serde::Serialize; use serde::Serialize;
use crate::core::{ use crate::core::{
collection::{artist::Artist, Collection}, collection::{album::Album, artist::Artist, Collection},
database::serde::Database, database::serde::Database,
}; };
@ -15,11 +15,18 @@ pub struct SerializeArtist<'a> {
sort: Option<&'a str>, sort: Option<&'a str>,
musicbrainz: Option<&'a str>, musicbrainz: Option<&'a str>,
properties: BTreeMap<&'a str, &'a Vec<String>>, 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> { impl<'a> From<&'a Collection> for SerializeDatabase<'a> {
fn from(collection: &'a Collection) -> Self { 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() .iter()
.map(|(k, v)| (k.as_ref(), v)) .map(|(k, v)| (k.as_ref(), v))
.collect(), .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,
} }
} }
} }