Commonise album lib id serde code

This commit is contained in:
Wojciech Kozlowski 2025-01-03 08:50:31 +01:00
parent 11785ffa1e
commit d765b3da73
3 changed files with 21 additions and 27 deletions

View File

@ -13,6 +13,21 @@ pub enum AlbumLibIdDef {
None,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SerdeAlbumLibId(#[serde(with = "AlbumLibIdDef")] AlbumLibId);
impl From<SerdeAlbumLibId> for AlbumLibId {
fn from(value: SerdeAlbumLibId) -> Self {
value.0
}
}
impl From<AlbumLibId> for SerdeAlbumLibId {
fn from(value: AlbumLibId) -> Self {
SerdeAlbumLibId(value)
}
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(remote = "MbRefOption")]
pub enum MbRefOptionDef<T> {

View File

@ -4,7 +4,7 @@ use serde::{de::Visitor, Deserialize, Deserializer};
use crate::{
collection::{
album::{AlbumInfo, AlbumLibId, AlbumMeta},
album::{AlbumInfo, AlbumMeta},
artist::{ArtistInfo, ArtistMeta},
musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption, Mbid},
},
@ -14,7 +14,7 @@ use crate::{
Collection, Error as CollectionError,
},
external::database::serde::common::{
AlbumLibIdDef, MbRefOptionDef, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType,
MbRefOptionDef, SerdeAlbumLibId, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType,
},
};
@ -45,22 +45,13 @@ pub struct DeserializeArtist {
#[derive(Debug, Deserialize)]
pub struct DeserializeAlbum {
title: String,
lib_id: DeserializeAlbumLibId,
lib_id: SerdeAlbumLibId,
seq: u8,
musicbrainz: DeserializeMbRefOption,
primary_type: Option<SerdeAlbumPrimaryType>,
secondary_types: Vec<SerdeAlbumSecondaryType>,
}
#[derive(Debug, Deserialize)]
pub struct DeserializeAlbumLibId(#[serde(with = "AlbumLibIdDef")] AlbumLibId);
impl From<DeserializeAlbumLibId> for AlbumLibId {
fn from(value: DeserializeAlbumLibId) -> Self {
value.0
}
}
#[derive(Debug, Deserialize)]
pub struct DeserializeMbRefOption(#[serde(with = "MbRefOptionDef")] MbRefOption<DeserializeMbid>);

View File

@ -3,13 +3,10 @@ use std::collections::BTreeMap;
use serde::Serialize;
use crate::{
collection::{
album::AlbumLibId,
musicbrainz::{MbRefOption, Mbid},
},
collection::musicbrainz::{MbRefOption, Mbid},
core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection},
external::database::serde::common::{
AlbumLibIdDef, MbRefOptionDef, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType,
MbRefOptionDef, SerdeAlbumLibId, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType,
},
};
@ -36,22 +33,13 @@ pub struct SerializeArtist<'a> {
#[derive(Debug, Serialize)]
pub struct SerializeAlbum<'a> {
title: &'a str,
lib_id: SerializeAlbumLibId,
lib_id: SerdeAlbumLibId,
seq: u8,
musicbrainz: SerializeMbRefOption<'a>,
primary_type: Option<SerdeAlbumPrimaryType>,
secondary_types: Vec<SerdeAlbumSecondaryType>,
}
#[derive(Debug, Serialize)]
pub struct SerializeAlbumLibId(#[serde(with = "AlbumLibIdDef")] AlbumLibId);
impl From<AlbumLibId> for SerializeAlbumLibId {
fn from(value: AlbumLibId) -> Self {
SerializeAlbumLibId(value)
}
}
#[derive(Debug, Serialize)]
pub struct SerializeMbRefOption<'a>(
#[serde(with = "MbRefOptionDef")] MbRefOption<SerializeMbid<'a>>,