2023-03-29 01:29:11 +02:00
|
|
|
//! MusicHoard - a music collection manager.
|
|
|
|
|
2023-03-29 08:37:01 +02:00
|
|
|
pub mod database;
|
2023-03-31 14:24:54 +02:00
|
|
|
pub mod library;
|
2023-03-29 01:29:11 +02:00
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use database::IDatabase;
|
|
|
|
use library::{ILibrary, Query};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-03-29 01:29:11 +02:00
|
|
|
/// [MusicBrainz Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).
|
2023-03-29 08:37:01 +02:00
|
|
|
pub type Mbid = Uuid;
|
2023-03-29 01:29:11 +02:00
|
|
|
|
2023-04-10 11:27:07 +02:00
|
|
|
/// The track file format.
|
2023-05-11 21:45:23 +02:00
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
|
|
|
pub enum Format {
|
2023-04-10 11:27:07 +02:00
|
|
|
Flac,
|
2023-05-11 21:45:23 +02:00
|
|
|
Mp3,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The track quality. Combines format and bitrate information.
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
|
|
|
pub struct Quality {
|
|
|
|
pub format: Format,
|
|
|
|
pub bitrate: u32,
|
2023-04-10 11:27:07 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:24:54 +02:00
|
|
|
/// A single track on an album.
|
2023-04-10 00:13:18 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
2023-03-29 01:29:11 +02:00
|
|
|
pub struct Track {
|
|
|
|
pub number: u32,
|
|
|
|
pub title: String,
|
2023-03-31 14:24:54 +02:00
|
|
|
pub artist: Vec<String>,
|
2023-05-10 23:44:02 +02:00
|
|
|
pub quality: Quality,
|
2023-03-28 22:49:59 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 14:24:54 +02:00
|
|
|
/// The album identifier.
|
2023-04-10 00:13:18 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
|
2023-03-31 14:24:54 +02:00
|
|
|
pub struct AlbumId {
|
|
|
|
pub year: u32,
|
|
|
|
pub title: String,
|
2023-03-29 01:29:11 +02:00
|
|
|
}
|
2023-03-28 22:49:59 +02:00
|
|
|
|
2023-03-31 14:24:54 +02:00
|
|
|
/// An album is a collection of tracks that were released together.
|
2023-04-10 00:13:18 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
2023-03-31 14:24:54 +02:00
|
|
|
pub struct Album {
|
|
|
|
pub id: AlbumId,
|
|
|
|
pub tracks: Vec<Track>,
|
2023-03-28 22:49:59 +02:00
|
|
|
}
|
2023-04-01 01:59:59 +02:00
|
|
|
|
|
|
|
/// The artist identifier.
|
2023-04-10 00:13:18 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)]
|
2023-04-01 01:59:59 +02:00
|
|
|
pub struct ArtistId {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An artist.
|
2023-04-10 00:13:18 +02:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
2023-04-01 01:59:59 +02:00
|
|
|
pub struct Artist {
|
|
|
|
pub id: ArtistId,
|
|
|
|
pub albums: Vec<Album>,
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
/// The collection type. Currently, a collection is a list of artists.
|
|
|
|
pub type Collection = Vec<Artist>;
|
|
|
|
|
|
|
|
/// Error type for `musichoard`.
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub enum Error {
|
|
|
|
/// The [`MusicHoard`] failed to read/write from/to the library.
|
|
|
|
LibraryError(String),
|
|
|
|
/// The [`MusicHoard`] failed to read/write from/to the database.
|
|
|
|
DatabaseError(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Self::LibraryError(ref s) => write!(f, "failed to read/write from/to the library: {s}"),
|
|
|
|
Self::DatabaseError(ref s) => {
|
|
|
|
write!(f, "failed to read/write from/to the database: {s}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<library::Error> for Error {
|
|
|
|
fn from(err: library::Error) -> Error {
|
|
|
|
Error::LibraryError(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<database::Error> for Error {
|
|
|
|
fn from(err: database::Error) -> Error {
|
|
|
|
Error::DatabaseError(err.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Music Hoard. It is responsible for pulling information from both the library and the
|
|
|
|
/// database, ensuring its consistent and writing back any changes.
|
|
|
|
pub struct MusicHoard<LIB, DB> {
|
|
|
|
library: LIB,
|
|
|
|
database: DB,
|
|
|
|
collection: Collection,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<LIB: ILibrary, DB: IDatabase> MusicHoard<LIB, DB> {
|
|
|
|
/// Create a new [`MusicHoard`] with the provided [`ILibrary`] and [`IDatabase`].
|
|
|
|
pub fn new(library: LIB, database: DB) -> Self {
|
|
|
|
MusicHoard {
|
|
|
|
library,
|
|
|
|
database,
|
|
|
|
collection: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rescan_library(&mut self) -> Result<(), Error> {
|
|
|
|
self.collection = self.library.list(&Query::new())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_to_database(&mut self) -> Result<(), Error> {
|
|
|
|
self.database.write(&self.collection)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_collection(&self) -> &Collection {
|
|
|
|
&self.collection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-01 01:59:59 +02:00
|
|
|
#[cfg(test)]
|
2023-04-13 14:09:59 +02:00
|
|
|
#[macro_use]
|
|
|
|
mod testlib;
|
2023-04-01 01:59:59 +02:00
|
|
|
|
2023-04-13 14:09:59 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-05-10 22:52:03 +02:00
|
|
|
use mockall::predicate;
|
2023-04-10 00:13:18 +02:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2023-05-10 22:52:03 +02:00
|
|
|
use crate::{database::MockIDatabase, library::MockILibrary};
|
|
|
|
|
2023-04-13 14:09:59 +02:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| collection!());
|
2023-05-10 22:52:03 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn read_get_write() {
|
|
|
|
let mut library = MockILibrary::new();
|
|
|
|
let mut database = MockIDatabase::new();
|
|
|
|
|
|
|
|
let library_input = Query::new();
|
|
|
|
let library_result = Ok(COLLECTION.to_owned());
|
|
|
|
|
|
|
|
let database_input = COLLECTION.to_owned();
|
|
|
|
let database_result = Ok(());
|
|
|
|
|
|
|
|
library
|
|
|
|
.expect_list()
|
|
|
|
.with(predicate::eq(library_input))
|
|
|
|
.times(1)
|
|
|
|
.return_once(|_| library_result);
|
|
|
|
|
|
|
|
database
|
|
|
|
.expect_write()
|
|
|
|
.with(predicate::eq(database_input))
|
|
|
|
.times(1)
|
|
|
|
.return_once(|_: &Collection| database_result);
|
|
|
|
|
|
|
|
let mut music_hoard = MusicHoard::new(library, database);
|
|
|
|
|
|
|
|
music_hoard.rescan_library().unwrap();
|
|
|
|
assert_eq!(music_hoard.get_collection(), &*COLLECTION);
|
|
|
|
music_hoard.save_to_database().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn library_error() {
|
|
|
|
let mut library = MockILibrary::new();
|
|
|
|
let database = MockIDatabase::new();
|
|
|
|
|
|
|
|
let library_result = Err(library::Error::Invalid(String::from("invalid data")));
|
|
|
|
|
|
|
|
library
|
|
|
|
.expect_list()
|
|
|
|
.times(1)
|
|
|
|
.return_once(|_| library_result);
|
|
|
|
|
|
|
|
let mut music_hoard = MusicHoard::new(library, database);
|
|
|
|
|
|
|
|
let actual_err = music_hoard.rescan_library().unwrap_err();
|
|
|
|
let expected_err =
|
|
|
|
Error::LibraryError(library::Error::Invalid(String::from("invalid data")).to_string());
|
|
|
|
|
|
|
|
assert_eq!(actual_err, expected_err);
|
|
|
|
assert_eq!(actual_err.to_string(), expected_err.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn database_error() {
|
|
|
|
let library = MockILibrary::new();
|
|
|
|
let mut database = MockIDatabase::new();
|
|
|
|
|
|
|
|
let database_result = Err(database::Error::IoError(String::from("I/O error")));
|
|
|
|
|
|
|
|
database
|
|
|
|
.expect_write()
|
|
|
|
.times(1)
|
|
|
|
.return_once(|_: &Collection| database_result);
|
|
|
|
|
|
|
|
let mut music_hoard = MusicHoard::new(library, database);
|
|
|
|
|
|
|
|
let actual_err = music_hoard.save_to_database().unwrap_err();
|
|
|
|
let expected_err =
|
|
|
|
Error::DatabaseError(database::Error::IoError(String::from("I/O error")).to_string());
|
|
|
|
|
|
|
|
assert_eq!(actual_err, expected_err);
|
|
|
|
assert_eq!(actual_err.to_string(), expected_err.to_string());
|
|
|
|
}
|
2023-04-01 01:59:59 +02:00
|
|
|
}
|