Add date to the database entries
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 1m33s
Cargo CI / Lint (pull_request) Failing after 31s

This commit is contained in:
Wojciech Kozlowski 2025-01-03 09:04:09 +01:00
parent d765b3da73
commit 36106a2c83
4 changed files with 45 additions and 18 deletions

View File

@ -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. // There are crates for handling dates, but we don't need much complexity beyond year-month-day.
/// The album's release date. /// 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 struct AlbumDate {
pub year: Option<u32>, pub year: Option<u32>,
pub month: Option<u8>, pub month: Option<u8>,
@ -271,6 +271,9 @@ impl Merge for AlbumMeta {
fn merge_in_place(&mut self, other: Self) { fn merge_in_place(&mut self, other: Self) {
assert!(self.id.compatible(&other.id)); assert!(self.id.compatible(&other.id));
self.id.mb_ref = self.id.mb_ref.take().or(other.id.mb_ref); 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.seq = std::cmp::max(self.seq, other.seq);
self.info.merge_in_place(other.info); self.info.merge_in_place(other.info);
} }

View File

@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::core::collection::{
collection::musicbrainz::MbRefOption, album::{AlbumDate, AlbumLibId, AlbumPrimaryType, AlbumSecondaryType},
core::collection::album::{AlbumLibId, AlbumPrimaryType, AlbumSecondaryType}, musicbrainz::MbRefOption,
}; };
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
@ -28,6 +28,29 @@ impl From<AlbumLibId> for SerdeAlbumLibId {
} }
} }
#[derive(Debug, Deserialize, Serialize)]
#[serde(remote = "AlbumDate")]
pub struct AlbumDateDef {
year: Option<u32>,
month: Option<u8>,
day: Option<u8>,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct SerdeAlbumDate(#[serde(with = "AlbumDateDef")] AlbumDate);
impl From<SerdeAlbumDate> for AlbumDate {
fn from(value: SerdeAlbumDate) -> Self {
value.0
}
}
impl From<AlbumDate> for SerdeAlbumDate {
fn from(value: AlbumDate) -> Self {
SerdeAlbumDate(value)
}
}
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(remote = "MbRefOption")] #[serde(remote = "MbRefOption")]
pub enum MbRefOptionDef<T> { pub enum MbRefOptionDef<T> {

View File

@ -3,30 +3,27 @@ use std::{collections::HashMap, fmt};
use serde::{de::Visitor, Deserialize, Deserializer}; use serde::{de::Visitor, Deserialize, Deserializer};
use crate::{ use crate::{
collection::{
album::{AlbumInfo, AlbumMeta},
artist::{ArtistInfo, ArtistMeta},
musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption, Mbid},
},
core::collection::{ core::collection::{
album::{Album, AlbumDate, AlbumId, AlbumSeq}, album::{Album, AlbumId, AlbumInfo, AlbumMeta, AlbumSeq},
artist::{Artist, ArtistId}, artist::{Artist, ArtistId, ArtistInfo, ArtistMeta},
musicbrainz::{MbAlbumRef, MbArtistRef, MbRefOption, Mbid},
Collection, Error as CollectionError, Collection, Error as CollectionError,
}, },
external::database::serde::common::{ external::database::serde::common::{
MbRefOptionDef, SerdeAlbumLibId, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType, MbRefOptionDef, SerdeAlbumDate, SerdeAlbumLibId, SerdeAlbumPrimaryType,
SerdeAlbumSecondaryType,
}, },
}; };
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub enum DeserializeDatabase { pub enum DeserializeDatabase {
V20250101(Vec<DeserializeArtist>), V20250103(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::V20250101(collection) => { DeserializeDatabase::V20250103(collection) => {
collection.into_iter().map(Into::into).collect() collection.into_iter().map(Into::into).collect()
} }
} }
@ -46,6 +43,7 @@ pub struct DeserializeArtist {
pub struct DeserializeAlbum { pub struct DeserializeAlbum {
title: String, title: String,
lib_id: SerdeAlbumLibId, lib_id: SerdeAlbumLibId,
date: SerdeAlbumDate,
seq: u8, seq: u8,
musicbrainz: DeserializeMbRefOption, musicbrainz: DeserializeMbRefOption,
primary_type: Option<SerdeAlbumPrimaryType>, primary_type: Option<SerdeAlbumPrimaryType>,
@ -137,7 +135,7 @@ impl From<DeserializeAlbum> for Album {
lib_id: album.lib_id.into(), lib_id: album.lib_id.into(),
mb_ref: album.musicbrainz.into(), mb_ref: album.musicbrainz.into(),
}, },
date: AlbumDate::default(), date: album.date.into(),
seq: AlbumSeq(album.seq), seq: AlbumSeq(album.seq),
info: AlbumInfo { info: AlbumInfo {
primary_type: album.primary_type.map(Into::into), primary_type: album.primary_type.map(Into::into),

View File

@ -6,18 +6,19 @@ use crate::{
collection::musicbrainz::{MbRefOption, Mbid}, collection::musicbrainz::{MbRefOption, Mbid},
core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection}, core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection},
external::database::serde::common::{ external::database::serde::common::{
MbRefOptionDef, SerdeAlbumLibId, SerdeAlbumPrimaryType, SerdeAlbumSecondaryType, MbRefOptionDef, SerdeAlbumDate, SerdeAlbumLibId, SerdeAlbumPrimaryType,
SerdeAlbumSecondaryType,
}, },
}; };
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub enum SerializeDatabase<'a> { pub enum SerializeDatabase<'a> {
V20250101(Vec<SerializeArtist<'a>>), V20250103(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::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> { pub struct SerializeAlbum<'a> {
title: &'a str, title: &'a str,
lib_id: SerdeAlbumLibId, lib_id: SerdeAlbumLibId,
date: SerdeAlbumDate,
seq: u8, seq: u8,
musicbrainz: SerializeMbRefOption<'a>, musicbrainz: SerializeMbRefOption<'a>,
primary_type: Option<SerdeAlbumPrimaryType>, primary_type: Option<SerdeAlbumPrimaryType>,
@ -92,6 +94,7 @@ impl<'a> From<&'a Album> for SerializeAlbum<'a> {
SerializeAlbum { SerializeAlbum {
title: &album.meta.id.title, title: &album.meta.id.title,
lib_id: album.meta.id.lib_id.into(), lib_id: album.meta.id.lib_id.into(),
date: album.meta.date.into(),
seq: album.meta.seq.0, seq: album.meta.seq.0,
musicbrainz: (&album.meta.id.mb_ref).into(), musicbrainz: (&album.meta.id.mb_ref).into(),
primary_type: album.meta.info.primary_type.map(Into::into), primary_type: album.meta.info.primary_type.map(Into::into),