Reduce code repetition

This commit is contained in:
Wojciech Kozlowski 2024-01-07 16:27:28 +01:00
parent 73c90a7c11
commit d8a1ec1645

View File

@ -542,6 +542,10 @@ impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
}
}
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<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
}
}
pub fn get_collection(&self) -> &Collection {
&self.collection
pub fn new_artist<ID: AsRef<ArtistId>>(&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<ID: AsRef<ArtistId>>(&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<ID: AsRef<ArtistId>, S: AsRef<str>>(
&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<ID: AsRef<ArtistId>, S: AsRef<str>>(
&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<ID: AsRef<ArtistId>, S: AsRef<str>>(
&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<ID: AsRef<ArtistId>>(
&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<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
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<Artist> = &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<S: AsRef<str>>(
&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<S: AsRef<str>>(
&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<S: AsRef<str>>(
&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))
})
}
}