Break down the musichoard files #165

Merged
wojtek merged 9 commits from 164---break-down-the-musichoard-files into main 2024-03-09 22:52:04 +01:00
7 changed files with 53 additions and 20 deletions
Showing only changes of commit b8e1a771c8 - Show all commits

View File

@ -7,17 +7,45 @@ use crate::core::{
musichoard::{Error, MusicHoard}, musichoard::{Error, MusicHoard},
}; };
impl<Database, Library> MusicHoard<Database, Library> { pub trait IMusicHoardBase {
/// Retrieve the [`Collection`]. fn get_collection(&self) -> &Collection;
pub fn get_collection(&self) -> &Collection { }
impl<Database, Library> IMusicHoardBase for MusicHoard<Database, Library> {
fn get_collection(&self) -> &Collection {
&self.collection &self.collection
} }
}
pub fn sort_artists(collection: &mut [Artist]) { pub trait IMusicHoardBasePrivate {
fn sort_artists(collection: &mut [Artist]);
fn sort_albums_and_tracks<'a, C: Iterator<Item = &'a mut Artist>>(collection: C);
fn merge_collections(&self) -> Collection;
fn get_artist<'a>(collection: &'a Collection, artist_id: &ArtistId) -> Option<&'a Artist>;
fn get_artist_mut<'a>(
collection: &'a mut Collection,
artist_id: &ArtistId,
) -> Option<&'a mut Artist>;
fn get_artist_mut_or_err<'a>(
collection: &'a mut Collection,
artist_id: &ArtistId,
) -> Result<&'a mut Artist, Error>;
fn get_album_mut<'a>(artist: &'a mut Artist, album_id: &AlbumId) -> Option<&'a mut Album>;
fn get_album_mut_or_err<'a>(
artist: &'a mut Artist,
album_id: &AlbumId,
) -> Result<&'a mut Album, Error>;
}
impl<Database, Library> IMusicHoardBasePrivate for MusicHoard<Database, Library> {
fn sort_artists(collection: &mut [Artist]) {
collection.sort_unstable(); collection.sort_unstable();
} }
pub fn sort_albums_and_tracks<'a, COL: Iterator<Item = &'a mut Artist>>(collection: COL) { fn sort_albums_and_tracks<'a, COL: Iterator<Item = &'a mut Artist>>(collection: COL) {
for artist in collection { for artist in collection {
artist.albums.sort_unstable(); artist.albums.sort_unstable();
for album in artist.albums.iter_mut() { for album in artist.albums.iter_mut() {
@ -26,22 +54,22 @@ impl<Database, Library> MusicHoard<Database, Library> {
} }
} }
pub fn merge_collections(&self) -> Collection { fn merge_collections(&self) -> Collection {
MergeCollections::merge(self.library_cache.clone(), self.database_cache.clone()) MergeCollections::merge(self.library_cache.clone(), self.database_cache.clone())
} }
pub fn get_artist<'a>(collection: &'a Collection, artist_id: &ArtistId) -> Option<&'a Artist> { fn get_artist<'a>(collection: &'a Collection, artist_id: &ArtistId) -> Option<&'a Artist> {
collection.iter().find(|a| &a.id == artist_id) collection.iter().find(|a| &a.id == artist_id)
} }
pub fn get_artist_mut<'a>( fn get_artist_mut<'a>(
collection: &'a mut Collection, collection: &'a mut Collection,
artist_id: &ArtistId, artist_id: &ArtistId,
) -> Option<&'a mut Artist> { ) -> Option<&'a mut Artist> {
collection.iter_mut().find(|a| &a.id == artist_id) collection.iter_mut().find(|a| &a.id == artist_id)
} }
pub fn get_artist_mut_or_err<'a>( fn get_artist_mut_or_err<'a>(
collection: &'a mut Collection, collection: &'a mut Collection,
artist_id: &ArtistId, artist_id: &ArtistId,
) -> Result<&'a mut Artist, Error> { ) -> Result<&'a mut Artist, Error> {
@ -50,11 +78,11 @@ impl<Database, Library> MusicHoard<Database, Library> {
}) })
} }
pub fn get_album_mut<'a>(artist: &'a mut Artist, album_id: &AlbumId) -> Option<&'a mut Album> { fn get_album_mut<'a>(artist: &'a mut Artist, album_id: &AlbumId) -> Option<&'a mut Album> {
artist.albums.iter_mut().find(|a| &a.id == album_id) artist.albums.iter_mut().find(|a| &a.id == album_id)
} }
pub fn get_album_mut_or_err<'a>( fn get_album_mut_or_err<'a>(
artist: &'a mut Artist, artist: &'a mut Artist,
album_id: &AlbumId, album_id: &AlbumId,
) -> Result<&'a mut Album, Error> { ) -> Result<&'a mut Album, Error> {

View File

@ -6,7 +6,7 @@ use crate::core::{
Collection, Collection,
}, },
interface::database::IDatabase, interface::database::IDatabase,
musichoard::{Error, MusicHoard, NoDatabase}, musichoard::{base::IMusicHoardBasePrivate, Error, MusicHoard, NoDatabase},
}; };
impl<Library> MusicHoard<NoDatabase, Library> { impl<Library> MusicHoard<NoDatabase, Library> {
@ -227,7 +227,7 @@ mod tests {
use crate::core::{ use crate::core::{
collection::{album::AlbumDate, artist::ArtistId, musicbrainz::MusicBrainzUrl}, collection::{album::AlbumDate, artist::ArtistId, musicbrainz::MusicBrainzUrl},
interface::database::{self, MockIDatabase}, interface::database::{self, MockIDatabase},
musichoard::NoLibrary, musichoard::{base::IMusicHoardBase, NoLibrary},
testmod::FULL_COLLECTION, testmod::FULL_COLLECTION,
}; };

View File

@ -11,7 +11,7 @@ use crate::core::{
database::IDatabase, database::IDatabase,
library::{ILibrary, Item, Query}, library::{ILibrary, Item, Query},
}, },
musichoard::{Error, MusicHoard, NoDatabase}, musichoard::{base::IMusicHoardBasePrivate, Error, MusicHoard, NoDatabase},
}; };
impl<Library: ILibrary> MusicHoard<NoDatabase, Library> { impl<Library: ILibrary> MusicHoard<NoDatabase, Library> {
@ -124,6 +124,7 @@ mod tests {
database::MockIDatabase, database::MockIDatabase,
library::{self, testmod::LIBRARY_ITEMS, MockILibrary}, library::{self, testmod::LIBRARY_ITEMS, MockILibrary},
}, },
musichoard::base::IMusicHoardBase,
testmod::LIBRARY_COLLECTION, testmod::LIBRARY_COLLECTION,
}; };

