obs_wrapper/
native_enum.rs1#[derive(Debug)]
2pub struct NativeParsingError {
3 struct_name: &'static str,
4 value: i64,
5}
6
7impl NativeParsingError {
8 pub(crate) fn new(struct_name: &'static str, value: i64) -> Self {
9 Self { struct_name, value }
10 }
11}
12
13impl std::fmt::Display for NativeParsingError {
14 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 write!(
16 f,
17 "Failed to convert native value {} into {}",
18 self.value, self.struct_name
19 )
20 }
21}
22
23impl std::error::Error for NativeParsingError {}
24
25#[macro_export]
26macro_rules! native_enum {
27 ($name:ident,$native_name:ident { $($rust:ident => $native:ident),* }) => {
28 paste::item! {
29 #[derive(Debug, Clone, Copy, Eq, PartialEq)]
30 pub enum $name {
31 $($rust),*
32 }
33
34 #[allow(clippy::from_over_into)]
35 impl Into<$native_name> for $name {
36 fn into(self) -> obs_text_type {
37 match self {
38 $(Self::$rust => [<$native_name _ $native>]),*
39 }
40 }
41 }
42
43 impl std::convert::TryFrom<$native_name> for $name {
44 type Error = $crate::native_enum::NativeParsingError;
45 fn try_from(value: $native_name) -> Result<Self, Self::Error> {
46 match value {
47 $([<$native_name _ $native>] => Ok(Self::$rust)),*,
48 _ => Err($crate::native_enum::NativeParsingError::new(stringify!($name), value as i64))
49 }
50 }
51 }
52 }
53 };
54}