Make database and library optional #86

Merged
wojtek merged 4 commits from 25---make-database-and-library-optional into main 2024-01-07 11:07:36 +01:00
3 changed files with 43 additions and 28 deletions
Showing only changes of commit 04c19ea3e0 - Show all commits

View File

@ -460,14 +460,14 @@ impl From<InvalidUrlError> for Error {
/// The Music Hoard. It is responsible for pulling information from both the library and the
/// database, ensuring its consistent and writing back any changes.
pub struct MusicHoard<LIB, DB> {
library: LIB,
database: DB,
library: Option<LIB>,
database: Option<DB>,
collection: Collection,
}
impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
/// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`].
pub fn new(library: LIB, database: DB) -> Self {
pub fn new(library: Option<LIB>, database: Option<DB>) -> Self {
MusicHoard {
library,
database,
@ -476,31 +476,46 @@ impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
}
pub fn rescan_library(&mut self) -> Result<(), Error> {
let items = self.library.list(&Query::new())?;
let mut library = Self::items_to_artists(items);
Self::sort(&mut library);
match self.library {
Some(ref mut library) => {
let items = library.list(&Query::new())?;
let mut library_collection = Self::items_to_artists(items);
Self::sort(&mut library_collection);
let collection = mem::take(&mut self.collection);
self.collection = Self::merge(library, collection);
self.collection = Self::merge(library_collection, collection);
Ok(())
}
None => Err(Error::LibraryError(String::from("library not provided"))),
}
}
pub fn load_from_database(&mut self) -> Result<(), Error> {
let mut database: Collection = vec![];
self.database.load(&mut database)?;
Self::sort(&mut database);
match self.database {
Some(ref mut database) => {
let mut database_collection: Collection = vec![];
database.load(&mut database_collection)?;
Self::sort(&mut database_collection);
let collection = mem::take(&mut self.collection);
self.collection = Self::merge(collection, database);
self.collection = Self::merge(collection, database_collection);
Ok(())
}
None => Err(Error::DatabaseError(String::from("database not provided"))),
}
}
pub fn save_to_database(&mut self) -> Result<(), Error> {
self.database.save(&self.collection)?;
match self.database {
Some(ref mut database) => {
database.save(&self.collection)?;
Ok(())
}
None => Err(Error::DatabaseError(String::from("database not provided"))),
}
}
pub fn get_collection(&self) -> &Collection {
&self.collection
@ -834,7 +849,7 @@ mod tests {
.times(1)
.return_once(|_| library_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.rescan_library().unwrap();
assert_eq!(
@ -861,7 +876,7 @@ mod tests {
.times(1)
.return_once(|_| library_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.rescan_library().unwrap();
assert_eq!(
@ -888,7 +903,7 @@ mod tests {
.times(1)
.return_once(|_| library_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.rescan_library().unwrap();
assert_eq!(music_hoard.get_collection(), &expected);
@ -907,7 +922,7 @@ mod tests {
Ok(())
});
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.load_from_database().unwrap();
assert_eq!(music_hoard.get_collection(), &*COLLECTION);
@ -936,7 +951,7 @@ mod tests {
.times(1)
.return_once(|_: &Collection| database_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.rescan_library().unwrap();
assert_eq!(
@ -958,7 +973,7 @@ mod tests {
.times(1)
.return_once(|_| library_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
let actual_err = music_hoard.rescan_library().unwrap_err();
let expected_err =
@ -980,7 +995,7 @@ mod tests {
.times(1)
.return_once(|_: &mut Collection| database_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
let actual_err = music_hoard.load_from_database().unwrap_err();
let expected_err = Error::DatabaseError(
@ -1003,7 +1018,7 @@ mod tests {
.times(1)
.return_once(|_: &Collection| database_result);
let mut music_hoard = MusicHoard::new(library, database);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
let actual_err = music_hoard.save_to_database().unwrap_err();
let expected_err = Error::DatabaseError(

View File

@ -42,7 +42,7 @@ struct Opt {
}
fn with<LIB: ILibrary, DB: IDatabase>(lib: LIB, db: DB) {
let music_hoard = MusicHoard::new(lib, db);
let music_hoard = MusicHoard::new(Some(lib), Some(db));
// Initialize the terminal user interface.
let backend = CrosstermBackend::new(io::stdout());

View File

@ -30,7 +30,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);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.rescan_library().unwrap();
music_hoard.load_from_database().unwrap();
@ -55,7 +55,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);
let mut music_hoard = MusicHoard::new(Some(library), Some(database));
music_hoard.load_from_database().unwrap();
music_hoard.rescan_library().unwrap();