Add impl_from! macro to shorten up error code

This commit is contained in:
Lucien Greathouse
2019-01-15 13:08:02 -08:00
parent 9d3638fa46
commit aae1d8b34f
6 changed files with 41 additions and 56 deletions

18
server/src/impl_from.rs Normal file
View File

@@ -0,0 +1,18 @@
/// Implements 'From' for a list of variants, intended for use with error enums
/// that are wrapping a number of errors from other methods.
#[macro_export]
macro_rules! impl_from {
(
$enum_name: ident {
$($error_type: ty => $variant_name: ident),* $(,)*
}
) => {
$(
impl From<$error_type> for $enum_name {
fn from(error: $error_type) -> $enum_name {
$enum_name::$variant_name(error)
}
}
)*
}
}