From 3c92e88164db6a94177fb4adeb18c80dffc377e4 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 12 Dec 2019 02:16:27 +0100 Subject: remove quick_error quick_error used the deprecated Error::cause interface, therefore we want to use our own error enums with proper methods. --- src/api/mod.rs | 55 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 19 deletions(-) (limited to 'src/api/mod.rs') diff --git a/src/api/mod.rs b/src/api/mod.rs index c33e6ba..27f0da1 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -18,32 +18,49 @@ use image::DynamicImage; use itertools::Itertools; use reqwest::{Client, StatusCode, Url}; use serde::{de::DeserializeOwned, Serialize}; -use std::path::Path; +use std::{error::Error, fmt, path::Path}; use super::cache::{Cache, CacheError}; /// The base URL of the official Guild Wars 2 API. const BASE_URL: &str = "https://api.guildwars2.com/v2/"; -quick_error! { - #[derive(Debug)] - pub enum ApiError { - ItemNotFound {} - SerializationError(err: serde_json::Error) { - cause(err) - from() - } - CacheError(err: CacheError) { - cause(err) - from() - } - HttpError(err: reqwest::Error) { - cause(err) - from() +#[derive(Debug)] +pub enum ApiError { + ItemNotFound, + SerializationError(serde_json::Error), + CacheError(CacheError), + HttpError(reqwest::Error), + ImageError(image::ImageError), +} + +error_froms! { ApiError, + err: serde_json::Error => ApiError::SerializationError(err), + err: CacheError => ApiError::CacheError(err), + err: reqwest::Error => ApiError::HttpError(err), + err: image::ImageError => ApiError::ImageError(err), +} + +impl fmt::Display for ApiError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + ApiError::ItemNotFound => write!(f, "the requested item was not found in the API"), + ApiError::SerializationError(_) => write!(f, "error deserializing the returned value"), + ApiError::CacheError(_) => write!(f, "error accessing the cache"), + ApiError::HttpError(_) => write!(f, "HTTP error"), + ApiError::ImageError(_) => write!(f, "image processing error"), } - ImageError(err: image::ImageError) { - cause(err) - from() + } +} + +impl Error for ApiError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match *self { + ApiError::SerializationError(ref err) => Some(err), + ApiError::CacheError(ref err) => Some(err), + ApiError::HttpError(ref err) => Some(err), + ApiError::ImageError(ref err) => Some(err), + _ => None, } } } -- cgit v1.2.3