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