es_entity/
macros.rs

1#[macro_export]
2macro_rules! idempotency_guard {
3    ($events:expr, $( $pattern:pat $(if $guard:expr)? ),+ $(,)?) => {
4        for event in $events {
5            match event {
6                $(
7                    $pattern $(if $guard)? => return $crate::FromIdempotentIgnored::from_ignored(),
8                )+
9                _ => {}
10            }
11        }
12    };
13    ($events:expr, $( $pattern:pat $(if $guard:expr)? ),+,
14     => $break_pattern:pat $(if $break_guard:expr)?) => {
15        for event in $events {
16            match event {
17                $($pattern $(if $guard)? => return $crate::FromIdempotentIgnored::from_ignored(),)+
18                $break_pattern $(if $break_guard)? => break,
19                _ => {}
20            }
21        }
22    };
23}
24
25#[macro_export]
26macro_rules! es_query {
27    ($prefix:literal, $db:expr, $query:expr) => ({
28        $crate::expand_es_query!(
29            ignore_prefix = $prefix,
30            executor = $db,
31            sql = $query
32        )
33    });
34    ($prefix:literal, $db:expr, $query:expr, $($args:tt)*) => ({
35        $crate::expand_es_query!(
36            ignore_prefix = $prefix,
37            executor = $db,
38            sql = $query,
39            args = [$($args)*]
40        )
41    });
42    ($db:expr, $query:expr) => ({
43        $crate::expand_es_query!(
44            executor = $db,
45            sql = $query
46        )
47    });
48    ($db:expr, $query:expr, $($args:tt)*) => ({
49        $crate::expand_es_query!(
50            executor = $db,
51            sql = $query,
52            args = [$($args)*]
53        )
54    });
55}
56
57#[macro_export]
58macro_rules! from_es_entity_error {
59    ($name:ident) => {
60        impl $name {
61            pub fn was_not_found(&self) -> bool {
62                matches!(self, $name::EsEntityError($crate::EsEntityError::NotFound))
63            }
64            pub fn was_concurrent_modification(&self) -> bool {
65                matches!(
66                    self,
67                    $name::EsEntityError($crate::EsEntityError::ConcurrentModification)
68                )
69            }
70        }
71        impl From<$crate::EsEntityError> for $name {
72            fn from(e: $crate::EsEntityError) -> Self {
73                $name::EsEntityError(e)
74            }
75        }
76    };
77}
78
79#[cfg(feature = "graphql")]
80#[macro_export]
81macro_rules! entity_id {
82    // Match identifiers without conversions
83    ($($name:ident),+ $(,)?) => {
84        $crate::entity_id! { $($name),+ ; }
85    };
86    ($($name:ident),+ $(,)? ; $($from:ty => $to:ty),* $(,)?) => {
87        $(
88            #[derive(
89                sqlx::Type,
90                Debug,
91                Clone,
92                Copy,
93                PartialEq,
94                Eq,
95                PartialOrd,
96                Ord,
97                Hash,
98                serde::Deserialize,
99                serde::Serialize,
100            )]
101            #[serde(transparent)]
102            #[sqlx(transparent)]
103            pub struct $name($crate::prelude::uuid::Uuid);
104
105            impl $name {
106                #[allow(clippy::new_without_default)]
107                pub fn new() -> Self {
108                    $crate::prelude::uuid::Uuid::new_v4().into()
109                }
110            }
111
112            impl From<$crate::prelude::uuid::Uuid> for $name {
113                fn from(uuid: $crate::prelude::uuid::Uuid) -> Self {
114                    Self(uuid)
115                }
116            }
117
118            impl From<$name> for $crate::prelude::uuid::Uuid {
119                fn from(id: $name) -> Self {
120                    id.0
121                }
122            }
123
124            impl From<&$name> for $crate::prelude::uuid::Uuid {
125                fn from(id: &$name) -> Self {
126                    id.0
127                }
128            }
129
130            impl From<$crate::graphql::UUID> for $name {
131                fn from(id: $crate::graphql::UUID) -> Self {
132                    $name($crate::prelude::uuid::Uuid::from(&id))
133                }
134            }
135
136            impl From<&$crate::graphql::UUID> for $name {
137                fn from(id: &$crate::graphql::UUID) -> Self {
138                    $name($crate::prelude::uuid::Uuid::from(id))
139                }
140            }
141
142            impl std::fmt::Display for $name {
143                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144                    write!(f, "{}", self.0)
145                }
146            }
147
148            impl std::str::FromStr for $name {
149                type Err = $crate::prelude::uuid::Error;
150
151                fn from_str(s: &str) -> Result<Self, Self::Err> {
152                    Ok(Self($crate::prelude::uuid::Uuid::parse_str(s)?))
153                }
154            }
155        )+
156        // Implement additional conversions
157        $(
158            impl From<$from> for $to {
159                fn from(id: $from) -> Self {
160                    <$to>::from($crate::prelude::uuid::Uuid::from(id))
161                }
162            }
163            impl From<$to> for $from {
164                fn from(id: $to) -> Self {
165                    <$from>::from($crate::prelude::uuid::Uuid::from(id))
166                }
167            }
168        )*
169    };
170}
171
172#[cfg(not(feature = "graphql"))]
173#[macro_export]
174macro_rules! entity_id {
175    // Match identifiers without conversions
176    ($($name:ident),+ $(,)?) => {
177        $crate::entity_id! { $($name),+ ; }
178    };
179    ($($name:ident),+ $(,)? ; $($from:ty => $to:ty),* $(,)?) => {
180        $(
181            #[derive(
182                sqlx::Type,
183                Debug,
184                Clone,
185                Copy,
186                PartialEq,
187                Eq,
188                PartialOrd,
189                Ord,
190                Hash,
191                serde::Deserialize,
192                serde::Serialize,
193            )]
194            #[serde(transparent)]
195            #[sqlx(transparent)]
196            pub struct $name($crate::prelude::uuid::Uuid);
197
198            impl $name {
199                #[allow(clippy::new_without_default)]
200                pub fn new() -> Self {
201                    $crate::prelude::uuid::Uuid::new_v4().into()
202                }
203            }
204
205            impl From<$crate::prelude::uuid::Uuid> for $name {
206                fn from(uuid: $crate::prelude::uuid::Uuid) -> Self {
207                    Self(uuid)
208                }
209            }
210
211            impl From<$name> for $crate::prelude::uuid::Uuid {
212                fn from(id: $name) -> Self {
213                    id.0
214                }
215            }
216
217            impl From<&$name> for $crate::prelude::uuid::Uuid {
218                fn from(id: &$name) -> Self {
219                    id.0
220                }
221            }
222
223            impl std::fmt::Display for $name {
224                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
225                    write!(f, "{}", self.0)
226                }
227            }
228
229            impl std::str::FromStr for $name {
230                type Err = $crate::prelude::uuid::Error;
231
232                fn from_str(s: &str) -> Result<Self, Self::Err> {
233                    Ok(Self($crate::prelude::uuid::Uuid::parse_str(s)?))
234                }
235            }
236        )+
237        // Implement additional conversions
238        $(
239            impl From<$from> for $to {
240                fn from(id: $from) -> Self {
241                    <$to>::from($crate::prelude::uuid::Uuid::from(id))
242                }
243            }
244            impl From<$to> for $from {
245                fn from(id: $to) -> Self {
246                    <$from>::from($crate::prelude::uuid::Uuid::from(id))
247                }
248            }
249        )*
250    };
251}