use serde::Deserialize;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Debug)]
pub enum Error {
ParsingError(serde_json::error::Error),
HTTPError(reqwest::Error),
LastFMError(LastFMErrorResponse),
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match *self {
Error::ParsingError(ref e) => Some(e),
Error::HTTPError(ref e) => Some(e),
Error::LastFMError(_) => None,
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match *self {
Error::ParsingError(ref inner) => inner.fmt(f),
Error::HTTPError(ref inner) => inner.fmt(f),
Error::LastFMError(ref inner) => inner.fmt(f),
}
}
}
#[derive(Debug)]
pub enum LastFMErrorResponse {
InvalidService(LastFMError),
InvalidMethod(LastFMError),
AuthenticationFailed(LastFMError),
InvalidFormat(LastFMError),
InvalidParameters(LastFMError),
InvalidResourceSpecified(LastFMError),
OperationFailed(LastFMError),
InvalidSessionKey(LastFMError),
InvalidAPIKey(LastFMError),
ServiceOffline(LastFMError),
InvalidMethodSignatureSupplied(LastFMError),
GenericError(LastFMError),
SuspendedAPIKey(LastFMError),
RateLimitExceeded(LastFMError),
}
impl Display for LastFMErrorResponse {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match *self {
LastFMErrorResponse::InvalidService(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidMethod(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::AuthenticationFailed(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidFormat(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidParameters(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidResourceSpecified(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::OperationFailed(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidSessionKey(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidAPIKey(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::ServiceOffline(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::InvalidMethodSignatureSupplied(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::GenericError(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::SuspendedAPIKey(ref inner) => write!(f, "{}", inner.message),
LastFMErrorResponse::RateLimitExceeded(ref inner) => write!(f, "{}", inner.message),
}
}
}
#[derive(Deserialize, Debug)]
pub struct LastFMError {
pub error: i32,
pub message: String,
pub links: Option<Vec<String>>,
}
impl From<LastFMError> for LastFMErrorResponse {
fn from(lastm_error: LastFMError) -> LastFMErrorResponse {
match lastm_error.error {
2 => LastFMErrorResponse::InvalidService(lastm_error),
3 => LastFMErrorResponse::InvalidMethod(lastm_error),
4 => LastFMErrorResponse::AuthenticationFailed(lastm_error),
5 => LastFMErrorResponse::InvalidFormat(lastm_error),
6 => LastFMErrorResponse::InvalidParameters(lastm_error),
7 => LastFMErrorResponse::InvalidResourceSpecified(lastm_error),
8 => LastFMErrorResponse::OperationFailed(lastm_error),
9 => LastFMErrorResponse::InvalidSessionKey(lastm_error),
10 => LastFMErrorResponse::InvalidAPIKey(lastm_error),
11 => LastFMErrorResponse::ServiceOffline(lastm_error),
13 => LastFMErrorResponse::InvalidMethodSignatureSupplied(lastm_error),
16 => LastFMErrorResponse::GenericError(lastm_error),
26 => LastFMErrorResponse::SuspendedAPIKey(lastm_error),
29 => LastFMErrorResponse::RateLimitExceeded(lastm_error),
_ => LastFMErrorResponse::GenericError(lastm_error),
}
}
}