1use reifydb_value::Result;
5
6use super::{
7 dictionary::{
8 DictionaryPostCreateContext, DictionaryPostCreateInterceptor, DictionaryPostUpdateContext,
9 DictionaryPostUpdateInterceptor, DictionaryPreDeleteContext, DictionaryPreDeleteInterceptor,
10 DictionaryPreUpdateContext, DictionaryPreUpdateInterceptor,
11 },
12 dictionary_row::{
13 DictionaryRowPostDeleteContext, DictionaryRowPostDeleteInterceptor, DictionaryRowPostInsertContext,
14 DictionaryRowPostInsertInterceptor, DictionaryRowPostUpdateContext, DictionaryRowPostUpdateInterceptor,
15 DictionaryRowPreDeleteContext, DictionaryRowPreDeleteInterceptor, DictionaryRowPreInsertContext,
16 DictionaryRowPreInsertInterceptor, DictionaryRowPreUpdateContext, DictionaryRowPreUpdateInterceptor,
17 },
18 filter::InterceptFilter,
19 namespace::{
20 NamespacePostCreateContext, NamespacePostCreateInterceptor, NamespacePostUpdateContext,
21 NamespacePostUpdateInterceptor, NamespacePreDeleteContext, NamespacePreDeleteInterceptor,
22 NamespacePreUpdateContext, NamespacePreUpdateInterceptor,
23 },
24 ringbuffer::{
25 RingBufferPostCreateContext, RingBufferPostCreateInterceptor, RingBufferPostUpdateContext,
26 RingBufferPostUpdateInterceptor, RingBufferPreDeleteContext, RingBufferPreDeleteInterceptor,
27 RingBufferPreUpdateContext, RingBufferPreUpdateInterceptor,
28 },
29 ringbuffer_row::{
30 RingBufferRowPostDeleteContext, RingBufferRowPostDeleteInterceptor, RingBufferRowPostInsertContext,
31 RingBufferRowPostInsertInterceptor, RingBufferRowPostUpdateContext, RingBufferRowPostUpdateInterceptor,
32 RingBufferRowPreDeleteContext, RingBufferRowPreDeleteInterceptor, RingBufferRowPreInsertContext,
33 RingBufferRowPreInsertInterceptor, RingBufferRowPreUpdateContext, RingBufferRowPreUpdateInterceptor,
34 },
35 series::{
36 SeriesPostCreateContext, SeriesPostCreateInterceptor, SeriesPostUpdateContext,
37 SeriesPostUpdateInterceptor, SeriesPreDeleteContext, SeriesPreDeleteInterceptor,
38 SeriesPreUpdateContext, SeriesPreUpdateInterceptor,
39 },
40 series_row::{
41 SeriesRowPostDeleteContext, SeriesRowPostDeleteInterceptor, SeriesRowPostInsertContext,
42 SeriesRowPostInsertInterceptor, SeriesRowPostUpdateContext, SeriesRowPostUpdateInterceptor,
43 SeriesRowPreDeleteContext, SeriesRowPreDeleteInterceptor, SeriesRowPreInsertContext,
44 SeriesRowPreInsertInterceptor, SeriesRowPreUpdateContext, SeriesRowPreUpdateInterceptor,
45 },
46 table::{
47 TablePostCreateContext, TablePostCreateInterceptor, TablePostUpdateContext, TablePostUpdateInterceptor,
48 TablePreDeleteContext, TablePreDeleteInterceptor, TablePreUpdateContext, TablePreUpdateInterceptor,
49 },
50 table_row::{
51 TableRowPostDeleteContext, TableRowPostDeleteInterceptor, TableRowPostInsertContext,
52 TableRowPostInsertInterceptor, TableRowPostUpdateContext, TableRowPostUpdateInterceptor,
53 TableRowPreDeleteContext, TableRowPreDeleteInterceptor, TableRowPreInsertContext,
54 TableRowPreInsertInterceptor, TableRowPreUpdateContext, TableRowPreUpdateInterceptor,
55 },
56 view::{
57 ViewPostCreateContext, ViewPostCreateInterceptor, ViewPostUpdateContext, ViewPostUpdateInterceptor,
58 ViewPreDeleteContext, ViewPreDeleteInterceptor, ViewPreUpdateContext, ViewPreUpdateInterceptor,
59 },
60};
61
62macro_rules! define_filtered_interceptor {
63 (
64 $wrapper_name:ident,
65 $trait_name:ident,
66 $context_type:ident,
67 $entity_field:ident
68 ) => {
69 pub struct $wrapper_name<F>
70 where
71 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
72 {
73 filter: InterceptFilter,
74 handler: F,
75 }
76
77 impl<F> $wrapper_name<F>
78 where
79 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
80 {
81 pub fn new(filter: InterceptFilter, handler: F) -> Self {
82 Self {
83 filter,
84 handler,
85 }
86 }
87 }
88
89 impl<F> Clone for $wrapper_name<F>
90 where
91 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync + Clone,
92 {
93 fn clone(&self) -> Self {
94 Self {
95 filter: self.filter.clone(),
96 handler: self.handler.clone(),
97 }
98 }
99 }
100
101 impl<F> $trait_name for $wrapper_name<F>
102 where
103 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
104 {
105 fn intercept<'a>(&self, ctx: &mut $context_type<'a>) -> Result<()> {
106 let entity_name = ctx.$entity_field.name();
107 let name_matches =
108 self.filter.name.as_ref().map_or(true, |n| n.as_str() == entity_name);
109 if name_matches {
110 (self.handler)(ctx)
111 } else {
112 Ok(())
113 }
114 }
115 }
116 };
117 (
118 $wrapper_name:ident,
119 $trait_name:ident,
120 $context_type:ident,
121 $entity_field:ident,
122 $name_method:ident
123 ) => {
124 pub struct $wrapper_name<F>
125 where
126 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
127 {
128 filter: InterceptFilter,
129 handler: F,
130 }
131
132 impl<F> $wrapper_name<F>
133 where
134 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
135 {
136 pub fn new(filter: InterceptFilter, handler: F) -> Self {
137 Self {
138 filter,
139 handler,
140 }
141 }
142 }
143
144 impl<F> Clone for $wrapper_name<F>
145 where
146 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync + Clone,
147 {
148 fn clone(&self) -> Self {
149 Self {
150 filter: self.filter.clone(),
151 handler: self.handler.clone(),
152 }
153 }
154 }
155
156 impl<F> $trait_name for $wrapper_name<F>
157 where
158 F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
159 {
160 fn intercept<'a>(&self, ctx: &mut $context_type<'a>) -> Result<()> {
161 let entity_name = ctx.$entity_field.$name_method();
162 let name_matches =
163 self.filter.name.as_ref().map_or(true, |n| n.as_str() == entity_name);
164 if name_matches {
165 (self.handler)(ctx)
166 } else {
167 Ok(())
168 }
169 }
170 }
171 };
172}
173
174define_filtered_interceptor!(
175 FilteredTableRowPreInsertInterceptor,
176 TableRowPreInsertInterceptor,
177 TableRowPreInsertContext,
178 table
179);
180
181define_filtered_interceptor!(
182 FilteredTableRowPostInsertInterceptor,
183 TableRowPostInsertInterceptor,
184 TableRowPostInsertContext,
185 table
186);
187
188define_filtered_interceptor!(
189 FilteredTableRowPreUpdateInterceptor,
190 TableRowPreUpdateInterceptor,
191 TableRowPreUpdateContext,
192 table
193);
194
195define_filtered_interceptor!(
196 FilteredTableRowPostUpdateInterceptor,
197 TableRowPostUpdateInterceptor,
198 TableRowPostUpdateContext,
199 table
200);
201
202define_filtered_interceptor!(
203 FilteredTableRowPreDeleteInterceptor,
204 TableRowPreDeleteInterceptor,
205 TableRowPreDeleteContext,
206 table
207);
208
209define_filtered_interceptor!(
210 FilteredTableRowPostDeleteInterceptor,
211 TableRowPostDeleteInterceptor,
212 TableRowPostDeleteContext,
213 table
214);
215
216define_filtered_interceptor!(
217 FilteredRingBufferRowPreInsertInterceptor,
218 RingBufferRowPreInsertInterceptor,
219 RingBufferRowPreInsertContext,
220 ringbuffer
221);
222
223define_filtered_interceptor!(
224 FilteredRingBufferRowPostInsertInterceptor,
225 RingBufferRowPostInsertInterceptor,
226 RingBufferRowPostInsertContext,
227 ringbuffer
228);
229
230define_filtered_interceptor!(
231 FilteredRingBufferRowPreUpdateInterceptor,
232 RingBufferRowPreUpdateInterceptor,
233 RingBufferRowPreUpdateContext,
234 ringbuffer
235);
236
237define_filtered_interceptor!(
238 FilteredRingBufferRowPostUpdateInterceptor,
239 RingBufferRowPostUpdateInterceptor,
240 RingBufferRowPostUpdateContext,
241 ringbuffer
242);
243
244define_filtered_interceptor!(
245 FilteredRingBufferRowPreDeleteInterceptor,
246 RingBufferRowPreDeleteInterceptor,
247 RingBufferRowPreDeleteContext,
248 ringbuffer
249);
250
251define_filtered_interceptor!(
252 FilteredRingBufferRowPostDeleteInterceptor,
253 RingBufferRowPostDeleteInterceptor,
254 RingBufferRowPostDeleteContext,
255 ringbuffer
256);
257
258define_filtered_interceptor!(FilteredViewPostCreateInterceptor, ViewPostCreateInterceptor, ViewPostCreateContext, post);
259
260define_filtered_interceptor!(FilteredViewPreUpdateInterceptor, ViewPreUpdateInterceptor, ViewPreUpdateContext, pre);
261
262define_filtered_interceptor!(FilteredViewPostUpdateInterceptor, ViewPostUpdateInterceptor, ViewPostUpdateContext, pre);
263
264define_filtered_interceptor!(FilteredViewPreDeleteInterceptor, ViewPreDeleteInterceptor, ViewPreDeleteContext, pre);
265
266define_filtered_interceptor!(
267 FilteredTablePostCreateInterceptor,
268 TablePostCreateInterceptor,
269 TablePostCreateContext,
270 post
271);
272
273define_filtered_interceptor!(FilteredTablePreUpdateInterceptor, TablePreUpdateInterceptor, TablePreUpdateContext, pre);
274
275define_filtered_interceptor!(
276 FilteredTablePostUpdateInterceptor,
277 TablePostUpdateInterceptor,
278 TablePostUpdateContext,
279 pre
280);
281
282define_filtered_interceptor!(FilteredTablePreDeleteInterceptor, TablePreDeleteInterceptor, TablePreDeleteContext, pre);
283
284define_filtered_interceptor!(
285 FilteredRingBufferPostCreateInterceptor,
286 RingBufferPostCreateInterceptor,
287 RingBufferPostCreateContext,
288 post
289);
290
291define_filtered_interceptor!(
292 FilteredRingBufferPreUpdateInterceptor,
293 RingBufferPreUpdateInterceptor,
294 RingBufferPreUpdateContext,
295 pre
296);
297
298define_filtered_interceptor!(
299 FilteredRingBufferPostUpdateInterceptor,
300 RingBufferPostUpdateInterceptor,
301 RingBufferPostUpdateContext,
302 pre
303);
304
305define_filtered_interceptor!(
306 FilteredRingBufferPreDeleteInterceptor,
307 RingBufferPreDeleteInterceptor,
308 RingBufferPreDeleteContext,
309 pre
310);
311
312define_filtered_interceptor!(
313 FilteredSeriesRowPreInsertInterceptor,
314 SeriesRowPreInsertInterceptor,
315 SeriesRowPreInsertContext,
316 series
317);
318
319define_filtered_interceptor!(
320 FilteredSeriesRowPostInsertInterceptor,
321 SeriesRowPostInsertInterceptor,
322 SeriesRowPostInsertContext,
323 series
324);
325
326define_filtered_interceptor!(
327 FilteredSeriesRowPreUpdateInterceptor,
328 SeriesRowPreUpdateInterceptor,
329 SeriesRowPreUpdateContext,
330 series
331);
332
333define_filtered_interceptor!(
334 FilteredSeriesRowPostUpdateInterceptor,
335 SeriesRowPostUpdateInterceptor,
336 SeriesRowPostUpdateContext,
337 series
338);
339
340define_filtered_interceptor!(
341 FilteredSeriesRowPreDeleteInterceptor,
342 SeriesRowPreDeleteInterceptor,
343 SeriesRowPreDeleteContext,
344 series
345);
346
347define_filtered_interceptor!(
348 FilteredSeriesRowPostDeleteInterceptor,
349 SeriesRowPostDeleteInterceptor,
350 SeriesRowPostDeleteContext,
351 series
352);
353
354define_filtered_interceptor!(
355 FilteredSeriesPostCreateInterceptor,
356 SeriesPostCreateInterceptor,
357 SeriesPostCreateContext,
358 post
359);
360
361define_filtered_interceptor!(
362 FilteredSeriesPreUpdateInterceptor,
363 SeriesPreUpdateInterceptor,
364 SeriesPreUpdateContext,
365 pre
366);
367
368define_filtered_interceptor!(
369 FilteredSeriesPostUpdateInterceptor,
370 SeriesPostUpdateInterceptor,
371 SeriesPostUpdateContext,
372 pre
373);
374
375define_filtered_interceptor!(
376 FilteredSeriesPreDeleteInterceptor,
377 SeriesPreDeleteInterceptor,
378 SeriesPreDeleteContext,
379 pre
380);
381
382define_filtered_interceptor!(
383 FilteredDictionaryRowPreInsertInterceptor,
384 DictionaryRowPreInsertInterceptor,
385 DictionaryRowPreInsertContext,
386 dictionary
387);
388
389define_filtered_interceptor!(
390 FilteredDictionaryRowPostInsertInterceptor,
391 DictionaryRowPostInsertInterceptor,
392 DictionaryRowPostInsertContext,
393 dictionary
394);
395
396define_filtered_interceptor!(
397 FilteredDictionaryRowPreUpdateInterceptor,
398 DictionaryRowPreUpdateInterceptor,
399 DictionaryRowPreUpdateContext,
400 dictionary
401);
402
403define_filtered_interceptor!(
404 FilteredDictionaryRowPostUpdateInterceptor,
405 DictionaryRowPostUpdateInterceptor,
406 DictionaryRowPostUpdateContext,
407 dictionary
408);
409
410define_filtered_interceptor!(
411 FilteredDictionaryRowPreDeleteInterceptor,
412 DictionaryRowPreDeleteInterceptor,
413 DictionaryRowPreDeleteContext,
414 dictionary
415);
416
417define_filtered_interceptor!(
418 FilteredDictionaryRowPostDeleteInterceptor,
419 DictionaryRowPostDeleteInterceptor,
420 DictionaryRowPostDeleteContext,
421 dictionary
422);
423
424define_filtered_interceptor!(
425 FilteredDictionaryPostCreateInterceptor,
426 DictionaryPostCreateInterceptor,
427 DictionaryPostCreateContext,
428 post
429);
430
431define_filtered_interceptor!(
432 FilteredDictionaryPreUpdateInterceptor,
433 DictionaryPreUpdateInterceptor,
434 DictionaryPreUpdateContext,
435 pre
436);
437
438define_filtered_interceptor!(
439 FilteredDictionaryPostUpdateInterceptor,
440 DictionaryPostUpdateInterceptor,
441 DictionaryPostUpdateContext,
442 pre
443);
444
445define_filtered_interceptor!(
446 FilteredDictionaryPreDeleteInterceptor,
447 DictionaryPreDeleteInterceptor,
448 DictionaryPreDeleteContext,
449 pre
450);
451
452define_filtered_interceptor!(
453 FilteredNamespacePostCreateInterceptor,
454 NamespacePostCreateInterceptor,
455 NamespacePostCreateContext,
456 post,
457 name
458);
459
460define_filtered_interceptor!(
461 FilteredNamespacePreUpdateInterceptor,
462 NamespacePreUpdateInterceptor,
463 NamespacePreUpdateContext,
464 pre,
465 name
466);
467
468define_filtered_interceptor!(
469 FilteredNamespacePostUpdateInterceptor,
470 NamespacePostUpdateInterceptor,
471 NamespacePostUpdateContext,
472 pre,
473 name
474);
475
476define_filtered_interceptor!(
477 FilteredNamespacePreDeleteInterceptor,
478 NamespacePreDeleteInterceptor,
479 NamespacePreDeleteContext,
480 pre,
481 name
482);