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
2 changed files with 33 additions and 10 deletions
Showing only changes of commit f1a02e8e58 - Show all commits

View File

@ -4,7 +4,7 @@ use crate::core::collection::album::{AlbumPrimaryType, AlbumSecondaryType};
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(remote = "AlbumPrimaryType")] #[serde(remote = "AlbumPrimaryType")]
pub enum SerdeAlbumPrimaryTypeDef { pub enum AlbumPrimaryTypeDef {
Album, Album,
Single, Single,
Ep, Ep,
@ -13,7 +13,7 @@ pub enum SerdeAlbumPrimaryTypeDef {
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct SerdeAlbumPrimaryType(#[serde(with = "SerdeAlbumPrimaryTypeDef")] AlbumPrimaryType); pub struct SerdeAlbumPrimaryType(#[serde(with = "AlbumPrimaryTypeDef")] AlbumPrimaryType);
impl From<SerdeAlbumPrimaryType> for AlbumPrimaryType { impl From<SerdeAlbumPrimaryType> for AlbumPrimaryType {
fn from(value: SerdeAlbumPrimaryType) -> Self { fn from(value: SerdeAlbumPrimaryType) -> Self {
@ -29,7 +29,7 @@ impl From<AlbumPrimaryType> for SerdeAlbumPrimaryType {
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(remote = "AlbumSecondaryType")] #[serde(remote = "AlbumSecondaryType")]
pub enum SerdeAlbumSecondaryTypeDef { pub enum AlbumSecondaryTypeDef {
Compilation, Compilation,
Soundtrack, Soundtrack,
Spokenword, Spokenword,
@ -45,9 +45,7 @@ pub enum SerdeAlbumSecondaryTypeDef {
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
pub struct SerdeAlbumSecondaryType( pub struct SerdeAlbumSecondaryType(#[serde(with = "AlbumSecondaryTypeDef")] AlbumSecondaryType);
#[serde(with = "SerdeAlbumSecondaryTypeDef")] AlbumSecondaryType,
);
impl From<SerdeAlbumSecondaryType> for AlbumSecondaryType { impl From<SerdeAlbumSecondaryType> for AlbumSecondaryType {
fn from(value: SerdeAlbumSecondaryType) -> Self { fn from(value: SerdeAlbumSecondaryType) -> Self {

View File

@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{
collection::musicbrainz::{MbAlbumRef, MbArtistRef},
core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection}, core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection},
external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType}, external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType},
}; };
@ -22,7 +23,7 @@ impl<'a> From<&'a Collection> for SerializeDatabase<'a> {
pub struct SerializeArtist<'a> { pub struct SerializeArtist<'a> {
name: &'a str, name: &'a str,
sort: Option<&'a str>, sort: Option<&'a str>,
musicbrainz: Option<&'a str>, musicbrainz: Option<SerdeMbArtistRef<'a>>,
properties: BTreeMap<&'a str, &'a Vec<String>>, properties: BTreeMap<&'a str, &'a Vec<String>>,
albums: Vec<SerializeAlbum<'a>>, albums: Vec<SerializeAlbum<'a>>,
} }
@ -31,17 +32,41 @@ pub struct SerializeArtist<'a> {
pub struct SerializeAlbum<'a> { pub struct SerializeAlbum<'a> {
title: &'a str, title: &'a str,
seq: u8, seq: u8,
musicbrainz: Option<&'a str>, musicbrainz: Option<SerdeMbAlbumRef<'a>>,
primary_type: Option<SerdeAlbumPrimaryType>, primary_type: Option<SerdeAlbumPrimaryType>,
secondary_types: Vec<SerdeAlbumSecondaryType>, secondary_types: Vec<SerdeAlbumSecondaryType>,
} }
#[derive(Clone, Debug)]
pub struct SerdeMbArtistRef<'a>(&'a MbArtistRef);
impl<'a> Serialize for SerdeMbArtistRef<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0.url().as_str())
}
}
#[derive(Clone, Debug)]
pub struct SerdeMbAlbumRef<'a>(&'a MbAlbumRef);
impl<'a> Serialize for SerdeMbAlbumRef<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0.url().as_str())
}
}
impl<'a> From<&'a Artist> for SerializeArtist<'a> { impl<'a> From<&'a Artist> for SerializeArtist<'a> {
fn from(artist: &'a Artist) -> Self { fn from(artist: &'a Artist) -> Self {
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.as_ref().map(|mb| mb.url().as_str()), musicbrainz: artist.musicbrainz.as_ref().map(|mb| SerdeMbArtistRef(mb)),
properties: artist properties: artist
.properties .properties
.iter() .iter()
@ -57,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| mb.url().as_str()), musicbrainz: album.musicbrainz.as_ref().map(|mb| SerdeMbAlbumRef(mb)),
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