Rename MusicBrainz -> MusicBrainzUrl
This commit is contained in:
parent
8550f7d6da
commit
90225d9b85
@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use crate::core::collection::{
|
||||
merge::{Merge, MergeSorted, WithId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
track::{Track, TrackFormat},
|
||||
};
|
||||
|
||||
@ -15,7 +15,7 @@ pub struct Album {
|
||||
pub id: AlbumId,
|
||||
pub date: AlbumDate,
|
||||
pub seq: AlbumSeq,
|
||||
pub musicbrainz: Option<MusicBrainz>,
|
||||
pub musicbrainz: Option<MusicBrainzUrl>,
|
||||
pub tracks: Vec<Track>,
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ use std::{
|
||||
use crate::core::collection::{
|
||||
album::Album,
|
||||
merge::{Merge, MergeCollections, WithId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
};
|
||||
|
||||
/// An artist.
|
||||
@ -15,7 +15,7 @@ use crate::core::collection::{
|
||||
pub struct Artist {
|
||||
pub id: ArtistId,
|
||||
pub sort: Option<ArtistId>,
|
||||
pub musicbrainz: Option<MusicBrainz>,
|
||||
pub musicbrainz: Option<MusicBrainzUrl>,
|
||||
pub properties: HashMap<String, Vec<String>>,
|
||||
pub albums: Vec<Album>,
|
||||
}
|
||||
@ -58,7 +58,7 @@ impl Artist {
|
||||
_ = self.sort.take();
|
||||
}
|
||||
|
||||
pub fn set_musicbrainz_url(&mut self, url: MusicBrainz) {
|
||||
pub fn set_musicbrainz_url(&mut self, url: MusicBrainzUrl) {
|
||||
_ = self.musicbrainz.insert(url);
|
||||
}
|
||||
|
||||
@ -216,19 +216,19 @@ mod tests {
|
||||
fn set_clear_musicbrainz_url() {
|
||||
let mut artist = Artist::new(ArtistId::new("an artist"));
|
||||
|
||||
let mut expected: Option<MusicBrainz> = None;
|
||||
let mut expected: Option<MusicBrainzUrl> = None;
|
||||
assert_eq!(artist.musicbrainz, expected);
|
||||
|
||||
// Setting a URL on an artist.
|
||||
artist.set_musicbrainz_url(MusicBrainz::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
_ = expected.insert(MusicBrainz::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
artist.set_musicbrainz_url(MusicBrainzUrl::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
_ = expected.insert(MusicBrainzUrl::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
assert_eq!(artist.musicbrainz, expected);
|
||||
|
||||
artist.set_musicbrainz_url(MusicBrainz::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
artist.set_musicbrainz_url(MusicBrainzUrl::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
assert_eq!(artist.musicbrainz, expected);
|
||||
|
||||
artist.set_musicbrainz_url(MusicBrainz::artist_from_str(MUSICBRAINZ_2).unwrap());
|
||||
_ = expected.insert(MusicBrainz::artist_from_str(MUSICBRAINZ_2).unwrap());
|
||||
artist.set_musicbrainz_url(MusicBrainzUrl::artist_from_str(MUSICBRAINZ_2).unwrap());
|
||||
_ = expected.insert(MusicBrainzUrl::artist_from_str(MUSICBRAINZ_2).unwrap());
|
||||
assert_eq!(artist.musicbrainz, expected);
|
||||
|
||||
// Clearing URLs.
|
||||
|
@ -5,17 +5,16 @@ use uuid::Uuid;
|
||||
|
||||
use crate::core::collection::Error;
|
||||
|
||||
/// An object with the [`IMbid`] trait contains a [MusicBrainz
|
||||
/// Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).
|
||||
pub trait IMbid {
|
||||
fn mbid(&self) -> &str;
|
||||
}
|
||||
|
||||
/// MusicBrainz reference.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct MusicBrainz(Url);
|
||||
pub struct MusicBrainzUrl(Url);
|
||||
|
||||
impl MusicBrainzUrl {
|
||||
pub fn mbid(&self) -> &str {
|
||||
// The URL is assumed to have been validated.
|
||||
self.0.path_segments().and_then(|mut ps| ps.nth(1)).unwrap()
|
||||
}
|
||||
|
||||
impl MusicBrainz {
|
||||
pub fn artist_from_str<S: AsRef<str>>(url: S) -> Result<Self, Error> {
|
||||
Self::artist_from_url(url.as_ref().try_into()?)
|
||||
}
|
||||
@ -52,7 +51,7 @@ impl MusicBrainz {
|
||||
None => return Err(Self::invalid_url_error(url, mb_type)),
|
||||
};
|
||||
|
||||
Ok(MusicBrainz(url))
|
||||
Ok(MusicBrainzUrl(url))
|
||||
}
|
||||
|
||||
fn invalid_url_error<U: Display>(url: U, mb_type: &str) -> Error {
|
||||
@ -60,19 +59,12 @@ impl MusicBrainz {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for MusicBrainz {
|
||||
impl AsRef<str> for MusicBrainzUrl {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl IMbid for MusicBrainz {
|
||||
fn mbid(&self) -> &str {
|
||||
// The URL is assumed to have been validated.
|
||||
self.0.path_segments().and_then(|mut ps| ps.nth(1)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@ -82,12 +74,12 @@ mod tests {
|
||||
let uuid = "d368baa8-21ca-4759-9731-0b2753071ad8";
|
||||
let url_str = format!("https://musicbrainz.org/artist/{uuid}");
|
||||
|
||||
let mb = MusicBrainz::artist_from_str(&url_str).unwrap();
|
||||
let mb = MusicBrainzUrl::artist_from_str(&url_str).unwrap();
|
||||
assert_eq!(url_str, mb.as_ref());
|
||||
assert_eq!(uuid, mb.mbid());
|
||||
|
||||
let url: Url = url_str.as_str().try_into().unwrap();
|
||||
let mb = MusicBrainz::artist_from_url(url).unwrap();
|
||||
let mb = MusicBrainzUrl::artist_from_url(url).unwrap();
|
||||
assert_eq!(url_str, mb.as_ref());
|
||||
assert_eq!(uuid, mb.mbid());
|
||||
}
|
||||
@ -97,12 +89,12 @@ mod tests {
|
||||
let uuid = "d368baa8-21ca-4759-9731-0b2753071ad8";
|
||||
let url_str = format!("https://musicbrainz.org/release-group/{uuid}");
|
||||
|
||||
let mb = MusicBrainz::album_from_str(&url_str).unwrap();
|
||||
let mb = MusicBrainzUrl::album_from_str(&url_str).unwrap();
|
||||
assert_eq!(url_str, mb.as_ref());
|
||||
assert_eq!(uuid, mb.mbid());
|
||||
|
||||
let url: Url = url_str.as_str().try_into().unwrap();
|
||||
let mb = MusicBrainz::album_from_url(url).unwrap();
|
||||
let mb = MusicBrainzUrl::album_from_url(url).unwrap();
|
||||
assert_eq!(url_str, mb.as_ref());
|
||||
assert_eq!(uuid, mb.mbid());
|
||||
}
|
||||
@ -111,7 +103,7 @@ mod tests {
|
||||
fn not_a_url() {
|
||||
let url = "not a url at all";
|
||||
let expected_error: Error = url::ParseError::RelativeUrlWithoutBase.into();
|
||||
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::artist_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
@ -120,7 +112,7 @@ mod tests {
|
||||
fn invalid_url() {
|
||||
let url = "https://www.musicbutler.io/artist-page/483340948";
|
||||
let expected_error = Error::UrlError(format!("invalid artist MusicBrainz URL: {url}"));
|
||||
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::artist_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
@ -129,7 +121,7 @@ mod tests {
|
||||
fn artist_invalid_type() {
|
||||
let url = "https://musicbrainz.org/release-group/i-am-not-a-uuid";
|
||||
let expected_error = Error::UrlError(format!("invalid artist MusicBrainz URL: {url}"));
|
||||
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::artist_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
@ -139,7 +131,7 @@ mod tests {
|
||||
let url = "https://musicbrainz.org/artist/i-am-not-a-uuid";
|
||||
let expected_error =
|
||||
Error::UrlError(format!("invalid release-group MusicBrainz URL: {url}"));
|
||||
let actual_error = MusicBrainz::album_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::album_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
@ -148,7 +140,7 @@ mod tests {
|
||||
fn invalid_uuid() {
|
||||
let url = "https://musicbrainz.org/artist/i-am-not-a-uuid";
|
||||
let expected_error: Error = Uuid::try_parse("i-am-not-a-uuid").unwrap_err().into();
|
||||
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::artist_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
@ -157,7 +149,7 @@ mod tests {
|
||||
fn missing_type() {
|
||||
let url = "https://musicbrainz.org";
|
||||
let expected_error = Error::UrlError(format!("invalid artist MusicBrainz URL: {url}/"));
|
||||
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::artist_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
@ -166,7 +158,7 @@ mod tests {
|
||||
fn missing_uuid() {
|
||||
let url = "https://musicbrainz.org/artist";
|
||||
let expected_error = Error::UrlError(format!("invalid artist MusicBrainz URL: {url}"));
|
||||
let actual_error = MusicBrainz::artist_from_str(url).unwrap_err();
|
||||
let actual_error = MusicBrainzUrl::artist_from_str(url).unwrap_err();
|
||||
assert_eq!(actual_error, expected_error);
|
||||
assert_eq!(actual_error.to_string(), expected_error.to_string());
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use crate::core::{
|
||||
collection::{
|
||||
album::{Album, AlbumDate, AlbumId, AlbumSeq},
|
||||
artist::{Artist, ArtistId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
track::{Track, TrackId, TrackNum, TrackQuality},
|
||||
Collection, MergeCollections,
|
||||
},
|
||||
@ -360,7 +360,7 @@ impl<LIB, DB: IDatabase> MusicHoard<LIB, DB> {
|
||||
artist_id: Id,
|
||||
url: S,
|
||||
) -> Result<(), Error> {
|
||||
let mb = MusicBrainz::artist_from_str(url)?;
|
||||
let mb = MusicBrainzUrl::artist_from_str(url)?;
|
||||
self.update_artist(artist_id.as_ref(), |artist| artist.set_musicbrainz_url(mb))
|
||||
}
|
||||
|
||||
@ -469,7 +469,7 @@ mod tests {
|
||||
use mockall::{predicate, Sequence};
|
||||
|
||||
use crate::core::{
|
||||
collection::{artist::ArtistId, musicbrainz::MusicBrainz},
|
||||
collection::{artist::ArtistId, musicbrainz::MusicBrainzUrl},
|
||||
interface::{
|
||||
database::{self, MockIDatabase},
|
||||
library::{self, testmod::LIBRARY_ITEMS, MockILibrary},
|
||||
@ -614,7 +614,7 @@ mod tests {
|
||||
|
||||
assert!(music_hoard.add_artist(artist_id.clone()).is_ok());
|
||||
|
||||
let mut expected: Option<MusicBrainz> = None;
|
||||
let mut expected: Option<MusicBrainzUrl> = None;
|
||||
assert_eq!(music_hoard.collection[0].musicbrainz, expected);
|
||||
|
||||
// Setting a URL on an artist not in the collection is an error.
|
||||
@ -627,7 +627,7 @@ mod tests {
|
||||
assert!(music_hoard
|
||||
.set_artist_musicbrainz(&artist_id, MUSICBRAINZ)
|
||||
.is_ok());
|
||||
_ = expected.insert(MusicBrainz::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
_ = expected.insert(MusicBrainzUrl::artist_from_str(MUSICBRAINZ).unwrap());
|
||||
assert_eq!(music_hoard.collection[0].musicbrainz, expected);
|
||||
|
||||
// Clearing URLs on an artist that does not exist is an error.
|
||||
|
@ -4,7 +4,7 @@ use std::collections::HashMap;
|
||||
use crate::core::collection::{
|
||||
album::{Album, AlbumDate, AlbumId, AlbumMonth, AlbumSeq},
|
||||
artist::{Artist, ArtistId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
track::{Track, TrackFormat, TrackId, TrackNum, TrackQuality},
|
||||
};
|
||||
use crate::tests::*;
|
||||
|
@ -6,7 +6,7 @@ use crate::core::{
|
||||
collection::{
|
||||
album::{Album, AlbumDate, AlbumId, AlbumSeq},
|
||||
artist::{Artist, ArtistId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
Collection,
|
||||
},
|
||||
interface::database::LoadError,
|
||||
@ -55,7 +55,7 @@ impl TryFrom<DeserializeArtist> for Artist {
|
||||
sort: artist.sort.map(ArtistId::new),
|
||||
musicbrainz: artist
|
||||
.musicbrainz
|
||||
.map(MusicBrainz::artist_from_str)
|
||||
.map(MusicBrainzUrl::artist_from_str)
|
||||
.transpose()?,
|
||||
properties: artist.properties,
|
||||
albums: artist
|
||||
@ -77,7 +77,7 @@ impl TryFrom<DeserializeAlbum> for Album {
|
||||
seq: AlbumSeq(album.seq),
|
||||
musicbrainz: album
|
||||
.musicbrainz
|
||||
.map(MusicBrainz::album_from_str)
|
||||
.map(MusicBrainzUrl::album_from_str)
|
||||
.transpose()?,
|
||||
tracks: vec![],
|
||||
})
|
||||
|
12
src/tests.rs
12
src/tests.rs
@ -464,7 +464,7 @@ macro_rules! full_collection {
|
||||
let artist_a = iter.next().unwrap();
|
||||
assert_eq!(artist_a.id.name, "Album_Artist ‘A’");
|
||||
|
||||
artist_a.musicbrainz = Some(MusicBrainz::artist_from_str(
|
||||
artist_a.musicbrainz = Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/00000000-0000-0000-0000-000000000000",
|
||||
).unwrap());
|
||||
|
||||
@ -482,14 +482,14 @@ macro_rules! full_collection {
|
||||
artist_a.albums[0].seq = AlbumSeq(1);
|
||||
artist_a.albums[1].seq = AlbumSeq(1);
|
||||
|
||||
artist_a.albums[0].musicbrainz = Some(MusicBrainz::album_from_str(
|
||||
artist_a.albums[0].musicbrainz = Some(MusicBrainzUrl::album_from_str(
|
||||
"https://musicbrainz.org/release-group/00000000-0000-0000-0000-000000000000"
|
||||
).unwrap());
|
||||
|
||||
let artist_b = iter.next().unwrap();
|
||||
assert_eq!(artist_b.id.name, "Album_Artist ‘B’");
|
||||
|
||||
artist_b.musicbrainz = Some(MusicBrainz::artist_from_str(
|
||||
artist_b.musicbrainz = Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/11111111-1111-1111-1111-111111111111",
|
||||
).unwrap());
|
||||
|
||||
@ -511,18 +511,18 @@ macro_rules! full_collection {
|
||||
artist_b.albums[2].seq = AlbumSeq(2);
|
||||
artist_b.albums[3].seq = AlbumSeq(4);
|
||||
|
||||
artist_b.albums[1].musicbrainz = Some(MusicBrainz::album_from_str(
|
||||
artist_b.albums[1].musicbrainz = Some(MusicBrainzUrl::album_from_str(
|
||||
"https://musicbrainz.org/release-group/11111111-1111-1111-1111-111111111111"
|
||||
).unwrap());
|
||||
|
||||
artist_b.albums[2].musicbrainz = Some(MusicBrainz::album_from_str(
|
||||
artist_b.albums[2].musicbrainz = Some(MusicBrainzUrl::album_from_str(
|
||||
"https://musicbrainz.org/release-group/11111111-1111-1111-1111-111111111112"
|
||||
).unwrap());
|
||||
|
||||
let artist_c = iter.next().unwrap();
|
||||
assert_eq!(artist_c.id.name, "The Album_Artist ‘C’");
|
||||
|
||||
artist_c.musicbrainz = Some(MusicBrainz::artist_from_str(
|
||||
artist_c.musicbrainz = Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/11111111-1111-1111-1111-111111111111",
|
||||
).unwrap());
|
||||
|
||||
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||
use musichoard::collection::{
|
||||
album::{Album, AlbumDate, AlbumId, AlbumMonth, AlbumSeq},
|
||||
artist::{Artist, ArtistId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
track::{Track, TrackFormat, TrackId, TrackNum, TrackQuality},
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
|
@ -4,7 +4,7 @@ use std::collections::HashMap;
|
||||
use musichoard::collection::{
|
||||
album::{Album, AlbumDate, AlbumId, AlbumMonth, AlbumSeq},
|
||||
artist::{Artist, ArtistId},
|
||||
musicbrainz::MusicBrainz,
|
||||
musicbrainz::MusicBrainzUrl,
|
||||
track::{Track, TrackFormat, TrackId, TrackNum, TrackQuality},
|
||||
Collection,
|
||||
};
|
||||
@ -18,7 +18,7 @@ pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| -> Collection {
|
||||
sort: Some(ArtistId{
|
||||
name: String::from("Arkona")
|
||||
}),
|
||||
musicbrainz: Some(MusicBrainz::artist_from_str(
|
||||
musicbrainz: Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/baad262d-55ef-427a-83c7-f7530964f212"
|
||||
).unwrap()),
|
||||
properties: HashMap::from([
|
||||
@ -206,7 +206,7 @@ pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| -> Collection {
|
||||
name: String::from("Eluveitie"),
|
||||
},
|
||||
sort: None,
|
||||
musicbrainz: Some(MusicBrainz::artist_from_str(
|
||||
musicbrainz: Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/8000598a-5edb-401c-8e6d-36b167feaf38",
|
||||
).unwrap()),
|
||||
properties: HashMap::from([
|
||||
@ -451,7 +451,7 @@ pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| -> Collection {
|
||||
name: String::from("Frontside"),
|
||||
},
|
||||
sort: None,
|
||||
musicbrainz: Some(MusicBrainz::artist_from_str(
|
||||
musicbrainz: Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/3a901353-fccd-4afd-ad01-9c03f451b490",
|
||||
).unwrap()),
|
||||
properties: HashMap::from([
|
||||
@ -605,7 +605,7 @@ pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| -> Collection {
|
||||
sort: Some(ArtistId {
|
||||
name: String::from("Heaven’s Basement"),
|
||||
}),
|
||||
musicbrainz: Some(MusicBrainz::artist_from_str(
|
||||
musicbrainz: Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/c2c4d56a-d599-4a18-bd2f-ae644e2198cc",
|
||||
).unwrap()),
|
||||
properties: HashMap::from([
|
||||
@ -737,7 +737,7 @@ pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| -> Collection {
|
||||
name: String::from("Metallica"),
|
||||
},
|
||||
sort: None,
|
||||
musicbrainz: Some(MusicBrainz::artist_from_str(
|
||||
musicbrainz: Some(MusicBrainzUrl::artist_from_str(
|
||||
"https://musicbrainz.org/artist/65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab",
|
||||
).unwrap()),
|
||||
properties: HashMap::from([
|
||||
|
Loading…
Reference in New Issue
Block a user