Move error into musichoard
All checks were successful
Cargo CI / Build and Test (pull_request) Successful in 1m4s
Cargo CI / Lint (pull_request) Successful in 43s

This commit is contained in:
Wojciech Kozlowski 2024-01-21 21:11:29 +01:00
parent 680939b749
commit 66fa2a98a2
3 changed files with 72 additions and 80 deletions

View File

@ -14,7 +14,7 @@ use super::album::Album;
use super::merge::{Merge, MergeSorted}; use super::merge::{Merge, MergeSorted};
// FIXME: these imports are not acceptable // FIXME: these imports are not acceptable
use crate::Error; use crate::core::musichoard::Error;
/// An object with the [`IMbid`] trait contains a [MusicBrainz /// An object with the [`IMbid`] trait contains a [MusicBrainz
/// Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID). /// Identifier](https://musicbrainz.org/doc/MusicBrainz_Identifier) (MBID).

View File

@ -1,18 +1,16 @@
use std::fmt::{self, Display};
use std::{collections::HashMap, mem}; use std::{collections::HashMap, mem};
use paste::paste; use paste::paste;
use super::collection::{ use super::collection::{
album::{Album, AlbumId}, album::{Album, AlbumId},
artist::{Artist, ArtistId}, artist::{Artist, ArtistId, InvalidUrlError},
track::{Quality, Track, TrackId}, track::{Quality, Track, TrackId},
Collection, Merge, Collection, Merge,
}; };
use super::database::IDatabase; use super::database::{self, IDatabase};
use super::library::{ILibrary, Item, Query}; use super::library::{self, ILibrary, Item, Query};
// FIXME: should not be importing this error.
use crate::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.
@ -372,6 +370,71 @@ impl<LIB, DB> MusicHoardBuilder<LIB, DB> {
} }
} }
/// Error type for `musichoard`.
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// The [`MusicHoard`] is not able to read/write its in-memory collection.
CollectionError(String),
/// The [`MusicHoard`] failed to read/write from/to the library.
LibraryError(String),
/// The [`MusicHoard`] failed to read/write from/to the database.
DatabaseError(String),
/// The [`MusicHoard`] failed to parse a user-provided URL.
UrlParseError(String),
/// The user-provided URL is not valid.
InvalidUrlError(String),
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::CollectionError(ref s) => write!(f, "failed to read/write the collection: {s}"),
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}")
}
Self::UrlParseError(ref s) => write!(f, "failed to parse a user-provided URL: {s}"),
Self::InvalidUrlError(ref s) => write!(f, "user-provided URL is invalid: {s}"),
}
}
}
impl From<library::Error> for Error {
fn from(err: library::Error) -> Error {
Error::LibraryError(err.to_string())
}
}
impl From<database::LoadError> for Error {
fn from(err: database::LoadError) -> Error {
Error::DatabaseError(err.to_string())
}
}
impl From<database::SaveError> for Error {
fn from(err: database::SaveError) -> Error {
Error::DatabaseError(err.to_string())
}
}
impl From<url::ParseError> for Error {
fn from(err: url::ParseError) -> Error {
Error::UrlParseError(err.to_string())
}
}
impl From<uuid::Error> for Error {
fn from(err: uuid::Error) -> Error {
Error::UrlParseError(err.to_string())
}
}
impl From<InvalidUrlError> for Error {
fn from(err: InvalidUrlError) -> Error {
Error::InvalidUrlError(err.to_string())
}
}
#[cfg(test)] #[cfg(test)]
pub mod testmod; pub mod testmod;

View File

@ -3,8 +3,6 @@
// FIXME: make private and only provide via re-exports. // FIXME: make private and only provide via re-exports.
pub mod core; pub mod core;
use std::fmt::{self, Display};
// FIXME: validate the re-exports. // FIXME: validate the re-exports.
pub use core::database; pub use core::database;
pub use core::library; pub use core::library;
@ -12,82 +10,13 @@ pub use core::library;
// FIXME: validate the re-exports. // FIXME: validate the re-exports.
pub use core::collection::{ pub use core::collection::{
album::{Album, AlbumId}, album::{Album, AlbumId},
artist::{ artist::{Artist, ArtistId, ArtistProperties, Bandcamp, MusicBrainz, MusicButler, Qobuz},
Artist, ArtistId, ArtistProperties, Bandcamp, InvalidUrlError, MusicBrainz, MusicButler,
Qobuz,
},
track::{Format, Quality, Track, TrackId}, track::{Format, Quality, Track, TrackId},
Collection, Collection,
}; };
// FIXME: validate the re-exports // FIXME: validate the re-exports
pub use core::musichoard::{MusicHoard, MusicHoardBuilder, NoDatabase, NoLibrary}; pub use core::musichoard::{Error, MusicHoard, MusicHoardBuilder, NoDatabase, NoLibrary};
// FIXME: this should be in the musichoard module.
/// Error type for `musichoard`.
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
/// The [`MusicHoard`] is not able to read/write its in-memory collection.
CollectionError(String),
/// The [`MusicHoard`] failed to read/write from/to the library.
LibraryError(String),
/// The [`MusicHoard`] failed to read/write from/to the database.
DatabaseError(String),
/// The [`MusicHoard`] failed to parse a user-provided URL.
UrlParseError(String),
/// The user-provided URL is not valid.
InvalidUrlError(String),
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::CollectionError(ref s) => write!(f, "failed to read/write the collection: {s}"),
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}")
}
Self::UrlParseError(ref s) => write!(f, "failed to parse a user-provided URL: {s}"),
Self::InvalidUrlError(ref s) => write!(f, "user-provided URL is invalid: {s}"),
}
}
}
impl From<library::Error> for Error {
fn from(err: library::Error) -> Error {
Error::LibraryError(err.to_string())
}
}
impl From<database::LoadError> for Error {
fn from(err: database::LoadError) -> Error {
Error::DatabaseError(err.to_string())
}
}
impl From<database::SaveError> for Error {
fn from(err: database::SaveError) -> Error {
Error::DatabaseError(err.to_string())
}
}
impl From<url::ParseError> for Error {
fn from(err: url::ParseError) -> Error {
Error::UrlParseError(err.to_string())
}
}
impl From<uuid::Error> for Error {
fn from(err: uuid::Error) -> Error {
Error::UrlParseError(err.to_string())
}
}
impl From<InvalidUrlError> for Error {
fn from(err: InvalidUrlError) -> Error {
Error::InvalidUrlError(err.to_string())
}
}
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]