diff --git a/src/core/interface/database/mod.rs b/src/core/interface/database/mod.rs index b60575c..dd93d4d 100644 --- a/src/core/interface/database/mod.rs +++ b/src/core/interface/database/mod.rs @@ -13,8 +13,8 @@ pub trait IDatabase { /// Load collection from the database. fn load(&self) -> Result; - /// Save collection to the database. - fn save(&mut self, collection: &Collection) -> Result<(), SaveError>; + /// Save collection to the database. Return the written data. + fn save(&mut self, collection: &Collection) -> Result; } /// Null database implementation of [`IDatabase`]. @@ -25,8 +25,8 @@ impl IDatabase for NullDatabase { Ok(vec![]) } - fn save(&mut self, _collection: &Collection) -> Result<(), SaveError> { - Ok(()) + fn save(&mut self, _collection: &Collection) -> Result { + Ok(vec![]) } } diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index 990871c..392546c 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -374,9 +374,14 @@ 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()); + match self.database.save(&self.pre_commit) { + Ok(collection) => { + self.database_cache = collection; + } + Err(err) => { + self.pre_commit = self.collection.clone(); + return Err(err.into()); + } } self.collection = self.pre_commit.clone(); self.filtered = self.filter_collection(); @@ -496,13 +501,13 @@ mod tests { .times(1) .in_sequence(&mut seq) .with(predicate::eq(with_artist.clone())) - .returning(|_| Ok(())); + .returning(|_| Ok(vec![])); database .expect_save() .times(1) .in_sequence(&mut seq) .with(predicate::eq(collection.clone())) - .returning(|_| Ok(())); + .returning(|_| Ok(vec![])); let mut music_hoard = MusicHoard::database(database).unwrap(); assert_eq!(music_hoard.collection, collection); @@ -524,7 +529,7 @@ mod tests { fn artist_sort_set_clear() { let mut database = MockIDatabase::new(); database.expect_load().times(1).returning(|| Ok(vec![])); - database.expect_save().times(4).returning(|_| Ok(())); + database.expect_save().times(4).returning(|_| Ok(vec![])); type MH = MusicHoard; let mut music_hoard: MH = MusicHoard::database(database).unwrap(); @@ -592,7 +597,7 @@ mod tests { fn set_clear_artist_mb_ref() { let mut database = MockIDatabase::new(); database.expect_load().times(1).returning(|| Ok(vec![])); - database.expect_save().times(3).returning(|_| Ok(())); + database.expect_save().times(3).returning(|_| Ok(vec![])); let mut artist_id = ArtistId::new("an artist"); let artist_id_2 = ArtistId::new("another artist"); @@ -638,7 +643,7 @@ mod tests { fn set_clear_artist_info() { let mut database = MockIDatabase::new(); database.expect_load().times(1).returning(|| Ok(vec![])); - database.expect_save().times(3).returning(|_| Ok(())); + database.expect_save().times(3).returning(|_| Ok(vec![])); let artist_id = ArtistId::new("an artist"); let artist_id_2 = ArtistId::new("another artist"); @@ -682,7 +687,7 @@ mod tests { fn add_to_remove_from_property() { let mut database = MockIDatabase::new(); database.expect_load().times(1).returning(|| Ok(vec![])); - database.expect_save().times(3).returning(|_| Ok(())); + database.expect_save().times(3).returning(|_| Ok(vec![])); let artist_id = ArtistId::new("an artist"); let artist_id_2 = ArtistId::new("another artist"); @@ -731,7 +736,7 @@ mod tests { fn set_clear_property() { let mut database = MockIDatabase::new(); database.expect_load().times(1).returning(|| Ok(vec![])); - database.expect_save().times(3).returning(|_| Ok(())); + database.expect_save().times(3).returning(|_| Ok(vec![])); let artist_id = ArtistId::new("an artist"); let artist_id_2 = ArtistId::new("another artist"); @@ -797,13 +802,13 @@ mod tests { .times(1) .in_sequence(&mut seq) .with(predicate::eq(with_album.clone())) - .returning(|_| Ok(())); + .returning(|_| Ok(vec![])); database .expect_save() .times(1) .in_sequence(&mut seq) .with(predicate::eq(collection.clone())) - .returning(|_| Ok(())); + .returning(|_| Ok(vec![])); let mut music_hoard = MusicHoard::database(database).unwrap(); assert_eq!(music_hoard.collection, collection); @@ -842,7 +847,7 @@ mod tests { .expect_load() .times(1) .return_once(|| Ok(database_result)); - database.expect_save().times(2).returning(|_| Ok(())); + database.expect_save().times(2).returning(|_| Ok(vec![])); let mut music_hoard = MusicHoard::database(database).unwrap(); let album = &music_hoard.collection[0].albums[0]; @@ -900,7 +905,7 @@ mod tests { .expect_load() .times(1) .return_once(|| Ok(database_result)); - database.expect_save().times(2).returning(|_| Ok(())); + database.expect_save().times(2).returning(|_| Ok(vec![])); let mut music_hoard = MusicHoard::database(database).unwrap(); assert_eq!(music_hoard.collection[0].albums[0].meta.seq, AlbumSeq(0)); @@ -940,7 +945,7 @@ mod tests { .expect_load() .times(1) .return_once(|| Ok(database_result)); - database.expect_save().times(2).returning(|_| Ok(())); + database.expect_save().times(2).returning(|_| Ok(vec![])); let mut music_hoard = MusicHoard::database(database).unwrap(); let meta = &music_hoard.collection[0].albums[0].meta; diff --git a/src/core/musichoard/library.rs b/src/core/musichoard/library.rs index 70eabe2..86abcee 100644 --- a/src/core/musichoard/library.rs +++ b/src/core/musichoard/library.rs @@ -157,7 +157,7 @@ mod tests { .expect_save() .with(predicate::eq(&*LIBRARY_COLLECTION)) .times(1) - .return_once(|_| Ok(())); + .return_once(|_| Ok(vec![])); let mut music_hoard = MusicHoard::new(database, library).unwrap(); diff --git a/src/external/database/json/mod.rs b/src/external/database/json/mod.rs index 0b7edf9..40762a3 100644 --- a/src/external/database/json/mod.rs +++ b/src/external/database/json/mod.rs @@ -55,11 +55,12 @@ impl IDatabase for JsonDatabase { Ok(database.into()) } - fn save(&mut self, collection: &Collection) -> Result<(), SaveError> { + fn save(&mut self, collection: &Collection) -> Result { let database: SerializeDatabase = collection.into(); let serialized = serde_json::to_string(&database)?; + let deserialized: DeserializeDatabase = serde_json::from_str(&serialized)?; self.backend.write(&serialized)?; - Ok(()) + Ok(deserialized.into()) } }