For the database serde implementation use Mbid rather than MbRef #199

Merged
wojtek merged 3 commits from 198---for-the-database-serde-implementation-use-mbid-rather-than-mbref into main 2024-08-29 13:37:48 +02:00
15 changed files with 61 additions and 90 deletions
Showing only changes of commit ccaee6d893 - Show all commits

View File

@ -1,12 +1,9 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use musichoard::{ use musichoard::{collection::musicbrainz::Mbid, external::musicbrainz::{
external::musicbrainz::{
api::{lookup::LookupArtistRequest, MusicBrainzClient}, api::{lookup::LookupArtistRequest, MusicBrainzClient},
http::MusicBrainzHttp, http::MusicBrainzHttp,
}, }};
interface::musicbrainz::Mbid,
};
use structopt::StructOpt; use structopt::StructOpt;
use uuid::Uuid; use uuid::Uuid;

View File

@ -3,11 +3,10 @@
use std::{num::ParseIntError, str::FromStr}; use std::{num::ParseIntError, str::FromStr};
use musichoard::{ use musichoard::{
collection::album::AlbumDate, collection::{album::AlbumDate, musicbrainz::Mbid},
external::musicbrainz::{ external::musicbrainz::{
api::search::SearchReleaseGroupRequest, api::MusicBrainzClient, http::MusicBrainzHttp, api::{search::SearchReleaseGroupRequest, MusicBrainzClient}, http::MusicBrainzHttp,
}, },
interface::musicbrainz::Mbid,
}; };
use structopt::StructOpt; use structopt::StructOpt;
use uuid::Uuid; use uuid::Uuid;

View File

