Break down the musichoard files #165
@ -1,7 +1,9 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{
|
core::{
|
||||||
interface::{database::IDatabase, library::ILibrary},
|
interface::{database::IDatabase, library::ILibrary},
|
||||||
musichoard::musichoard::{MusicHoard, NoDatabase, NoLibrary},
|
musichoard::{MusicHoard, NoDatabase, NoLibrary},
|
||||||
},
|
},
|
||||||
Error,
|
Error,
|
||||||
};
|
};
|
||||||
@ -61,6 +63,20 @@ impl MusicHoardBuilder<NoDatabase, NoLibrary> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MusicHoard<NoDatabase, NoLibrary> {
|
||||||
|
/// Create a new [`MusicHoard`] without any library or database.
|
||||||
|
pub fn empty() -> Self {
|
||||||
|
MusicHoard {
|
||||||
|
collection: vec![],
|
||||||
|
pre_commit: vec![],
|
||||||
|
database: NoDatabase,
|
||||||
|
database_cache: vec![],
|
||||||
|
library: NoLibrary,
|
||||||
|
library_cache: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<Library: ILibrary> MusicHoardBuilder<NoDatabase, Library> {
|
impl<Library: ILibrary> MusicHoardBuilder<NoDatabase, Library> {
|
||||||
/// Build [`MusicHoard`] with the currently set library and database.
|
/// Build [`MusicHoard`] with the currently set library and database.
|
||||||
pub fn build(self) -> MusicHoard<NoDatabase, Library> {
|
pub fn build(self) -> MusicHoard<NoDatabase, Library> {
|
||||||
@ -68,6 +84,20 @@ impl<Library: ILibrary> MusicHoardBuilder<NoDatabase, Library> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Library: ILibrary> MusicHoard<NoDatabase, Library> {
|
||||||
|
/// Create a new [`MusicHoard`] with the provided [`ILibrary`] and no database.
|
||||||
|
pub fn library(library: Library) -> Self {
|
||||||
|
MusicHoard {
|
||||||
|
collection: vec![],
|
||||||
|
pre_commit: vec![],
|
||||||
|
database: NoDatabase,
|
||||||
|
database_cache: vec![],
|
||||||
|
library,
|
||||||
|
library_cache: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<Database: IDatabase> MusicHoardBuilder<Database, NoLibrary> {
|
impl<Database: IDatabase> MusicHoardBuilder<Database, NoLibrary> {
|
||||||
/// Build [`MusicHoard`] with the currently set library and database.
|
/// Build [`MusicHoard`] with the currently set library and database.
|
||||||
pub fn build(self) -> Result<MusicHoard<Database, NoLibrary>, Error> {
|
pub fn build(self) -> Result<MusicHoard<Database, NoLibrary>, Error> {
|
||||||
@ -75,6 +105,22 @@ impl<Database: IDatabase> MusicHoardBuilder<Database, NoLibrary> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Database: IDatabase> MusicHoard<Database, NoLibrary> {
|
||||||
|
/// Create a new [`MusicHoard`] with the provided [`IDatabase`] and no library.
|
||||||
|
pub fn database(database: Database) -> Result<Self, Error> {
|
||||||
|
let mut mh = MusicHoard {
|
||||||
|
collection: vec![],
|
||||||
|
pre_commit: vec![],
|
||||||
|
database,
|
||||||
|
database_cache: vec![],
|
||||||
|
library: NoLibrary,
|
||||||
|
library_cache: HashMap::new(),
|
||||||
|
};
|
||||||
|
mh.reload_database()?;
|
||||||
|
Ok(mh)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<Database: IDatabase, Library: ILibrary> MusicHoardBuilder<Database, Library> {
|
impl<Database: IDatabase, Library: ILibrary> MusicHoardBuilder<Database, Library> {
|
||||||
/// Build [`MusicHoard`] with the currently set library and database.
|
/// Build [`MusicHoard`] with the currently set library and database.
|
||||||
pub fn build(self) -> Result<MusicHoard<Database, Library>, Error> {
|
pub fn build(self) -> Result<MusicHoard<Database, Library>, Error> {
|
||||||
@ -82,6 +128,22 @@ impl<Database: IDatabase, Library: ILibrary> MusicHoardBuilder<Database, Library
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<Database: IDatabase, Library: ILibrary> MusicHoard<Database, Library> {
|
||||||
|
/// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`].
|
||||||
|
pub fn new(database: Database, library: Library) -> Result<Self, Error> {
|
||||||
|
let mut mh = MusicHoard {
|
||||||
|
collection: vec![],
|
||||||
|
pre_commit: vec![],
|
||||||
|
database,
|
||||||
|
database_cache: vec![],
|
||||||
|
library,
|
||||||
|
library_cache: HashMap::new(),
|
||||||
|
};
|
||||||
|
mh.reload_database()?;
|
||||||
|
Ok(mh)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::core::interface::{database::NullDatabase, library::NullLibrary};
|
use crate::core::interface::{database::NullDatabase, library::NullLibrary};
|
||||||
|
@ -4,13 +4,49 @@
|
|||||||
pub mod builder;
|
pub mod builder;
|
||||||
pub mod musichoard;
|
pub mod musichoard;
|
||||||
|
|
||||||
use std::fmt::{self, Display};
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
fmt::{self, Display},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::core::collection::{
|
||||||
|
artist::{Artist, ArtistId},
|
||||||
|
Collection,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
collection,
|
collection,
|
||||||
interface::{database, library},
|
interface::{database, library},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// The Music Hoard. It is responsible for pulling information from both the library and the
|
||||||
|
/// database, ensuring its consistent and writing back any changes.
|
||||||
|
// TODO: Split into inner and external/interfaces to facilitate building.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct MusicHoard<Database, Library> {
|
||||||
|
collection: Collection,
|
||||||
|
pre_commit: Collection,
|
||||||
|
database: Database,
|
||||||
|
database_cache: Collection,
|
||||||
|
library: Library,
|
||||||
|
library_cache: HashMap<ArtistId, Artist>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Phantom type for when a library implementation is not needed.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct NoLibrary;
|
||||||
|
|
||||||
|
/// Phantom type for when a database implementation is not needed.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct NoDatabase;
|
||||||
|
|
||||||
|
impl Default for MusicHoard<NoDatabase, NoLibrary> {
|
||||||
|
/// Create a new [`MusicHoard`] without any library or database.
|
||||||
|
fn default() -> Self {
|
||||||
|
MusicHoard::empty()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Error type for `musichoard`.
|
/// Error type for `musichoard`.
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -12,50 +12,9 @@ use crate::core::{
|
|||||||
database::IDatabase,
|
database::IDatabase,
|
||||||
library::{ILibrary, Item, Query},
|
library::{ILibrary, Item, Query},
|
||||||
},
|
},
|
||||||
musichoard::Error,
|
musichoard::{Error, MusicHoard, NoDatabase},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The Music Hoard. It is responsible for pulling information from both the library and the
|
|
||||||
/// database, ensuring its consistent and writing back any changes.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct MusicHoard<Database, Library> {
|
|
||||||
collection: Collection,
|
|
||||||
pre_commit: Collection,
|
|
||||||
database: Database,
|
|
||||||
database_cache: Collection,
|
|
||||||
library: Library,
|
|
||||||
library_cache: HashMap<ArtistId, Artist>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Phantom type for when a library implementation is not needed.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct NoLibrary;
|
|
||||||
|
|
||||||
/// Phantom type for when a database implementation is not needed.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct NoDatabase;
|
|
||||||
|
|
||||||
impl Default for MusicHoard<NoDatabase, NoLibrary> {
|
|
||||||
/// Create a new [`MusicHoard`] without any library or database.
|
|
||||||
fn default() -> Self {
|
|
||||||
MusicHoard::empty()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MusicHoard<NoDatabase, NoLibrary> {
|
|
||||||
/// Create a new [`MusicHoard`] without any library or database.
|
|
||||||
pub fn empty() -> Self {
|
|
||||||
MusicHoard {
|
|
||||||
collection: vec![],
|
|
||||||
pre_commit: vec![],
|
|
||||||
database: NoDatabase,
|
|
||||||
database_cache: vec![],
|
|
||||||
library: NoLibrary,
|
|
||||||
library_cache: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Database, Library> MusicHoard<Database, Library> {
|
impl<Database, Library> MusicHoard<Database, Library> {
|
||||||
/// Retrieve the [`Collection`].
|
/// Retrieve the [`Collection`].
|
||||||
pub fn get_collection(&self) -> &Collection {
|
pub fn get_collection(&self) -> &Collection {
|
||||||
@ -192,18 +151,6 @@ impl<Database, Library> MusicHoard<Database, Library> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Library: ILibrary> MusicHoard<NoDatabase, Library> {
|
impl<Library: ILibrary> MusicHoard<NoDatabase, Library> {
|
||||||
/// Create a new [`MusicHoard`] with the provided [`ILibrary`] and no database.
|
|
||||||
pub fn library(library: Library) -> Self {
|
|
||||||
MusicHoard {
|
|
||||||
collection: vec![],
|
|
||||||
pre_commit: vec![],
|
|
||||||
database: NoDatabase,
|
|
||||||
database_cache: vec![],
|
|
||||||
library,
|
|
||||||
library_cache: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Rescan the library and merge with the in-memory collection.
|
/// Rescan the library and merge with the in-memory collection.
|
||||||
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
||||||
self.pre_commit = self.rescan_library_inner()?;
|
self.pre_commit = self.rescan_library_inner()?;
|
||||||
@ -221,22 +168,6 @@ impl<Database, Library: ILibrary> MusicHoard<Database, Library> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Database: IDatabase> MusicHoard<Database, NoLibrary> {
|
|
||||||
/// Create a new [`MusicHoard`] with the provided [`IDatabase`] and no library.
|
|
||||||
pub fn database(database: Database) -> Result<Self, Error> {
|
|
||||||
let mut mh = MusicHoard {
|
|
||||||
collection: vec![],
|
|
||||||
pre_commit: vec![],
|
|
||||||
database,
|
|
||||||
database_cache: vec![],
|
|
||||||
library: NoLibrary,
|
|
||||||
library_cache: HashMap::new(),
|
|
||||||
};
|
|
||||||
mh.reload_database()?;
|
|
||||||
Ok(mh)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Library> MusicHoard<NoDatabase, Library> {
|
impl<Library> MusicHoard<NoDatabase, Library> {
|
||||||
fn commit(&mut self) -> Result<(), Error> {
|
fn commit(&mut self) -> Result<(), Error> {
|
||||||
self.collection = self.pre_commit.clone();
|
self.collection = self.pre_commit.clone();
|
||||||
@ -449,20 +380,6 @@ impl<Database: IDatabase, Library> MusicHoard<Database, Library> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<Database: IDatabase, Library: ILibrary> MusicHoard<Database, Library> {
|
impl<Database: IDatabase, Library: ILibrary> MusicHoard<Database, Library> {
|
||||||
/// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`].
|
|
||||||
pub fn new(database: Database, library: Library) -> Result<Self, Error> {
|
|
||||||
let mut mh = MusicHoard {
|
|
||||||
collection: vec![],
|
|
||||||
pre_commit: vec![],
|
|
||||||
database,
|
|
||||||
database_cache: vec![],
|
|
||||||
library,
|
|
||||||
library_cache: HashMap::new(),
|
|
||||||
};
|
|
||||||
mh.reload_database()?;
|
|
||||||
Ok(mh)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Rescan the library and merge with the in-memory collection.
|
/// Rescan the library and merge with the in-memory collection.
|
||||||
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
||||||
self.pre_commit = self.rescan_library_inner()?;
|
self.pre_commit = self.rescan_library_inner()?;
|
||||||
@ -480,6 +397,7 @@ mod tests {
|
|||||||
database::{self, MockIDatabase},
|
database::{self, MockIDatabase},
|
||||||
library::{self, testmod::LIBRARY_ITEMS, MockILibrary},
|
library::{self, testmod::LIBRARY_ITEMS, MockILibrary},
|
||||||
},
|
},
|
||||||
|
musichoard::NoLibrary,
|
||||||
testmod::{FULL_COLLECTION, LIBRARY_COLLECTION},
|
testmod::{FULL_COLLECTION, LIBRARY_COLLECTION},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,11 +7,7 @@ pub mod library;
|
|||||||
pub use core::collection;
|
pub use core::collection;
|
||||||
pub use core::interface;
|
pub use core::interface;
|
||||||
|
|
||||||
pub use core::musichoard::{
|
pub use core::musichoard::{builder::MusicHoardBuilder, Error, MusicHoard, NoDatabase, NoLibrary};
|
||||||
builder::MusicHoardBuilder,
|
|
||||||
musichoard::{MusicHoard, NoDatabase, NoLibrary},
|
|
||||||
Error,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
|
Loading…
Reference in New Issue
Block a user