From d8a1ec1645991b4d54fc9068561c7166895796f5 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 7 Jan 2024 16:27:28 +0100 Subject: [PATCH] Reduce code repetition --- src/lib.rs | 166 ++++++++++++++++++++++++----------------------------- 1 file changed, 76 insertions(+), 90 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ec1ae65..b7cb440 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -542,6 +542,10 @@ impl MusicHoard { } } + pub fn get_collection(&self) -> &Collection { + &self.collection + } + pub fn rescan_library(&mut self) -> Result<(), Error> { match self.library { Some(ref mut library) => { @@ -584,8 +588,73 @@ impl MusicHoard { } } - pub fn get_collection(&self) -> &Collection { - &self.collection + pub fn new_artist>(&mut self, artist_id: ID) -> Result<(), Error> { + if let Ok(artist) = self.get_artist_or_err(artist_id.as_ref()) { + 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 collection = mem::take(&mut self.collection); + self.collection = Self::merge(collection, new_artist); + + Ok(()) + } + + pub fn delete_artist>(&mut self, artist_id: ID) -> Result<(), Error> { + let index_opt = self + .collection + .iter() + .position(|a| &a.id == artist_id.as_ref()); + + match index_opt { + Some(index) => { + self.collection.remove(index); + Ok(()) + } + None => Err(Error::CollectionError(format!( + "artist '{}' is not in the collection", + artist_id.as_ref() + ))), + } + } + + pub fn add_musicbrainz_url, S: AsRef>( + &mut self, + artist_id: ID, + url: S, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())? + .add_musicbrainz_url(url) + } + + pub fn remove_musicbrainz_url, S: AsRef>( + &mut self, + artist_id: ID, + url: S, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())? + .remove_musicbrainz_url(url) + } + + pub fn set_musicbrainz_url, S: AsRef>( + &mut self, + artist_id: ID, + url: S, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())? + .set_musicbrainz_url(url) + } + + pub fn clear_musicbrainz_url>( + &mut self, + artist_id: ID, + ) -> Result<(), Error> { + self.get_artist_or_err(artist_id.as_ref())? + .clear_musicbrainz_url() } fn sort(collection: &mut [Artist]) { @@ -669,94 +738,11 @@ impl MusicHoard { artists } - pub fn new_artist(&mut self, artist_id: ArtistId) -> Result<(), Error> { - // We want to return an error if the artist already exists so we first do a check. - let artists: &Vec = &self.collection; - - if let Some(ref a) = artists.iter().find(|a| a.id == artist_id) { - return Err(Error::CollectionError(format!( - "artist '{}' is already in the collection", - a.id - ))); - } - - let new_artist = vec![Artist::new(artist_id)]; - - let collection = mem::take(&mut self.collection); - self.collection = Self::merge(collection, new_artist); - - Ok(()) - } - - pub fn delete_artist(&mut self, artist_id: ArtistId) -> Result<(), Error> { - let index_opt = self.collection.iter().position(|a| a.id == artist_id); - - match index_opt { - Some(index) => { - self.collection.remove(index); - Ok(()) - } - None => Err(Error::CollectionError(format!( - "artist '{}' is not in the collection", - artist_id - ))), - } - } - - pub fn add_musicbrainz_url>( - &mut self, - artist_id: ArtistId, - url: S, - ) -> Result<(), Error> { - let mut artist_opt = self.collection.iter_mut().find(|a| a.id == artist_id); - match artist_opt { - Some(ref mut artist) => artist.add_musicbrainz_url(url), - None => Err(Error::CollectionError(format!( - "artist '{}' is not in the collection", - artist_id - ))), - } - } - - pub fn remove_musicbrainz_url>( - &mut self, - artist_id: ArtistId, - url: S, - ) -> Result<(), Error> { - let mut artist_opt = self.collection.iter_mut().find(|a| a.id == artist_id); - match artist_opt { - Some(ref mut artist) => artist.remove_musicbrainz_url(url), - None => Err(Error::CollectionError(format!( - "artist '{}' is not in the collection", - artist_id - ))), - } - } - - pub fn set_musicbrainz_url>( - &mut self, - artist_id: ArtistId, - url: S, - ) -> Result<(), Error> { - let mut artist_opt = self.collection.iter_mut().find(|a| a.id == artist_id); - match artist_opt { - Some(ref mut artist) => artist.set_musicbrainz_url(url), - None => Err(Error::CollectionError(format!( - "artist '{}' is not in the collection", - artist_id - ))), - } - } - - pub fn clear_musicbrainz_url(&mut self, artist_id: ArtistId) -> Result<(), Error> { - let mut artist_opt = self.collection.iter_mut().find(|a| a.id == artist_id); - match artist_opt { - Some(ref mut artist) => artist.clear_musicbrainz_url(), - None => Err(Error::CollectionError(format!( - "artist '{}' is not in the collection", - artist_id - ))), - } + fn get_artist_or_err(&mut self, artist_id: &ArtistId) -> Result<&mut Artist, Error> { + let artist_opt = self.collection.iter_mut().find(|a| &a.id == artist_id); + artist_opt.ok_or_else(|| { + Error::CollectionError(format!("artist '{}' is not in the collection", artist_id)) + }) } }