@ -16,6 +16,8 @@ pub type Collection = Vec<artist::Artist>;
/// Error type for the [`collection`] module. /// Error type for the [`collection`] module.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum Error { pub enum Error {
/// An error occurred when processing an MBID.
MbidError(String),
/// An error occurred when processing a URL. /// An error occurred when processing a URL.
UrlError(String), UrlError(String),
} }
@ -23,6 +25,7 @@ pub enum Error {
impl Display for Error { impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self { match *self {
Self::MbidError(ref s) => write!(f, "an error occurred when processing an MBID: {s}"),
Self::UrlError(ref s) => write!(f, "an error occurred when processing a URL: {s}"), Self::UrlError(ref s) => write!(f, "an error occurred when processing a URL: {s}"),
} }
} }
@ -36,6 +39,6 @@ impl From<url::ParseError> for Error {
impl From<uuid::Error> for Error { impl From<uuid::Error> for Error {
fn from(err: uuid::Error) -> Error { fn from(err: uuid::Error) -> Error {
Error::UrlError(err.to_string()) Error::MbidError(err.to_string())
} }
} }

View File

@ -3,10 +3,41 @@ use std::fmt::{Debug, Display};
use url::Url; use url::Url;
use uuid::Uuid; use uuid::Uuid;
use crate::{core::collection::Error, interface::musicbrainz::Mbid}; use crate::core::collection::Error;
const MB_DOMAIN: &str = "musicbrainz.org"; const MB_DOMAIN: &str = "musicbrainz.org";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mbid(Uuid);
impl Mbid {
pub fn uuid(&self) -> &Uuid {
&self.0
}
}
impl From<Uuid> for Mbid {
fn from(value: Uuid) -> Self {
Mbid(value)
}
}
macro_rules! try_from_impl_for_mbid {
($from:ty) => {
impl TryFrom<$from> for Mbid {
type Error = Error;
fn try_from(value: $from) -> Result<Self, Self::Error> {
Ok(Uuid::parse_str(value.as_ref())?.into())
}
}
};
}
try_from_impl_for_mbid!(&str);
try_from_impl_for_mbid!(&String);
try_from_impl_for_mbid!(String);
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
struct MusicBrainzRef { struct MusicBrainzRef {
mbid: Mbid, mbid: Mbid,

View File

@ -59,7 +59,9 @@ impl From<std::io::Error> for LoadError {
impl From<collection::Error> for LoadError { impl From<collection::Error> for LoadError {
fn from(err: collection::Error) -> Self { fn from(err: collection::Error) -> Self {
match err { match err {
collection::Error::UrlError(e) => LoadError::SerDeError(e), collection::Error::UrlError(e) | collection::Error::MbidError(e) => {
LoadError::SerDeError(e)
}
} }
} }
} }

View File

@ -1,3 +1,2 @@
pub mod database; pub mod database;
pub mod library; pub mod library;
pub mod musicbrainz;

View File

@ -1,56 +0,0 @@
use std::fmt;
use uuid::{self, Uuid};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mbid(Uuid);
impl Mbid {
pub fn uuid(&self) -> &Uuid {
&self.0
}
}
impl From<Uuid> for Mbid {
fn from(value: Uuid) -> Self {
Mbid(value)
}
}
#[derive(Debug)]
pub struct MbidError(String);
impl fmt::Display for MbidError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to parse a MBID: {}", self.0)
}
}
impl From<uuid::Error> for MbidError {
fn from(value: uuid::Error) -> Self {
MbidError(value.to_string())
}
}
macro_rules! try_from_impl_for_mbid {
($from:ty) => {
impl TryFrom<$from> for Mbid {
type Error = MbidError;
fn try_from(value: $from) -> Result<Self, Self::Error> {
Ok(Uuid::parse_str(value.as_ref())?.into())
}
}
};
}
try_from_impl_for_mbid!(&str);
try_from_impl_for_mbid!(&String);
try_from_impl_for_mbid!(String);
#[test]
fn errors() {
let mbid_err: MbidError = TryInto::<Mbid>::try_into("i-am-not-a-uuid").unwrap_err();
assert!(!mbid_err.to_string().is_empty());
assert!(!format!("{mbid_err:?}").is_empty());
}

View File

@ -3,13 +3,13 @@ use std::{collections::HashMap, fmt};
use serde::{de::Visitor, Deserialize, Deserializer}; use serde::{de::Visitor, Deserialize, Deserializer};
use crate::{ use crate::{
collection::musicbrainz::Mbid,
core::collection::{ core::collection::{
album::{Album, AlbumDate, AlbumId, AlbumSeq}, album::{Album, AlbumDate, AlbumId, AlbumSeq},
artist::{Artist, ArtistId}, artist::{Artist, ArtistId},
Collection, Collection, Error as CollectionError,
}, },
external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType}, external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType},
interface::musicbrainz::{Mbid, MbidError},
}; };
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -69,7 +69,7 @@ impl<'de> Visitor<'de> for DeserializeMbidVisitor {
{ {
Ok(DeserializeMbid( Ok(DeserializeMbid(
v.try_into() v.try_into()
.map_err(|e: MbidError| E::custom(e.to_string()))?, .map_err(|e: CollectionError| E::custom(e.to_string()))?,
)) ))
} }
} }

View File

@ -3,9 +3,7 @@ use std::collections::BTreeMap;
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{
core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection}, collection::musicbrainz::Mbid, core::collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection}, external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType}
external::database::serde::common::{SerdeAlbumPrimaryType, SerdeAlbumSecondaryType},
interface::musicbrainz::Mbid,
}; };
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]

View File

