Polish a few things (#67)

* Improve some match statements using macros

* Use `thiserror` instead of manually impl'ing Error
This commit is contained in:
T0mstone
2021-04-10 16:27:44 +02:00
committed by Keavon Chambers
parent a56f50c60c
commit 21ffb3571e
7 changed files with 145 additions and 75 deletions
+37
View File
@@ -47,3 +47,40 @@ macro_rules! gen_tools_hash_map {
hash_map
}};
}
/// Creates a string representation of an enum value that exactly matches the given name of each enum variant
///
/// # Example
///
/// ```ignore
/// enum E {
/// A(u8),
/// B
/// }
///
/// // this line is important
/// use E::*;
///
/// let a = E::A(7);
/// let s = match_variant_name!(match (a) { A, B });
/// ```
///
/// expands to
///
/// ```ignore
/// // ...
///
/// let s = match a {
/// A { .. } => "A",
/// B { .. } => "B"
/// };
/// ```
macro_rules! match_variant_name {
(match ($e:expr) { $($v:ident),* $(,)? }) => {
match $e {
$(
$v { .. } => stringify!(v)
),*
}
};
}