Database and library made optional
Some checks failed
Cargo CI / Build and Test (pull_request) Failing after 2m20s
Cargo CI / Lint (pull_request) Successful in 44s

This commit is contained in:
Wojciech Kozlowski 2024-01-07 10:21:33 +01:00
parent d7384476d4
commit 04c19ea3e0
3 changed files with 43 additions and 28 deletions

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

View File

@ -42,7 +42,7 @@ struct Opt {
} }
fn with<LIB: ILibrary, DB: IDatabase>(lib: LIB, db: DB) { 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. // Initialize the terminal user interface.
let backend = CrosstermBackend::new(io::stdout()); 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 backend = JsonDatabaseFileBackend::new(&*database::json::DATABASE_TEST_FILE);
let database = JsonDatabase::new(backend); 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.rescan_library().unwrap();
music_hoard.load_from_database().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 backend = JsonDatabaseFileBackend::new(&*database::json::DATABASE_TEST_FILE);
let database = JsonDatabase::new(backend); 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.load_from_database().unwrap();
music_hoard.rescan_library().unwrap(); music_hoard.rescan_library().unwrap();