Make database and library optional #86
65
src/lib.rs
65
src/lib.rs
@ -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,30 +476,45 @@ 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 {
|
||||||
Ok(())
|
Some(ref mut database) => {
|
||||||
|
database.save(&self.collection)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
None => Err(Error::DatabaseError(String::from("database not provided"))),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_collection(&self) -> &Collection {
|
pub fn get_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(
|
||||||
|
@ -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());
|
||||||
|
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user