Code fix
This commit is contained in:
parent
226c583117
commit
ce3571e393
@ -13,8 +13,8 @@ pub trait IDatabase {
|
|||||||
/// Load collection from the database.
|
/// Load collection from the database.
|
||||||
fn load(&self) -> Result<Collection, LoadError>;
|
fn load(&self) -> Result<Collection, LoadError>;
|
||||||
|
|
||||||
/// Save collection to the database.
|
/// Save collection to the database. Return the written data.
|
||||||
fn save(&mut self, collection: &Collection) -> Result<(), SaveError>;
|
fn save(&mut self, collection: &Collection) -> Result<Collection, SaveError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Null database implementation of [`IDatabase`].
|
/// Null database implementation of [`IDatabase`].
|
||||||
@ -25,8 +25,8 @@ impl IDatabase for NullDatabase {
|
|||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save(&mut self, _collection: &Collection) -> Result<(), SaveError> {
|
fn save(&mut self, _collection: &Collection) -> Result<Collection, SaveError> {
|
||||||
Ok(())
|
Ok(vec![])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,10 +374,15 @@ impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> {
|
|||||||
impl<Database: IDatabase, Library> IMusicHoardDatabasePrivate for MusicHoard<Database, Library> {
|
impl<Database: IDatabase, Library> IMusicHoardDatabasePrivate for MusicHoard<Database, Library> {
|
||||||
fn commit(&mut self) -> Result<(), Error> {
|
fn commit(&mut self) -> Result<(), Error> {
|
||||||
if self.collection != self.pre_commit {
|
if self.collection != self.pre_commit {
|
||||||
if let Err(err) = self.database.save(&self.pre_commit) {
|
match self.database.save(&self.pre_commit) {
|
||||||
|
Ok(collection) => {
|
||||||
|
self.database_cache = collection;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
self.pre_commit = self.collection.clone();
|
self.pre_commit = self.collection.clone();
|
||||||
return Err(err.into());
|
return Err(err.into());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.collection = self.pre_commit.clone();
|
self.collection = self.pre_commit.clone();
|
||||||
self.filtered = self.filter_collection();
|
self.filtered = self.filter_collection();
|
||||||
}
|
}
|
||||||
@ -496,13 +501,13 @@ mod tests {
|
|||||||
.times(1)
|
.times(1)
|
||||||
.in_sequence(&mut seq)
|
.in_sequence(&mut seq)
|
||||||
.with(predicate::eq(with_artist.clone()))
|
.with(predicate::eq(with_artist.clone()))
|
||||||
.returning(|_| Ok(()));
|
.returning(|_| Ok(vec![]));
|
||||||
database
|
database
|
||||||
.expect_save()
|
.expect_save()
|
||||||
.times(1)
|
.times(1)
|
||||||
.in_sequence(&mut seq)
|
.in_sequence(&mut seq)
|
||||||
.with(predicate::eq(collection.clone()))
|
.with(predicate::eq(collection.clone()))
|
||||||
.returning(|_| Ok(()));
|
.returning(|_| Ok(vec![]));
|
||||||
|
|
||||||
let mut music_hoard = MusicHoard::database(database).unwrap();
|
let mut music_hoard = MusicHoard::database(database).unwrap();
|
||||||
assert_eq!(music_hoard.collection, collection);
|
assert_eq!(music_hoard.collection, collection);
|
||||||
@ -524,7 +529,7 @@ mod tests {
|
|||||||
fn artist_sort_set_clear() {
|
fn artist_sort_set_clear() {
|
||||||
let mut database = MockIDatabase::new();
|
let mut database = MockIDatabase::new();
|
||||||
database.expect_load().times(1).returning(|| Ok(vec![]));
|
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<MockIDatabase, NoLibrary>;
|
type MH = MusicHoard<MockIDatabase, NoLibrary>;
|
||||||
let mut music_hoard: MH = MusicHoard::database(database).unwrap();
|
let mut music_hoard: MH = MusicHoard::database(database).unwrap();
|
||||||
@ -592,7 +597,7 @@ mod tests {
|
|||||||
fn set_clear_artist_mb_ref() {
|
fn set_clear_artist_mb_ref() {
|
||||||
let mut database = MockIDatabase::new();
|
let mut database = MockIDatabase::new();
|
||||||
database.expect_load().times(1).returning(|| Ok(vec![]));
|
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 mut artist_id = ArtistId::new("an artist");
|
||||||
let artist_id_2 = ArtistId::new("another artist");
|
let artist_id_2 = ArtistId::new("another artist");
|
||||||
@ -638,7 +643,7 @@ mod tests {
|
|||||||
fn set_clear_artist_info() {
|
fn set_clear_artist_info() {
|
||||||
let mut database = MockIDatabase::new();
|
let mut database = MockIDatabase::new();
|
||||||
database.expect_load().times(1).returning(|| Ok(vec![]));
|
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 = ArtistId::new("an artist");
|
||||||
let artist_id_2 = ArtistId::new("another artist");
|
let artist_id_2 = ArtistId::new("another artist");
|
||||||
@ -682,7 +687,7 @@ mod tests {
|
|||||||
fn add_to_remove_from_property() {
|
fn add_to_remove_from_property() {
|
||||||
let mut database = MockIDatabase::new();
|
let mut database = MockIDatabase::new();
|
||||||
database.expect_load().times(1).returning(|| Ok(vec![]));
|
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 = ArtistId::new("an artist");
|
||||||
let artist_id_2 = ArtistId::new("another artist");
|
let artist_id_2 = ArtistId::new("another artist");
|
||||||
@ -731,7 +736,7 @@ mod tests {
|
|||||||
fn set_clear_property() {
|
fn set_clear_property() {
|
||||||
let mut database = MockIDatabase::new();
|
let mut database = MockIDatabase::new();
|
||||||
database.expect_load().times(1).returning(|| Ok(vec![]));
|
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 = ArtistId::new("an artist");
|
||||||
let artist_id_2 = ArtistId::new("another artist");
|
let artist_id_2 = ArtistId::new("another artist");
|
||||||
@ -797,13 +802,13 @@ mod tests {
|
|||||||
.times(1)
|
.times(1)
|
||||||
.in_sequence(&mut seq)
|
.in_sequence(&mut seq)
|
||||||
.with(predicate::eq(with_album.clone()))
|
.with(predicate::eq(with_album.clone()))
|
||||||
.returning(|_| Ok(()));
|
.returning(|_| Ok(vec![]));
|
||||||
database
|
database
|
||||||
.expect_save()
|
.expect_save()
|
||||||
.times(1)
|
.times(1)
|
||||||
.in_sequence(&mut seq)
|
.in_sequence(&mut seq)
|
||||||
.with(predicate::eq(collection.clone()))
|
.with(predicate::eq(collection.clone()))
|
||||||
.returning(|_| Ok(()));
|
.returning(|_| Ok(vec![]));
|
||||||
|
|
||||||
let mut music_hoard = MusicHoard::database(database).unwrap();
|
let mut music_hoard = MusicHoard::database(database).unwrap();
|
||||||
assert_eq!(music_hoard.collection, collection);
|
assert_eq!(music_hoard.collection, collection);
|
||||||
@ -842,7 +847,7 @@ mod tests {
|
|||||||
.expect_load()
|
.expect_load()
|
||||||
.times(1)
|
.times(1)
|
||||||
.return_once(|| Ok(database_result));
|
.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 mut music_hoard = MusicHoard::database(database).unwrap();
|
||||||
let album = &music_hoard.collection[0].albums[0];
|
let album = &music_hoard.collection[0].albums[0];
|
||||||
@ -900,7 +905,7 @@ mod tests {
|
|||||||
.expect_load()
|
.expect_load()
|
||||||
.times(1)
|
.times(1)
|
||||||
.return_once(|| Ok(database_result));
|
.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 mut music_hoard = MusicHoard::database(database).unwrap();
|
||||||
assert_eq!(music_hoard.collection[0].albums[0].meta.seq, AlbumSeq(0));
|
assert_eq!(music_hoard.collection[0].albums[0].meta.seq, AlbumSeq(0));
|
||||||
@ -940,7 +945,7 @@ mod tests {
|
|||||||
.expect_load()
|
.expect_load()
|
||||||
.times(1)
|
.times(1)
|
||||||
.return_once(|| Ok(database_result));
|
.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 mut music_hoard = MusicHoard::database(database).unwrap();
|
||||||
|
|
||||||
let meta = &music_hoard.collection[0].albums[0].meta;
|
let meta = &music_hoard.collection[0].albums[0].meta;
|
||||||
|
@ -157,7 +157,7 @@ mod tests {
|
|||||||
.expect_save()
|
.expect_save()
|
||||||
.with(predicate::eq(&*LIBRARY_COLLECTION))
|
.with(predicate::eq(&*LIBRARY_COLLECTION))
|
||||||
.times(1)
|
.times(1)
|
||||||
.return_once(|_| Ok(()));
|
.return_once(|_| Ok(vec![]));
|
||||||
|
|
||||||
let mut music_hoard = MusicHoard::new(database, library).unwrap();
|
let mut music_hoard = MusicHoard::new(database, library).unwrap();
|
||||||
|
|
||||||
|
@ -55,11 +55,12 @@ impl<JDB: IJsonDatabaseBackend> IDatabase for JsonDatabase<JDB> {
|
|||||||
Ok(database.into())
|
Ok(database.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save(&mut self, collection: &Collection) -> Result<(), SaveError> {
|
fn save(&mut self, collection: &Collection) -> Result<Collection, SaveError> {
|
||||||
let database: SerializeDatabase = collection.into();
|
let database: SerializeDatabase = collection.into();
|
||||||
let serialized = serde_json::to_string(&database)?;
|
let serialized = serde_json::to_string(&database)?;
|
||||||
|
let deserialized: DeserializeDatabase = serde_json::from_str(&serialized)?;
|
||||||
self.backend.write(&serialized)?;
|
self.backend.write(&serialized)?;
|
||||||
Ok(())
|
Ok(deserialized.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user