From 36106a2c83b123af5dccd472fddcf1f79fd2be1e Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Fri, 3 Jan 2025 09:04:09 +0100 Subject: [PATCH] Add date to the database entries --- src/core/collection/album.rs | 5 +++- src/external/database/serde/common.rs | 29 +++++++++++++++++++--- src/external/database/serde/deserialize.rs | 20 +++++++-------- src/external/database/serde/serialize.rs | 9 ++++--- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/core/collection/album.rs b/src/core/collection/album.rs index 0d3fd74..d8bc3cd 100644 --- a/src/core/collection/album.rs +++ b/src/core/collection/album.rs @@ -65,7 +65,7 @@ impl MergeName for Album { // There are crates for handling dates, but we don't need much complexity beyond year-month-day. /// The album's release date. -#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] pub struct AlbumDate { pub year: Option, pub month: Option, @@ -271,6 +271,9 @@ impl Merge for AlbumMeta { fn merge_in_place(&mut self, other: Self) { assert!(self.id.compatible(&other.id)); self.id.mb_ref = self.id.mb_ref.take().or(other.id.mb_ref); + if self.date.year.is_none() && other.date.year.is_some() { + self.date = other.date; + } self.seq = std::cmp::max(self.seq, other.seq); self.info.merge_in_place(other.info); } diff --git a/src/external/database/serde/common.rs b/src/external/database/serde/common.rs index ff3e01f..1cc230e 100644 --- a/src/external/database/serde/common.rs +++ b/src/external/database/serde/common.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; -use crate::{ - collection::musicbrainz::MbRefOption, - core::collection::album::{AlbumLibId, AlbumPrimaryType, AlbumSecondaryType}, +use crate::core::collection::{ + album::{AlbumDate, AlbumLibId, AlbumPrimaryType, AlbumSecondaryType}, + musicbrainz::MbRefOption, }; #[derive(Debug, Deserialize, Serialize)] @@ -28,6 +28,29 @@ impl From for SerdeAlbumLibId { } } +#[derive(Debug, Deserialize, Serialize)] +#[serde(remote = "AlbumDate")] +pub struct AlbumDateDef { + year: Option, + month: Option, + day: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct SerdeAlbumDate(#[serde(with = "AlbumDateDef")] AlbumDate); + +impl From for AlbumDate { + fn from(value: SerdeAlbumDate) -> Self { + value.0 + } +} + +impl From for SerdeAlbumDate { + fn from(value: AlbumDate) -> Self { + SerdeAlbumDate(value) + } +} + #[derive(Debug, Deserialize, Serialize)] #[serde(remote = "MbRefOption")] pub enum MbRefOptionDef { diff --git a/src/external/database/serde/deserialize.rs b/src/external/database/serde/deserialize.rs index 4a361ab..182c9c6 100644 --- a/src/external/database/serde/deserialize.rs +++ b/src/external/database/serde/deserialize.rs @@ -3,30 +3,27 @@ use std::{collections::HashMap, fmt}; use serde::{de::Visitor, Deserialize, Deserializer}; use crate::{ - collection::{ - album::{AlbumInfo, AlbumMeta}, - artist::{ArtistInfo, ArtistMeta}, - musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption, Mbid}, - }, core::collection::{ - album::{Album, AlbumDate, AlbumId, AlbumSeq}, - artist::{Artist, ArtistId}, + album::{Album, AlbumId, AlbumInfo, AlbumMeta, AlbumSeq}, + artist::{Artist, ArtistId, ArtistInfo, ArtistMeta}, + musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption, Mbid}, Collection, Error as CollectionError, }, external::database::serde::common::{ - MbRefOptionDef, SerdeAlbumLibId, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType, + MbRefOptionDef, SerdeAlbumDate, SerdeAlbumLibId, SerdeAlbumPrimaryType, + SerdeAlbumSecondaryType, }, }; #[derive(Debug, Deserialize)] pub enum DeserializeDatabase { - V20250101(Vec), + V20250103(Vec), } impl From for Collection { fn from(database: DeserializeDatabase) -> Self { match database { - DeserializeDatabase::V20250101(collection) => { + DeserializeDatabase::V20250103(collection) => { collection.into_iter().map(Into::into).collect() } } @@ -46,6 +43,7 @@ pub struct DeserializeArtist { pub struct DeserializeAlbum { title: String, lib_id: SerdeAlbumLibId, + date: SerdeAlbumDate, seq: u8, musicbrainz: DeserializeMbRefOption, primary_type: Option, @@ -137,7 +135,7 @@ impl From for Album { lib_id: album.lib_id.into(), mb_ref: album.musicbrainz.into(), }, - date: AlbumDate::default(), + date: album.date.into(), seq: AlbumSeq(album.seq), info: AlbumInfo { primary_type: album.primary_type.map(Into::into), diff --git a/src/external/database/serde/serialize.rs b/src/external/database/serde/serialize.rs index 70ab598..a88657f 100644 --- a/src/external/database/serde/serialize.rs +++ b/src/external/database/serde/serialize.rs @@ -6,18 +6,19 @@ use crate::{ collection::musicbrainz::{MbRefOption, Mbid}, core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection}, external::database::serde::common::{ - MbRefOptionDef, SerdeAlbumLibId, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType, + MbRefOptionDef, SerdeAlbumDate, SerdeAlbumLibId, SerdeAlbumPrimaryType, + SerdeAlbumSecondaryType, }, }; #[derive(Debug, Serialize)] pub enum SerializeDatabase<'a> { - V20250101(Vec>), + V20250103(Vec>), } impl<'a> From<&'a Collection> for SerializeDatabase<'a> { fn from(collection: &'a Collection) -> Self { - SerializeDatabase::V20250101(collection.iter().map(Into::into).collect()) + SerializeDatabase::V20250103(collection.iter().map(Into::into).collect()) } } @@ -34,6 +35,7 @@ pub struct SerializeArtist<'a> { pub struct SerializeAlbum<'a> { title: &'a str, lib_id: SerdeAlbumLibId, + date: SerdeAlbumDate, seq: u8, musicbrainz: SerializeMbRefOption<'a>, primary_type: Option, @@ -92,6 +94,7 @@ impl<'a> From<&'a Album> for SerializeAlbum<'a> { SerializeAlbum { title: &album.meta.id.title, lib_id: album.meta.id.lib_id.into(), + date: album.meta.date.into(), seq: album.meta.seq.0, musicbrainz: (&album.meta.id.mb_ref).into(), primary_type: album.meta.info.primary_type.map(Into::into),