From 5d71ac584c46de1c51baad7033eb229e377a81fb Mon Sep 17 00:00:00 2001 From: Ty Coghlan Date: Sat, 3 Nov 2018 01:08:57 -0400 Subject: [PATCH] gnome-podcasts: DRY out From impls in errors From impls for errors generally just take some error type and map it into a variant of some podcast error enum. This removes the duplicate impls by using a pattern macro to make the impls from the type of the enum, the given error type, and the desired enum variant. --- podcasts-data/src/errors.rs | 98 +++++++++---------------------- podcasts-downloader/src/errors.rs | 23 ++------ podcasts-downloader/src/lib.rs | 1 + 3 files changed, 36 insertions(+), 86 deletions(-) diff --git a/podcasts-data/src/errors.rs b/podcasts-data/src/errors.rs index c24e05b..dd86f1e 100644 --- a/podcasts-data/src/errors.rs +++ b/podcasts-data/src/errors.rs @@ -71,74 +71,34 @@ pub enum DataError { EpisodeNotChanged, } -impl From for DataError { - fn from(err: RunMigrationsError) -> Self { - DataError::DieselMigrationError(err) - } +// Maps a type to a variant of the DataError enum +#[macro_export] +macro_rules! easy_from_impl { + ($outer_type:ty, $from:ty => $to:expr) => ( + impl From<$from> for $outer_type { + fn from(err: $from) -> Self { + $to(err) + } + } + ); + ($outer_type:ty, $from:ty => $to:expr, $($f:ty => $t:expr),+) => ( + easy_from_impl!($outer_type, $from => $to); + easy_from_impl!($outer_type, $($f => $t),+); + ); } -impl From for DataError { - fn from(err: diesel::result::Error) -> Self { - DataError::DieselResultError(err) - } -} - -impl From for DataError { - fn from(err: r2d2::Error) -> Self { - DataError::R2D2Error(err) - } -} - -impl From for DataError { - fn from(err: r2d2::PoolError) -> Self { - DataError::R2D2PoolError(err) - } -} - -impl From for DataError { - fn from(err: hyper::Error) -> Self { - DataError::HyperError(err) - } -} - -impl From for DataError { - fn from(err: http::header::ToStrError) -> Self { - DataError::HttpToStr(err) - } -} - -impl From for DataError { - fn from(err: url::ParseError) -> Self { - DataError::UrlError(err) - } -} - -impl From for DataError { - fn from(err: native_tls::Error) -> Self { - DataError::TLSError(err) - } -} - -impl From for DataError { - fn from(err: io::Error) -> Self { - DataError::IOError(err) - } -} - -impl From for DataError { - fn from(err: rss::Error) -> Self { - DataError::RssError(err) - } -} - -impl From for DataError { - fn from(err: xml::reader::Error) -> Self { - DataError::XmlReaderError(err) - } -} - -impl From for DataError { - fn from(err: String) -> Self { - DataError::Bail(err) - } -} +easy_from_impl!( + DataError, + RunMigrationsError => DataError::DieselMigrationError, + diesel::result::Error => DataError::DieselResultError, + r2d2::Error => DataError::R2D2Error, + r2d2::PoolError => DataError::R2D2PoolError, + hyper::Error => DataError::HyperError, + http::header::ToStrError => DataError::HttpToStr, + url::ParseError => DataError::UrlError, + native_tls::Error => DataError::TLSError, + io::Error => DataError::IOError, + rss::Error => DataError::RssError, + xml::reader::Error => DataError::XmlReaderError, + String => DataError::Bail +); diff --git a/podcasts-downloader/src/errors.rs b/podcasts-downloader/src/errors.rs index 5750ac5..51def53 100644 --- a/podcasts-downloader/src/errors.rs +++ b/podcasts-downloader/src/errors.rs @@ -24,20 +24,9 @@ pub enum DownloadError { InvalidCachedImageLocation, } -impl From for DownloadError { - fn from(err: reqwest::Error) -> Self { - DownloadError::RequestError(err) - } -} - -impl From for DownloadError { - fn from(err: io::Error) -> Self { - DownloadError::IoError(err) - } -} - -impl From for DownloadError { - fn from(err: DataError) -> Self { - DownloadError::DataError(err) - } -} +easy_from_impl!( + DownloadError, + reqwest::Error => DownloadError::RequestError, + io::Error => DownloadError::IoError, + DataError => DownloadError::DataError +); diff --git a/podcasts-downloader/src/lib.rs b/podcasts-downloader/src/lib.rs index d3e95fa..18cde54 100644 --- a/podcasts-downloader/src/lib.rs +++ b/podcasts-downloader/src/lib.rs @@ -36,6 +36,7 @@ extern crate pretty_assertions; extern crate glob; extern crate mime_guess; +#[macro_use] extern crate podcasts_data; extern crate reqwest; extern crate tempdir;