diff --git a/src/core/musichoard/builder.rs b/src/core/musichoard/builder.rs index 46c9f4c..e828718 100644 --- a/src/core/musichoard/builder.rs +++ b/src/core/musichoard/builder.rs @@ -65,7 +65,6 @@ impl MusicHoard { filter: CollectionFilter::default(), filtered: vec![], collection: vec![], - pre_commit: vec![], database: NoDatabase, library: NoLibrary, library_cache: vec![], @@ -87,7 +86,6 @@ impl MusicHoard { filter: CollectionFilter::default(), filtered: vec![], collection: vec![], - pre_commit: vec![], database: NoDatabase, library, library_cache: vec![], @@ -109,7 +107,6 @@ impl MusicHoard { filter: CollectionFilter::default(), filtered: vec![], collection: vec![], - pre_commit: vec![], database, library: NoLibrary, library_cache: vec![], @@ -131,7 +128,6 @@ impl MusicHoard { filter: CollectionFilter::default(), filtered: vec![], collection: vec![], - pre_commit: vec![], database, library, library_cache: vec![], diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index ae9e58e..51f8ad5 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -122,8 +122,6 @@ impl IMusicHoardDatabase for MusicHoard IMusicHoardDatabasePrivate for MusicHoard { fn commit(&mut self) -> Result<(), Error> { - self.collection = self.pre_commit.clone(); self.filtered = self.filter_collection(); Ok(()) } @@ -373,14 +370,11 @@ impl IMusicHoardDatabasePrivate for MusicHoard { impl IMusicHoardDatabasePrivate for MusicHoard { fn commit(&mut self) -> Result<(), Error> { - if self.collection != self.pre_commit { - if let Err(err) = self.database.save(&self.pre_commit) { - self.pre_commit = self.collection.clone(); - return Err(err.into()); - } - self.collection = self.pre_commit.clone(); - self.filtered = self.filter_collection(); + if let Err(err) = self.database.save(&self.collection) { + self.reload_database()?; + return Err(err.into()); } + self.filtered = self.filter_collection(); Ok(()) } } @@ -390,7 +384,7 @@ impl MusicHoard { where FnColl: FnOnce(&mut Collection), { - fn_coll(&mut self.pre_commit); + fn_coll(&mut self.collection); self.commit() } @@ -404,7 +398,7 @@ impl MusicHoard { FnArtist: FnOnce(&mut Artist), FnColl: FnOnce(&mut Collection), { - let artist = Self::get_artist_mut_or_err(&mut self.pre_commit, artist_id)?; + let artist = Self::get_artist_mut_or_err(&mut self.collection, artist_id)?; fn_artist(artist); self.update_collection(fn_coll) } @@ -431,7 +425,7 @@ impl MusicHoard { FnAlbum: FnOnce(&mut Album), FnArtist: FnOnce(&mut Artist), { - let artist = Self::get_artist_mut_or_err(&mut self.pre_commit, artist_id)?; + let artist = Self::get_artist_mut_or_err(&mut self.collection, artist_id)?; let album = Self::get_album_mut_or_err(artist, album_id)?; fn_album(album); fn_artist(artist); @@ -493,7 +487,7 @@ mod tests { .returning(|| Ok(FULL_COLLECTION.to_owned())); database .expect_save() - .times(1) + .times(3) .in_sequence(&mut seq) .with(predicate::eq(with_artist.clone())) .returning(|_| Ok(())); @@ -789,7 +783,7 @@ mod tests { .returning(|| Ok(FULL_COLLECTION.to_owned())); database .expect_save() - .times(1) + .times(3) .in_sequence(&mut seq) .with(predicate::eq(with_album.clone())) .returning(|_| Ok(())); @@ -1018,11 +1012,21 @@ mod tests { let database_result = Err(database::SaveError::IoError(String::from("I/O error"))); - database.expect_load().return_once(|| Ok(vec![])); + let mut seq = Sequence::new(); + database + .expect_load() + .times(1) + .in_sequence(&mut seq) + .return_once(|| Ok(vec![])); database .expect_save() .times(1) .return_once(|_: &Collection| database_result); + database + .expect_load() + .times(1) + .in_sequence(&mut seq) + .return_once(|| Ok(vec![])); let mut music_hoard = MusicHoard::database(database); music_hoard.reload_database().unwrap(); diff --git a/src/core/musichoard/library.rs b/src/core/musichoard/library.rs index d0deb30..29a5ba0 100644 --- a/src/core/musichoard/library.rs +++ b/src/core/musichoard/library.rs @@ -23,7 +23,7 @@ pub trait IMusicHoardLibrary { impl IMusicHoardLibrary for MusicHoard { fn rescan_library(&mut self) -> Result<(), Error> { - self.pre_commit = self.rescan_library_inner(vec![])?; + self.collection = self.rescan_library_inner(vec![])?; self.commit() } } @@ -33,7 +33,7 @@ impl IMusicHoardLibrary for MusicHoard { filter: CollectionFilter, filtered: Collection, collection: Collection, - pre_commit: Collection, database: Database, library: Library, library_cache: Collection,