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