@ -2,7 +2,7 @@ use serde::Deserialize;
use url::form_urlencoded; use url::form_urlencoded;
use crate::{ use crate::{
collection::album::{AlbumDate, AlbumPrimaryType, AlbumSecondaryType}, collection::{album::{AlbumDate, AlbumPrimaryType, AlbumSecondaryType}, musicbrainz::Mbid},
external::musicbrainz::{ external::musicbrainz::{
api::{ api::{
Error, MusicBrainzClient, SerdeAlbumDate, SerdeAlbumPrimaryType, Error, MusicBrainzClient, SerdeAlbumDate, SerdeAlbumPrimaryType,
@ -10,7 +10,6 @@ use crate::{
}, },
IMusicBrainzHttp, IMusicBrainzHttp,
}, },
interface::musicbrainz::Mbid,
}; };
impl<Http: IMusicBrainzHttp> MusicBrainzClient<Http> { impl<Http: IMusicBrainzHttp> MusicBrainzClient<Http> {

View File

@ -3,9 +3,12 @@ use std::{fmt, num};
use serde::{de::Visitor, Deserialize, Deserializer}; use serde::{de::Visitor, Deserialize, Deserializer};
use crate::{ use crate::{
collection::album::{AlbumDate, AlbumPrimaryType, AlbumSecondaryType}, collection::{
album::{AlbumDate, AlbumPrimaryType, AlbumSecondaryType},
musicbrainz::Mbid,
Error as CollectionError,
},
external::musicbrainz::HttpError, external::musicbrainz::HttpError,
interface::musicbrainz::{Mbid, MbidError},
}; };
pub mod lookup; pub mod lookup;
@ -92,7 +95,7 @@ impl<'de> Visitor<'de> for SerdeMbidVisitor {
{ {
Ok(SerdeMbid( Ok(SerdeMbid(
v.try_into() v.try_into()
.map_err(|e: MbidError| E::custom(e.to_string()))?, .map_err(|e: CollectionError| E::custom(e.to_string()))?,
)) ))
} }
} }

View File

@ -4,11 +4,8 @@ use serde::Deserialize;
use url::form_urlencoded; use url::form_urlencoded;
use crate::{ use crate::{
collection::album::AlbumDate, collection::{album::AlbumDate, musicbrainz::Mbid},
core::{ core::collection::album::{AlbumPrimaryType, AlbumSecondaryType},
collection::album::{AlbumPrimaryType, AlbumSecondaryType},
interface::musicbrainz::Mbid,
},
external::musicbrainz::{ external::musicbrainz::{
api::{ api::{
Error, MusicBrainzClient, SerdeAlbumDate, SerdeAlbumPrimaryType, Error, MusicBrainzClient, SerdeAlbumDate, SerdeAlbumPrimaryType,

View File

@ -129,7 +129,7 @@ impl IAppInteractBrowse for AppMachine<AppBrowse> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use mockall::{predicate, Sequence}; use mockall::{predicate, Sequence};
use musichoard::{collection::album::Album, interface::musicbrainz::Mbid}; use musichoard::collection::{album::Album, musicbrainz::Mbid};
use crate::tui::{ use crate::tui::{
app::{ app::{

View File

@ -1,7 +1,7 @@
//! Module for interacting with the [MusicBrainz API](https://musicbrainz.org/doc/MusicBrainz_API). //! Module for interacting with the [MusicBrainz API](https://musicbrainz.org/doc/MusicBrainz_API).
use musichoard::{ use musichoard::{
collection::album::{Album, AlbumDate}, collection::{album::{Album, AlbumDate}, musicbrainz::Mbid},
external::musicbrainz::{ external::musicbrainz::{
api::{ api::{
search::{SearchReleaseGroupRequest, SearchReleaseGroupResponseReleaseGroup}, search::{SearchReleaseGroupRequest, SearchReleaseGroupResponseReleaseGroup},
@ -9,7 +9,6 @@ use musichoard::{
}, },
IMusicBrainzHttp, IMusicBrainzHttp,
}, },
interface::musicbrainz::Mbid,
}; };
use crate::tui::lib::interface::musicbrainz::{Error, IMusicBrainz, Match}; use crate::tui::lib::interface::musicbrainz::{Error, IMusicBrainz, Match};

View File

@ -3,7 +3,7 @@
#[cfg(test)] #[cfg(test)]
use mockall::automock; use mockall::automock;
use musichoard::{collection::album::Album, interface::musicbrainz::Mbid}; use musichoard::collection::{album::Album, musicbrainz::Mbid};
/// Trait for interacting with the MusicBrainz API. /// Trait for interacting with the MusicBrainz API.
#[cfg_attr(test, automock)] #[cfg_attr(test, automock)]