View File

@ -6,6 +6,8 @@ mod library;
pub mod builder; pub mod builder;
pub use base::IMusicHoardBase;
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt::{self, Display}, fmt::{self, Display},

View File

@ -7,7 +7,9 @@ pub mod library;
pub use core::collection; pub use core::collection;
pub use core::interface; pub use core::interface;
pub use core::musichoard::{builder::MusicHoardBuilder, Error, MusicHoard, NoDatabase, NoLibrary}; pub use core::musichoard::{
builder::MusicHoardBuilder, Error, IMusicHoardBase, MusicHoard, NoDatabase, NoLibrary,
};
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]

View File

@ -1,6 +1,6 @@
use musichoard::{ use musichoard::{
collection::Collection, interface::database::IDatabase, interface::library::ILibrary, collection::Collection, interface::database::IDatabase, interface::library::ILibrary,
MusicHoard, IMusicHoardBase, MusicHoard,
}; };
#[cfg(test)] #[cfg(test)]
@ -16,15 +16,15 @@ pub trait IMusicHoard {
// GRCOV_EXCL_START // GRCOV_EXCL_START
impl<Database: IDatabase, Library: ILibrary> IMusicHoard for MusicHoard<Database, Library> { impl<Database: IDatabase, Library: ILibrary> IMusicHoard for MusicHoard<Database, Library> {
fn rescan_library(&mut self) -> Result<(), musichoard::Error> { fn rescan_library(&mut self) -> Result<(), musichoard::Error> {
MusicHoard::<Database, Library>::rescan_library(self) Self::rescan_library(self)
} }
fn reload_database(&mut self) -> Result<(), musichoard::Error> { fn reload_database(&mut self) -> Result<(), musichoard::Error> {
MusicHoard::reload_database(self) Self::reload_database(self)
} }
fn get_collection(&self) -> &Collection { fn get_collection(&self) -> &Collection {
MusicHoard::get_collection(self) <Self as IMusicHoardBase>::get_collection(self)
} }
} }
// GRCOV_EXCL_STOP // GRCOV_EXCL_STOP

View File

@ -9,7 +9,7 @@ mod testlib;
use musichoard::{ use musichoard::{
database::json::{backend::JsonDatabaseFileBackend, JsonDatabase}, database::json::{backend::JsonDatabaseFileBackend, JsonDatabase},
library::beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary}, library::beets::{executor::BeetsLibraryProcessExecutor, BeetsLibrary},
MusicHoard, IMusicHoardBase, MusicHoard,
}; };
use crate::testlib::COLLECTION; use crate::testlib::COLLECTION;