From f95afd5f9aebff08b5ff723a484d97ff42895068 Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sat, 9 Mar 2024 21:04:49 +0100 Subject: [PATCH] Emphasize the more fundamental nature of the db --- src/bin/musichoard-edit.rs | 2 +- src/core/musichoard/musichoard.rs | 54 +++++++++++------------ src/core/musichoard/musichoard_builder.rs | 46 ++++++++++--------- src/main.rs | 9 ++-- src/tui/lib.rs | 4 +- tests/lib.rs | 4 +- 6 files changed, 64 insertions(+), 55 deletions(-) diff --git a/src/bin/musichoard-edit.rs b/src/bin/musichoard-edit.rs index 94f7f0c..f589b12 100644 --- a/src/bin/musichoard-edit.rs +++ b/src/bin/musichoard-edit.rs @@ -8,7 +8,7 @@ use musichoard::{ MusicHoard, MusicHoardBuilder, NoLibrary, }; -type MH = MusicHoard>; +type MH = MusicHoard, NoLibrary>; #[derive(StructOpt, Debug)] #[structopt(about = "musichoard-edit: edit the MusicHoard database", diff --git a/src/core/musichoard/musichoard.rs b/src/core/musichoard/musichoard.rs index 191722c..0f416e2 100644 --- a/src/core/musichoard/musichoard.rs +++ b/src/core/musichoard/musichoard.rs @@ -18,13 +18,13 @@ use crate::core::{ /// 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 { +pub struct MusicHoard { collection: Collection, pre_commit: Collection, - library: LIB, - database: DB, - library_cache: HashMap, + database: Database, database_cache: Collection, + library: Library, + library_cache: HashMap, } /// Phantom type for when a library implementation is not needed. @@ -35,28 +35,28 @@ pub struct NoLibrary; #[derive(Debug)] pub struct NoDatabase; -impl Default for MusicHoard { +impl Default for MusicHoard { /// Create a new [`MusicHoard`] without any library or database. fn default() -> Self { MusicHoard::empty() } } -impl MusicHoard { +impl MusicHoard { /// Create a new [`MusicHoard`] without any library or database. pub fn empty() -> Self { MusicHoard { collection: vec![], pre_commit: vec![], - library: NoLibrary, database: NoDatabase, - library_cache: HashMap::new(), database_cache: vec![], + library: NoLibrary, + library_cache: HashMap::new(), } } } -impl MusicHoard { +impl MusicHoard { /// Retrieve the [`Collection`]. pub fn get_collection(&self) -> &Collection { &self.collection @@ -191,16 +191,16 @@ impl MusicHoard { } } -impl MusicHoard { +impl MusicHoard { /// Create a new [`MusicHoard`] with the provided [`ILibrary`] and no database. - pub fn library(library: LIB) -> Self { + pub fn library(library: Library) -> Self { MusicHoard { collection: vec![], pre_commit: vec![], - library, database: NoDatabase, - library_cache: HashMap::new(), database_cache: vec![], + library, + library_cache: HashMap::new(), } } @@ -211,7 +211,7 @@ impl MusicHoard { } } -impl MusicHoard { +impl MusicHoard { fn rescan_library_inner(&mut self) -> Result { let items = self.library.list(&Query::new())?; self.library_cache = Self::items_to_artists(items)?; @@ -221,30 +221,30 @@ impl MusicHoard { } } -impl MusicHoard { +impl MusicHoard { /// Create a new [`MusicHoard`] with the provided [`IDatabase`] and no library. - pub fn database(database: DB) -> Result { + pub fn database(database: Database) -> Result { let mut mh = MusicHoard { collection: vec![], pre_commit: vec![], - library: NoLibrary, database, - library_cache: HashMap::new(), database_cache: vec![], + library: NoLibrary, + library_cache: HashMap::new(), }; mh.reload_database()?; Ok(mh) } } -impl MusicHoard { +impl MusicHoard { fn commit(&mut self) -> Result<(), Error> { self.collection = self.pre_commit.clone(); Ok(()) } } -impl MusicHoard { +impl MusicHoard { /// Load the database and merge with the in-memory collection. pub fn reload_database(&mut self) -> Result<(), Error> { self.database_cache = self.database.load()?; @@ -448,16 +448,16 @@ impl MusicHoard { } } -impl MusicHoard { +impl MusicHoard { /// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`]. - pub fn new(library: LIB, database: DB) -> Result { + pub fn new(database: Database, library: Library) -> Result { let mut mh = MusicHoard { collection: vec![], pre_commit: vec![], - library, database, - library_cache: HashMap::new(), database_cache: vec![], + library, + library_cache: HashMap::new(), }; mh.reload_database()?; Ok(mh) @@ -542,7 +542,7 @@ mod tests { database.expect_load().times(1).returning(|| Ok(vec![])); database.expect_save().times(4).returning(|_| Ok(())); - type MH = MusicHoard; + type MH = MusicHoard; let mut music_hoard: MH = MusicHoard::database(database).unwrap(); let artist_1_id = ArtistId::new("the artist"); @@ -935,7 +935,7 @@ mod tests { .times(1) .return_once(|_| Ok(())); - let mut music_hoard = MusicHoard::new(library, database).unwrap(); + let mut music_hoard = MusicHoard::new(database, library).unwrap(); music_hoard.rescan_library().unwrap(); assert_eq!(music_hoard.get_collection(), &*LIBRARY_COLLECTION); @@ -1083,7 +1083,7 @@ mod tests { .times(1) .return_once(|| Ok(FULL_COLLECTION.to_owned())); - let music_hoard = MusicHoard::new(library, database).unwrap(); + let music_hoard = MusicHoard::new(database, library).unwrap(); assert_eq!(music_hoard.get_collection(), &*FULL_COLLECTION); } diff --git a/src/core/musichoard/musichoard_builder.rs b/src/core/musichoard/musichoard_builder.rs index 5b1bc5f..c699792 100644 --- a/src/core/musichoard/musichoard_builder.rs +++ b/src/core/musichoard/musichoard_builder.rs @@ -8,71 +8,77 @@ use crate::{ /// Builder for [`MusicHoard`]. Its purpose is to make it easier to set various combinations of /// library/database or their absence. -pub struct MusicHoardBuilder { - library: LIB, - database: DB, +pub struct MusicHoardBuilder { + database: Database, + library: Library, } -impl Default for MusicHoardBuilder { +impl Default for MusicHoardBuilder { /// Create a [`MusicHoardBuilder`]. fn default() -> Self { Self::new() } } -impl MusicHoardBuilder { +impl MusicHoardBuilder { /// Create a [`MusicHoardBuilder`]. pub fn new() -> Self { MusicHoardBuilder { - library: NoLibrary, database: NoDatabase, + library: NoLibrary, } } } -impl MusicHoardBuilder { +impl MusicHoardBuilder { /// Set a library for [`MusicHoard`]. - pub fn set_library(self, library: NEWLIB) -> MusicHoardBuilder { + pub fn set_library( + self, + library: NewLibrary, + ) -> MusicHoardBuilder { MusicHoardBuilder { - library, database: self.database, + library, } } /// Set a database for [`MusicHoard`]. - pub fn set_database(self, database: NEWDB) -> MusicHoardBuilder { + pub fn set_database( + self, + database: NewDatabase, + ) -> MusicHoardBuilder { MusicHoardBuilder { - library: self.library, database, + library: self.library, } } } -impl MusicHoardBuilder { +impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. - pub fn build(self) -> MusicHoard { + pub fn build(self) -> MusicHoard { MusicHoard::empty() } } -impl MusicHoardBuilder { +impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. - pub fn build(self) -> MusicHoard { + pub fn build(self) -> MusicHoard { MusicHoard::library(self.library) } } -impl MusicHoardBuilder { +impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. - pub fn build(self) -> Result, Error> { + pub fn build(self) -> Result, Error> { MusicHoard::database(self.database) } } -impl MusicHoardBuilder { +impl MusicHoardBuilder { /// Build [`MusicHoard`] with the currently set library and database. - pub fn build(self) -> Result, Error> { - MusicHoard::new(self.library, self.database) + pub fn build(self) -> Result, Error> { + MusicHoard::new(self.database, self.library) } } diff --git a/src/main.rs b/src/main.rs index 87e3140..96d57a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,7 @@ struct DbOpt { no_database: bool, } -fn with(builder: MusicHoardBuilder) { +fn with(builder: MusicHoardBuilder) { let music_hoard = builder.build().expect("failed to initialise MusicHoard"); // Initialize the terminal user interface. @@ -76,7 +76,10 @@ fn with(builder: MusicHoardBuilder) { Tui::run(terminal, app, ui, handler, listener).expect("failed to run tui"); } -fn with_database(db_opt: DbOpt, builder: MusicHoardBuilder) { +fn with_database( + db_opt: DbOpt, + builder: MusicHoardBuilder, +) { if db_opt.no_database { with(builder.set_database(NullDatabase)); } else { @@ -103,7 +106,7 @@ fn with_database(db_opt: DbOpt, builder: MusicHoardBuilder) { +fn with_library(lib_opt: LibOpt, db_opt: DbOpt, builder: MusicHoardBuilder) { if lib_opt.no_library { with_database(db_opt, builder.set_library(NullLibrary)); } else if let Some(uri) = lib_opt.beets_ssh_uri { diff --git a/src/tui/lib.rs b/src/tui/lib.rs index d75ddc8..4b32e13 100644 --- a/src/tui/lib.rs +++ b/src/tui/lib.rs @@ -14,9 +14,9 @@ pub trait IMusicHoard { } // GRCOV_EXCL_START -impl IMusicHoard for MusicHoard { +impl IMusicHoard for MusicHoard { fn rescan_library(&mut self) -> Result<(), musichoard::Error> { - MusicHoard::::rescan_library(self) + MusicHoard::::rescan_library(self) } fn reload_database(&mut self) -> Result<(), musichoard::Error> { diff --git a/tests/lib.rs b/tests/lib.rs index 5074fcf..624e7db 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -29,7 +29,7 @@ fn merge_library_then_database() { let backend = JsonDatabaseFileBackend::new(&*database::json::DATABASE_TEST_FILE); let database = JsonDatabase::new(backend); - let mut music_hoard = MusicHoard::new(library, database).unwrap(); + let mut music_hoard = MusicHoard::new(database, library).unwrap(); music_hoard.rescan_library().unwrap(); music_hoard.reload_database().unwrap(); @@ -52,7 +52,7 @@ fn merge_database_then_library() { let backend = JsonDatabaseFileBackend::new(&*database::json::DATABASE_TEST_FILE); let database = JsonDatabase::new(backend); - let mut music_hoard = MusicHoard::new(library, database).unwrap(); + let mut music_hoard = MusicHoard::new(database, library).unwrap(); music_hoard.reload_database().unwrap(); music_hoard.rescan_library().unwrap();