Use the Deserialize trait for JSON just like for MusicBrainz #197

Merged
wojtek merged 3 commits from 195---use-the-deserialize-trait-for-json-just-like-for-musicbrainz into main 2024-08-28 23:02:15 +02:00
4 changed files with 59 additions and 21 deletions
Showing only changes of commit 87c459f00b - Show all commits

View File

@ -1,10 +1,10 @@
pub static DATABASE_JSON: &str = "{\ pub static DATABASE_JSON: &str = "{\
\"V20240313\":\ \"V20240828\":\
[\ [\
{\ {\
\"name\":\"Album_Artist A\",\ \"name\":\"Album_Artist A\",\
\"sort\":null,\ \"sort\":null,\
\"musicbrainz\":\"https://musicbrainz.org/artist/00000000-0000-0000-0000-000000000000\",\ \"musicbrainz\":\"00000000-0000-0000-0000-000000000000\",\
\"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\"]\
@ -12,7 +12,7 @@ pub static DATABASE_JSON: &str = "{\
\"albums\":[\ \"albums\":[\
{\ {\
\"title\":\"album_title a.a\",\"seq\":1,\ \"title\":\"album_title a.a\",\"seq\":1,\
\"musicbrainz\":\"https://musicbrainz.org/release-group/00000000-0000-0000-0000-000000000000\",\ \"musicbrainz\":\"00000000-0000-0000-0000-000000000000\",\
\"primary_type\":\"Album\",\"secondary_types\":[]\ \"primary_type\":\"Album\",\"secondary_types\":[]\
},\ },\
{\ {\
@ -24,7 +24,7 @@ pub static DATABASE_JSON: &str = "{\
{\ {\
\"name\":\"Album_Artist B\",\ \"name\":\"Album_Artist B\",\
\"sort\":null,\ \"sort\":null,\
\"musicbrainz\":\"https://musicbrainz.org/artist/11111111-1111-1111-1111-111111111111\",\ \"musicbrainz\":\"11111111-1111-1111-1111-111111111111\",\
\"properties\":{\ \"properties\":{\
\"Bandcamp\":[\"https://artist-b.bandcamp.com/\"],\ \"Bandcamp\":[\"https://artist-b.bandcamp.com/\"],\
\"MusicButler\":[\ \"MusicButler\":[\
@ -40,12 +40,12 @@ pub static DATABASE_JSON: &str = "{\
},\ },\
{\ {\
\"title\":\"album_title b.b\",\"seq\":3,\ \"title\":\"album_title b.b\",\"seq\":3,\
\"musicbrainz\":\"https://musicbrainz.org/release-group/11111111-1111-1111-1111-111111111111\",\ \"musicbrainz\":\"11111111-1111-1111-1111-111111111111\",\
\"primary_type\":\"Album\",\"secondary_types\":[]\ \"primary_type\":\"Album\",\"secondary_types\":[]\
},\ },\
{\ {\
\"title\":\"album_title b.c\",\"seq\":2,\ \"title\":\"album_title b.c\",\"seq\":2,\
\"musicbrainz\":\"https://musicbrainz.org/release-group/11111111-1111-1111-1111-111111111112\",\ \"musicbrainz\":\"11111111-1111-1111-1111-111111111112\",\
\"primary_type\":\"Album\",\"secondary_types\":[]\ \"primary_type\":\"Album\",\"secondary_types\":[]\
},\ },\
{\ {\
@ -57,7 +57,7 @@ pub static DATABASE_JSON: &str = "{\
{\ {\
\"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\":\"11111111-1111-1111-1111-111111111111\",\
\"properties\":{},\ \"properties\":{},\
\"albums\":[\ \"albums\":[\
{\ {\

View File

@ -14,13 +14,13 @@ use crate::{
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub enum DeserializeDatabase { pub enum DeserializeDatabase {
V20240313(Vec<DeserializeArtist>), V20240828(Vec<DeserializeArtist>),
} }
impl From<DeserializeDatabase> for Collection { impl From<DeserializeDatabase> for Collection {
fn from(database: DeserializeDatabase) -> Self { fn from(database: DeserializeDatabase) -> Self {
match database { match database {
DeserializeDatabase::V20240313(collection) => { DeserializeDatabase::V20240828(collection) => {
collection.into_iter().map(Into::into).collect() collection.into_iter().map(Into::into).collect()
} }
} }
@ -68,7 +68,7 @@ impl<'de> Visitor<'de> for DeserializeMbArtistRefVisitor {
E: serde::de::Error, E: serde::de::Error,
{ {
Ok(DeserializeMbArtistRef( Ok(DeserializeMbArtistRef(
MbArtistRef::from_url_str(v).map_err(|e: CollectionError| E::custom(e.to_string()))?, MbArtistRef::from_uuid_str(v).map_err(|e: CollectionError| E::custom(e.to_string()))?,
)) ))
} }
} }
@ -105,7 +105,7 @@ impl<'de> Visitor<'de> for DeserializeMbAlbumRefVisitor {
E: serde::de::Error, E: serde::de::Error,
{ {
Ok(DeserializeMbAlbumRef( Ok(DeserializeMbAlbumRef(
MbAlbumRef::from_url_str(v).map_err(|e: CollectionError| E::custom(e.to_string()))?, MbAlbumRef::from_uuid_str(v).map_err(|e: CollectionError| E::custom(e.to_string()))?,
)) ))
} }
} }
@ -144,3 +144,44 @@ impl From<DeserializeAlbum> for Album {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn deserialize_mb_artist_ref() {
let mbid = "\"d368baa8-21ca-4759-9731-0b2753071ad8\"";
let mbref: DeserializeMbArtistRef = serde_json::from_str(mbid).unwrap();
let mbref: MbArtistRef = mbref.into();
assert_eq!(
mbref,
MbArtistRef::from_uuid_str("d368baa8-21ca-4759-9731-0b2753071ad8").unwrap()
);
let mbid = "null";
let result: Result<DeserializeMbArtistRef, _> = serde_json::from_str(mbid);
assert!(result
.unwrap_err()
.to_string()
.contains("a valid MusicBrainz identifier"));
}
#[test]
fn deserialize_mb_album_ref() {
let mbid = "\"d368baa8-21ca-4759-9731-0b2753071ad8\"";
let mbref: DeserializeMbAlbumRef = serde_json::from_str(mbid).unwrap();
let mbref: MbAlbumRef = mbref.into();
assert_eq!(
mbref,
MbAlbumRef::from_uuid_str("d368baa8-21ca-4759-9731-0b2753071ad8").unwrap()
);
let mbid = "null";
let result: Result<DeserializeMbAlbumRef, _> = serde_json::from_str(mbid);
assert!(result
.unwrap_err()
.to_string()
.contains("a valid MusicBrainz identifier"));
}
}

View File

@ -10,12 +10,12 @@ use crate::{
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub enum SerializeDatabase<'a> { pub enum SerializeDatabase<'a> {
V20240313(Vec<SerializeArtist<'a>>), V20240828(Vec<SerializeArtist<'a>>),
} }
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 {
SerializeDatabase::V20240313(collection.iter().map(Into::into).collect()) SerializeDatabase::V20240828(collection.iter().map(Into::into).collect())
} }
} }
@ -45,7 +45,7 @@ impl<'a> Serialize for SerializeMbArtistRef<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
serializer.serialize_str(&self.0.url().as_str()) serializer.serialize_str(&self.0.mbid().uuid().as_hyphenated().to_string())
} }
} }
@ -57,7 +57,7 @@ impl<'a> Serialize for SerializeMbAlbumRef<'a> {
where where
S: serde::Serializer, S: serde::Serializer,
{ {
serializer.serialize_str(&self.0.url().as_str()) serializer.serialize_str(&self.0.mbid().uuid().as_hyphenated().to_string())
} }
} }
@ -66,10 +66,7 @@ impl<'a> From<&'a Artist> for SerializeArtist<'a> {
SerializeArtist { SerializeArtist {
name: &artist.id.name, name: &artist.id.name,
sort: artist.sort.as_ref().map(|id| id.name.as_ref()), sort: artist.sort.as_ref().map(|id| id.name.as_ref()),
musicbrainz: artist musicbrainz: artist.musicbrainz.as_ref().map(SerializeMbArtistRef),
.musicbrainz
.as_ref()
.map(|mb| SerializeMbArtistRef(mb)),
properties: artist properties: artist
.properties .properties
.iter() .iter()
@ -85,7 +82,7 @@ impl<'a> From<&'a Album> for SerializeAlbum<'a> {
SerializeAlbum { SerializeAlbum {
title: &album.id.title, title: &album.id.title,
seq: album.seq.0, seq: album.seq.0,
musicbrainz: album.musicbrainz.as_ref().map(|mb| SerializeMbAlbumRef(mb)), musicbrainz: album.musicbrainz.as_ref().map(SerializeMbAlbumRef),
primary_type: album.primary_type.map(Into::into), primary_type: album.primary_type.map(Into::into),
secondary_types: album secondary_types: album
.secondary_types .secondary_types

View File

@ -1 +1 @@
{"V20240313":[{"name":"Аркона","sort":"Arkona","musicbrainz":"https://musicbrainz.org/artist/baad262d-55ef-427a-83c7-f7530964f212","properties":{"Bandcamp":["https://arkonamoscow.bandcamp.com/"],"MusicButler":["https://www.musicbutler.io/artist-page/283448581"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/arkona/download-streaming-albums"]},"albums":[{"title":"Slovo","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Eluveitie","sort":null,"musicbrainz":"https://musicbrainz.org/artist/8000598a-5edb-401c-8e6d-36b167feaf38","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/269358403"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/eluveitie/download-streaming-albums"]},"albums":[{"title":"Vên [rerecorded]","seq":0,"musicbrainz":null,"primary_type":"Ep","secondary_types":[]},{"title":"Slania","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Frontside","sort":null,"musicbrainz":"https://musicbrainz.org/artist/3a901353-fccd-4afd-ad01-9c03f451b490","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/826588800"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/frontside/download-streaming-albums"]},"albums":[{"title":"…nasze jest królestwo, potęga i chwała na wieki…","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Heavens Basement","sort":"Heavens Basement","musicbrainz":"https://musicbrainz.org/artist/c2c4d56a-d599-4a18-bd2f-ae644e2198cc","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/291158685"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/heaven-s-basement/download-streaming-albums"]},"albums":[{"title":"Paper Plague","seq":0,"musicbrainz":null,"primary_type":null,"secondary_types":[]},{"title":"Unbreakable","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Metallica","sort":null,"musicbrainz":"https://musicbrainz.org/artist/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/3996865"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/metallica/download-streaming-albums"]},"albums":[{"title":"Ride the Lightning","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]},{"title":"S&M","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":["Live"]}]}]} {"V20240828":[{"name":"Аркона","sort":"Arkona","musicbrainz":"baad262d-55ef-427a-83c7-f7530964f212","properties":{"Bandcamp":["https://arkonamoscow.bandcamp.com/"],"MusicButler":["https://www.musicbutler.io/artist-page/283448581"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/arkona/download-streaming-albums"]},"albums":[{"title":"Slovo","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Eluveitie","sort":null,"musicbrainz":"8000598a-5edb-401c-8e6d-36b167feaf38","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/269358403"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/eluveitie/download-streaming-albums"]},"albums":[{"title":"Vên [rerecorded]","seq":0,"musicbrainz":null,"primary_type":"Ep","secondary_types":[]},{"title":"Slania","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Frontside","sort":null,"musicbrainz":"3a901353-fccd-4afd-ad01-9c03f451b490","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/826588800"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/frontside/download-streaming-albums"]},"albums":[{"title":"…nasze jest królestwo, potęga i chwała na wieki…","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Heavens Basement","sort":"Heavens Basement","musicbrainz":"c2c4d56a-d599-4a18-bd2f-ae644e2198cc","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/291158685"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/heaven-s-basement/download-streaming-albums"]},"albums":[{"title":"Paper Plague","seq":0,"musicbrainz":null,"primary_type":null,"secondary_types":[]},{"title":"Unbreakable","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]}]},{"name":"Metallica","sort":null,"musicbrainz":"65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","properties":{"MusicButler":["https://www.musicbutler.io/artist-page/3996865"],"Qobuz":["https://www.qobuz.com/nl-nl/interpreter/metallica/download-streaming-albums"]},"albums":[{"title":"Ride the Lightning","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":[]},{"title":"S&M","seq":0,"musicbrainz":null,"primary_type":"Album","secondary_types":["Live"]}]}]}