Skip to main content

obws/responses/
ids.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4macro_rules! item_id {
5    ($ident:ident, $name:literal, $name_field:literal, $uuid_field:literal) => {
6        #[doc = concat!("Identifier of the", $name, ".")]
7        #[derive(
8            Clone, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
9        )]
10        pub struct $ident {
11            #[doc = concat!("Name of the", $name, ".")]
12            #[serde(rename = $name_field)]
13            pub name: String,
14            #[doc = concat!("UUID of the", $name, ".")]
15            #[serde(rename = $uuid_field)]
16            pub uuid: Uuid,
17        }
18
19        impl PartialEq<$ident> for String {
20            fn eq(&self, other: &$ident) -> bool {
21                other == self.as_str()
22            }
23        }
24
25        impl PartialEq<$ident> for &str {
26            fn eq(&self, other: &$ident) -> bool {
27                other == *self
28            }
29        }
30
31        impl PartialEq<$ident> for Uuid {
32            fn eq(&self, other: &$ident) -> bool {
33                other == self
34            }
35        }
36
37        impl PartialEq<str> for $ident {
38            fn eq(&self, other: &str) -> bool {
39                self.name == other
40            }
41        }
42
43        impl PartialEq<Uuid> for $ident {
44            fn eq(&self, other: &Uuid) -> bool {
45                self.uuid == *other
46            }
47        }
48    };
49}
50
51item_id!(InputId, "input", "inputName", "inputUuid");
52item_id!(SceneId, "scene", "sceneName", "sceneUuid");
53item_id!(SourceId, "source", "sourceName", "sourceUuid");
54item_id!(
55    TransitionId,
56    "transition",
57    "transitionName",
58    "transitionUuid"
59);
60item_id!(CanvasId, "canvas", "canvasName", "canvasUuid");
61
62item_id!(
63    CurrentPreviewSceneId,
64    "current preview scene",
65    "currentPreviewSceneName",
66    "currentPreviewSceneUuid"
67);
68item_id!(
69    CurrentProgramSceneId,
70    "current program scene",
71    "currentProgramSceneName",
72    "currentProgramSceneUuid"
73);
74item_id!(
75    CurrentSceneTransitionId,
76    "current scene transition",
77    "currentSceneTransitionName",
78    "currentSceneTransitionUuid"
79);
80
81macro_rules! convert {
82    ($source:ident, $target:ident) => {
83        impl From<$source> for $target {
84            fn from(value: $source) -> Self {
85                Self {
86                    name: value.name,
87                    uuid: value.uuid,
88                }
89            }
90        }
91
92        impl From<$target> for $source {
93            fn from(value: $target) -> Self {
94                Self {
95                    name: value.name,
96                    uuid: value.uuid,
97                }
98            }
99        }
100    };
101}
102
103convert!(SceneId, CurrentPreviewSceneId);
104convert!(SceneId, CurrentProgramSceneId);
105convert!(CurrentPreviewSceneId, CurrentProgramSceneId);
106convert!(TransitionId, CurrentSceneTransitionId);
107
108macro_rules! request {
109    ($ident:ident) => {
110        impl From<$ident> for crate::requests::ids::$ident<'_> {
111            fn from(value: $ident) -> Self {
112                Self::Uuid(value.uuid)
113            }
114        }
115
116        impl From<&$ident> for crate::requests::ids::$ident<'_> {
117            fn from(value: &$ident) -> Self {
118                Self::Uuid(value.uuid)
119            }
120        }
121
122        impl PartialEq<$ident> for crate::requests::ids::$ident<'_> {
123            fn eq(&self, other: &$ident) -> bool {
124                match *self {
125                    Self::Name(name) => name == other.name,
126                    Self::Uuid(uuid) => uuid == other.uuid,
127                }
128            }
129        }
130
131        impl PartialEq<crate::requests::ids::$ident<'_>> for $ident {
132            fn eq(&self, other: &crate::requests::ids::$ident<'_>) -> bool {
133                other == self
134            }
135        }
136    };
137}
138
139request!(InputId);
140request!(SceneId);
141request!(SourceId);
142request!(TransitionId);