From 0a63ac46190cbd084428c70ecd311bb1bcbc7ce2 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Wed, 10 Jan 2024 20:30:29 +0100 Subject: [PATCH] Some clean up --- src/bin/mh-edit.rs | 5 ++++- src/lib.rs | 44 +++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/bin/mh-edit.rs b/src/bin/mh-edit.rs index aaaf61a..daef105 100644 --- a/src/bin/mh-edit.rs +++ b/src/bin/mh-edit.rs @@ -47,7 +47,10 @@ enum ArtistCommand { Delete(ArtistValue), #[structopt(name = "musicbrainz", about = "Edit the MusicBrainz URL of an artist")] MusicBrainz(UrlCommand), - #[structopt(name = "musicbutler", about = "Edit the MusicButler URL(s) of an artist")] + #[structopt( + name = "musicbutler", + about = "Edit the MusicButler URL(s) of an artist" + )] MusicButler(UrlCommand), #[structopt(about = "Edit the Bandcamp URL(s) of an artist")] Bandcamp(UrlCommand), diff --git a/src/lib.rs b/src/lib.rs index 98627bd..5d95931 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ pub mod library; use std::{ cmp::Ordering, collections::{HashMap, HashSet}, - fmt::{self, Debug}, + fmt::{self, Debug, Display}, iter::Peekable, mem, }; @@ -42,7 +42,7 @@ struct InvalidUrlError { url: String, } -impl fmt::Display for InvalidUrlError { +impl Display for InvalidUrlError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "invalid url of type {:?}: {}", self.url_type, self.url) } @@ -88,7 +88,7 @@ impl TryFrom<&str> for MusicBrainz { } } -impl fmt::Display for MusicBrainz { +impl Display for MusicBrainz { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } @@ -232,7 +232,7 @@ impl TryFrom<&str> for Qobuz { } } -impl fmt::Display for Qobuz { +impl Display for Qobuz { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } @@ -344,7 +344,7 @@ impl ArtistId { } } -impl fmt::Display for ArtistId { +impl Display for ArtistId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.name) } @@ -431,10 +431,7 @@ impl Artist { } } - fn add_unique_url< - S: AsRef, - T: for<'a> TryFrom<&'a str, Error = Error> + Eq + fmt::Display, - >( + fn add_unique_url, T: for<'a> TryFrom<&'a str, Error = Error> + Eq + Display>( container: &mut Option, url: S, ) -> Result<(), Error> { @@ -643,7 +640,7 @@ pub enum Error { InvalidUrlError(String), } -impl fmt::Display for Error { +impl Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Self::CollectionError(ref s) => write!(f, "failed to read/write the collection: {s}"), @@ -832,15 +829,16 @@ impl MusicHoard { } } - pub fn new_artist>(&mut self, artist_id: ID) -> Result<(), Error> { - if let Ok(artist) = self.get_artist_or_err(artist_id.as_ref()) { + pub fn new_artist>(&mut self, artist_id: ID) -> Result<(), Error> { + let artist_id: ArtistId = artist_id.into(); + if let Ok(artist) = self.get_artist_or_err(&artist_id) { return Err(Error::CollectionError(format!( "artist '{}' is already in the collection", artist.id ))); } - let new_artist = vec![Artist::new(artist_id.as_ref().clone())]; + let new_artist = vec![Artist::new(artist_id)]; let collection = mem::take(&mut self.collection); self.collection = Self::merge(collection, new_artist); @@ -1090,9 +1088,9 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); - let actual_err = music_hoard.new_artist(&artist_id).unwrap_err(); + let actual_err = music_hoard.new_artist(artist_id.clone()).unwrap_err(); let expected_err = Error::CollectionError(String::from( "artist 'an artist' is already in the collection", )); @@ -1115,7 +1113,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Option = None; assert_eq!(music_hoard.collection[0].properties.musicbrainz, expected); @@ -1186,7 +1184,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Option = None; assert_eq!(music_hoard.collection[0].properties.musicbrainz, expected); @@ -1241,7 +1239,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Vec = vec![]; assert_eq!(music_hoard.collection[0].properties.musicbutler, expected); @@ -1371,7 +1369,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Vec = vec![]; assert_eq!(music_hoard.collection[0].properties.musicbutler, expected); @@ -1434,7 +1432,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Vec = vec![]; assert_eq!(music_hoard.collection[0].properties.bandcamp, expected); @@ -1564,7 +1562,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Vec = vec![]; assert_eq!(music_hoard.collection[0].properties.bandcamp, expected); @@ -1627,7 +1625,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Option = None; assert_eq!(music_hoard.collection[0].properties.qobuz, expected); @@ -1678,7 +1676,7 @@ mod tests { let artist_id_2 = ArtistId::new("another artist"); let mut music_hoard = MusicHoard::::new(None, None); - assert!(music_hoard.new_artist(&artist_id).is_ok()); + assert!(music_hoard.new_artist(artist_id.clone()).is_ok()); let mut expected: Option = None; assert_eq!(music_hoard.collection[0].properties.qobuz, expected);