From 3ade35e3fa4b591dc1a421b4ce50e611471fb7f5 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sat, 9 Mar 2024 21:21:33 +0100 Subject: [PATCH] Start splitting musichoard file --- src/core/musichoard/builder.rs | 64 ++++++++++++++++++++++- src/core/musichoard/mod.rs | 38 +++++++++++++- src/core/musichoard/musichoard.rs | 86 +------------------------------ src/lib.rs | 6 +-- 4 files changed, 103 insertions(+), 91 deletions(-) diff --git a/src/core/musichoard/builder.rs b/src/core/musichoard/builder.rs index c699792..f0dbcf5 100644 --- a/src/core/musichoard/builder.rs +++ b/src/core/musichoard/builder.rs @@ -1,7 +1,9 @@ +use std::collections::HashMap; + use crate::{ core::{ interface::{database::IDatabase, library::ILibrary}, - musichoard::musichoard::{MusicHoard, NoDatabase, NoLibrary}, + musichoard::{MusicHoard, NoDatabase, NoLibrary}, }, Error, }; @@ -61,6 +63,20 @@ impl MusicHoardBuilder { } } +impl MusicHoard { + /// Create a new [`MusicHoard`] without any library or database. + pub fn empty() -> Self { + MusicHoard { + collection: vec![], + pre_commit: vec![], + database: NoDatabase, + database_cache: vec![], + library: NoLibrary, + library_cache: HashMap::new(), + } + } +} + impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. pub fn build(self) -> MusicHoard { @@ -68,6 +84,20 @@ impl MusicHoardBuilder { } } +impl MusicHoard { + /// Create a new [`MusicHoard`] with the provided [`ILibrary`] and no database. + pub fn library(library: Library) -> Self { + MusicHoard { + collection: vec![], + pre_commit: vec![], + database: NoDatabase, + database_cache: vec![], + library, + library_cache: HashMap::new(), + } + } +} + impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. pub fn build(self) -> Result, Error> { @@ -75,6 +105,22 @@ impl MusicHoardBuilder { } } +impl MusicHoard { + /// Create a new [`MusicHoard`] with the provided [`IDatabase`] and no library. + pub fn database(database: Database) -> Result { + let mut mh = MusicHoard { + collection: vec![], + pre_commit: vec![], + database, + database_cache: vec![], + library: NoLibrary, + library_cache: HashMap::new(), + }; + mh.reload_database()?; + Ok(mh) + } +} + impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. pub fn build(self) -> Result, Error> { @@ -82,6 +128,22 @@ impl MusicHoardBuilder MusicHoard { + /// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`]. + pub fn new(database: Database, library: Library) -> Result { + let mut mh = MusicHoard { + collection: vec![], + pre_commit: vec![], + database, + database_cache: vec![], + library, + library_cache: HashMap::new(), + }; + mh.reload_database()?; + Ok(mh) + } +} + #[cfg(test)] mod tests { use crate::core::interface::{database::NullDatabase, library::NullLibrary}; diff --git a/src/core/musichoard/mod.rs b/src/core/musichoard/mod.rs index e82a435..ea8fba8 100644 --- a/src/core/musichoard/mod.rs +++ b/src/core/musichoard/mod.rs @@ -4,13 +4,49 @@ pub mod builder; pub mod musichoard; -use std::fmt::{self, Display}; +use std::{ + collections::HashMap, + fmt::{self, Display}, +}; + +use crate::core::collection::{ + artist::{Artist, ArtistId}, + Collection, +}; use crate::core::{ collection, interface::{database, library}, }; +/// The Music Hoard. It is responsible for pulling information from both the library and the +/// database, ensuring its consistent and writing back any changes. +// TODO: Split into inner and external/interfaces to facilitate building. +#[derive(Debug)] +pub struct MusicHoard { + collection: Collection, + pre_commit: Collection, + database: Database, + database_cache: Collection, + library: Library, + library_cache: HashMap, +} + +/// Phantom type for when a library implementation is not needed. +#[derive(Debug)] +pub struct NoLibrary; + +/// Phantom type for when a database implementation is not needed. +#[derive(Debug)] +pub struct NoDatabase; + +impl Default for MusicHoard { + /// Create a new [`MusicHoard`] without any library or database. + fn default() -> Self { + MusicHoard::empty() + } +} + /// Error type for `musichoard`. #[derive(Debug, PartialEq, Eq)] pub enum Error { diff --git a/src/core/musichoard/musichoard.rs b/src/core/musichoard/musichoard.rs index 0f416e2..ed0e103 100644 --- a/src/core/musichoard/musichoard.rs +++ b/src/core/musichoard/musichoard.rs @@ -12,50 +12,9 @@ use crate::core::{ database::IDatabase, library::{ILibrary, Item, Query}, }, - musichoard::Error, + musichoard::{Error, MusicHoard, NoDatabase}, }; -/// The Music Hoard. It is responsible for pulling information from both the library and the -/// database, ensuring its consistent and writing back any changes. -#[derive(Debug)] -pub struct MusicHoard { - collection: Collection, - pre_commit: Collection, - database: Database, - database_cache: Collection, - library: Library, - library_cache: HashMap, -} - -/// Phantom type for when a library implementation is not needed. -#[derive(Debug)] -pub struct NoLibrary; - -/// Phantom type for when a database implementation is not needed. -#[derive(Debug)] -pub struct NoDatabase; - -impl Default for MusicHoard { - /// Create a new [`MusicHoard`] without any library or database. - fn default() -> Self { - MusicHoard::empty() - } -} - -impl MusicHoard { - /// Create a new [`MusicHoard`] without any library or database. - pub fn empty() -> Self { - MusicHoard { - collection: vec![], - pre_commit: vec![], - database: NoDatabase, - database_cache: vec![], - library: NoLibrary, - library_cache: HashMap::new(), - } - } -} - impl MusicHoard { /// Retrieve the [`Collection`]. pub fn get_collection(&self) -> &Collection { @@ -192,18 +151,6 @@ impl MusicHoard { } impl MusicHoard { - /// Create a new [`MusicHoard`] with the provided [`ILibrary`] and no database. - pub fn library(library: Library) -> Self { - MusicHoard { - collection: vec![], - pre_commit: vec![], - database: NoDatabase, - database_cache: vec![], - library, - library_cache: HashMap::new(), - } - } - /// Rescan the library and merge with the in-memory collection. pub fn rescan_library(&mut self) -> Result<(), Error> { self.pre_commit = self.rescan_library_inner()?; @@ -221,22 +168,6 @@ impl MusicHoard { } } -impl MusicHoard { - /// Create a new [`MusicHoard`] with the provided [`IDatabase`] and no library. - pub fn database(database: Database) -> Result { - let mut mh = MusicHoard { - collection: vec![], - pre_commit: vec![], - database, - database_cache: vec![], - library: NoLibrary, - library_cache: HashMap::new(), - }; - mh.reload_database()?; - Ok(mh) - } -} - impl MusicHoard { fn commit(&mut self) -> Result<(), Error> { self.collection = self.pre_commit.clone(); @@ -449,20 +380,6 @@ impl MusicHoard { } impl MusicHoard { - /// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`]. - pub fn new(database: Database, library: Library) -> Result { - let mut mh = MusicHoard { - collection: vec![], - pre_commit: vec![], - database, - database_cache: vec![], - library, - library_cache: HashMap::new(), - }; - mh.reload_database()?; - Ok(mh) - } - /// Rescan the library and merge with the in-memory collection. pub fn rescan_library(&mut self) -> Result<(), Error> { self.pre_commit = self.rescan_library_inner()?; @@ -480,6 +397,7 @@ mod tests { database::{self, MockIDatabase}, library::{self, testmod::LIBRARY_ITEMS, MockILibrary}, }, + musichoard::NoLibrary, testmod::{FULL_COLLECTION, LIBRARY_COLLECTION}, }; diff --git a/src/lib.rs b/src/lib.rs index b1c54b5..4406b1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,7 @@ pub mod library; pub use core::collection; pub use core::interface; -pub use core::musichoard::{ - builder::MusicHoardBuilder, - musichoard::{MusicHoard, NoDatabase, NoLibrary}, - Error, -}; +pub use core::musichoard::{builder::MusicHoardBuilder, Error, MusicHoard, NoDatabase, NoLibrary}; #[cfg(test)] #[macro_use]