From f1a02e8e58fefddf2d993e499f164ada0580e390 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Wed, 28 Aug 2024 22:25:47 +0200 Subject: [PATCH] Serialize side done --- src/external/database/serde/common.rs | 10 +++---- src/external/database/serde/serialize.rs | 33 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/external/database/serde/common.rs b/src/external/database/serde/common.rs index 28f9c65..b0268d9 100644 --- a/src/external/database/serde/common.rs +++ b/src/external/database/serde/common.rs @@ -4,7 +4,7 @@ use crate::core::collection::album::{AlbumPrimaryType, AlbumSecondaryType}; #[derive(Debug, Deserialize, Serialize)] #[serde(remote = "AlbumPrimaryType")] -pub enum SerdeAlbumPrimaryTypeDef { +pub enum AlbumPrimaryTypeDef { Album, Single, Ep, @@ -13,7 +13,7 @@ pub enum SerdeAlbumPrimaryTypeDef { } #[derive(Debug, Deserialize, Serialize)] -pub struct SerdeAlbumPrimaryType(#[serde(with = "SerdeAlbumPrimaryTypeDef")] AlbumPrimaryType); +pub struct SerdeAlbumPrimaryType(#[serde(with = "AlbumPrimaryTypeDef")] AlbumPrimaryType); impl From for AlbumPrimaryType { fn from(value: SerdeAlbumPrimaryType) -> Self { @@ -29,7 +29,7 @@ impl From for SerdeAlbumPrimaryType { #[derive(Debug, Deserialize, Serialize)] #[serde(remote = "AlbumSecondaryType")] -pub enum SerdeAlbumSecondaryTypeDef { +pub enum AlbumSecondaryTypeDef { Compilation, Soundtrack, Spokenword, @@ -45,9 +45,7 @@ pub enum SerdeAlbumSecondaryTypeDef { } #[derive(Debug, Deserialize, Serialize)] -pub struct SerdeAlbumSecondaryType( - #[serde(with = "SerdeAlbumSecondaryTypeDef")] AlbumSecondaryType, -); +pub struct SerdeAlbumSecondaryType(#[serde(with = "AlbumSecondaryTypeDef")] AlbumSecondaryType); impl From for AlbumSecondaryType { fn from(value: SerdeAlbumSecondaryType) -> Self { diff --git a/src/external/database/serde/serialize.rs b/src/external/database/serde/serialize.rs index acfe155..e54fda0 100644 --- a/src/external/database/serde/serialize.rs +++ b/src/external/database/serde/serialize.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use serde::Serialize; use crate::{ + collection::musicbrainz::{MbAlbumRef, MbArtistRef}, core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection}, external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType}, }; @@ -22,7 +23,7 @@ impl<'a> From<&'a Collection> for SerializeDatabase<'a> { pub struct SerializeArtist<'a> { name: &'a str, sort: Option<&'a str>, - musicbrainz: Option<&'a str>, + musicbrainz: Option>, properties: BTreeMap<&'a str, &'a Vec>, albums: Vec>, } @@ -31,17 +32,41 @@ pub struct SerializeArtist<'a> { pub struct SerializeAlbum<'a> { title: &'a str, seq: u8, - musicbrainz: Option<&'a str>, + musicbrainz: Option>, primary_type: Option, secondary_types: Vec, } +#[derive(Clone, Debug)] +pub struct SerdeMbArtistRef<'a>(&'a MbArtistRef); + +impl<'a> Serialize for SerdeMbArtistRef<'a> { + fn serialize(&self, serializer: S) -> Result + 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(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.0.url().as_str()) + } +} + impl<'a> From<&'a Artist> for SerializeArtist<'a> { fn from(artist: &'a Artist) -> Self { SerializeArtist { name: &artist.id.name, 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 .iter() @@ -57,7 +82,7 @@ impl<'a> From<&'a Album> for SerializeAlbum<'a> { SerializeAlbum { title: &album.id.title, 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), secondary_types: album .secondary_types