events: reimplement From and To impls with macros

This commit is contained in:
Victoria Fischer 2024-12-14 15:32:25 +01:00
parent 01fdc11552
commit cdc82cdbf9

View File

@ -16,65 +16,34 @@ pub enum Variant {
RGB(Rgb<u8>)
}
impl From<u8> for Variant {
fn from(value: u8) -> Self {
Variant::Byte(value)
}
}
impl Into<u8> for Variant {
fn into(self) -> u8 {
match self {
Variant::Byte(b) => b,
_ => 0
macro_rules! impl_variant_type {
($type:ty, $var_type:tt) => {
impl From<$type> for Variant {
fn from(value: $type) -> Self {
Variant::$var_type(value)
}
}
}
}
impl From<u64> for Variant {
fn from(value: u64) -> Self {
Variant::BigUInt(value)
}
}
impl Into<u64> for Variant {
fn into(self) -> u64 {
match self {
Variant::BigUInt(b) => b,
_ => 0
impl Into<$type> for Variant {
fn into(self) -> $type {
match self {
Variant::$var_type(value) => value,
_ => panic!("Expected Variant::$var_type, but got {:?}", self)
}
}
}
}
};
}
impl From<bool> for Variant {
fn from(value: bool) -> Self {
Variant::Boolean(value)
}
}
impl From<i64> for Variant {
fn from(value: i64) -> Self {
Variant::BigInt(value)
}
}
impl From<u32> for Variant {
fn from(value: u32) -> Self {
Variant::UInt(value)
}
}
impl From<i32> for Variant {
fn from(value: i32) -> Self {
Variant::Int(value)
}
}
impl From<String> for Variant {
fn from(value: String) -> Self {
Variant::String(value)
}
}
impl_variant_type!(u8, Byte);
impl_variant_type!(i8, SignedByte);
impl_variant_type!(u32, UInt);
impl_variant_type!(i32, Int);
impl_variant_type!(i64, BigInt);
impl_variant_type!(u64, BigUInt);
impl_variant_type!(bool, Boolean);
impl_variant_type!(String, String);
impl_variant_type!(Rgb<u8>, RGB);
impl<'a> From<&'a str> for Variant {
fn from(value: &'a str) -> Self {
@ -82,18 +51,6 @@ impl<'a> From<&'a str> for Variant {
}
}
impl From<Rgb<u8>> for Variant {
fn from(value: Rgb<u8>) -> Self {
Variant::RGB(value)
}
}
impl From<i8> for Variant {
fn from(value: i8) -> Self {
Variant::SignedByte(value)
}
}
impl Display for Variant {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {