Skip to main content

reifydb_transaction/interceptor/
filtered.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_type::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	identity::{
20		IdentityPostCreateContext, IdentityPostCreateInterceptor, IdentityPostUpdateContext,
21		IdentityPostUpdateInterceptor, IdentityPreDeleteContext, IdentityPreDeleteInterceptor,
22		IdentityPreUpdateContext, IdentityPreUpdateInterceptor,
23	},
24	namespace::{
25		NamespacePostCreateContext, NamespacePostCreateInterceptor, NamespacePostUpdateContext,
26		NamespacePostUpdateInterceptor, NamespacePreDeleteContext, NamespacePreDeleteInterceptor,
27		NamespacePreUpdateContext, NamespacePreUpdateInterceptor,
28	},
29	ringbuffer::{
30		RingBufferPostCreateContext, RingBufferPostCreateInterceptor, RingBufferPostUpdateContext,
31		RingBufferPostUpdateInterceptor, RingBufferPreDeleteContext, RingBufferPreDeleteInterceptor,
32		RingBufferPreUpdateContext, RingBufferPreUpdateInterceptor,
33	},
34	ringbuffer_row::{
35		RingBufferRowPostDeleteContext, RingBufferRowPostDeleteInterceptor, RingBufferRowPostInsertContext,
36		RingBufferRowPostInsertInterceptor, RingBufferRowPostUpdateContext, RingBufferRowPostUpdateInterceptor,
37		RingBufferRowPreDeleteContext, RingBufferRowPreDeleteInterceptor, RingBufferRowPreInsertContext,
38		RingBufferRowPreInsertInterceptor, RingBufferRowPreUpdateContext, RingBufferRowPreUpdateInterceptor,
39	},
40	role::{
41		RolePostCreateContext, RolePostCreateInterceptor, RolePostUpdateContext, RolePostUpdateInterceptor,
42		RolePreDeleteContext, RolePreDeleteInterceptor, RolePreUpdateContext, RolePreUpdateInterceptor,
43	},
44	series::{
45		SeriesPostCreateContext, SeriesPostCreateInterceptor, SeriesPostUpdateContext,
46		SeriesPostUpdateInterceptor, SeriesPreDeleteContext, SeriesPreDeleteInterceptor,
47		SeriesPreUpdateContext, SeriesPreUpdateInterceptor,
48	},
49	series_row::{
50		SeriesRowPostDeleteContext, SeriesRowPostDeleteInterceptor, SeriesRowPostInsertContext,
51		SeriesRowPostInsertInterceptor, SeriesRowPostUpdateContext, SeriesRowPostUpdateInterceptor,
52		SeriesRowPreDeleteContext, SeriesRowPreDeleteInterceptor, SeriesRowPreInsertContext,
53		SeriesRowPreInsertInterceptor, SeriesRowPreUpdateContext, SeriesRowPreUpdateInterceptor,
54	},
55	table::{
56		TablePostCreateContext, TablePostCreateInterceptor, TablePostUpdateContext, TablePostUpdateInterceptor,
57		TablePreDeleteContext, TablePreDeleteInterceptor, TablePreUpdateContext, TablePreUpdateInterceptor,
58	},
59	table_row::{
60		TableRowPostDeleteContext, TableRowPostDeleteInterceptor, TableRowPostInsertContext,
61		TableRowPostInsertInterceptor, TableRowPostUpdateContext, TableRowPostUpdateInterceptor,
62		TableRowPreDeleteContext, TableRowPreDeleteInterceptor, TableRowPreInsertContext,
63		TableRowPreInsertInterceptor, TableRowPreUpdateContext, TableRowPreUpdateInterceptor,
64	},
65	view::{
66		ViewPostCreateContext, ViewPostCreateInterceptor, ViewPostUpdateContext, ViewPostUpdateInterceptor,
67		ViewPreDeleteContext, ViewPreDeleteInterceptor, ViewPreUpdateContext, ViewPreUpdateInterceptor,
68	},
69	view_row::{
70		ViewRowPostDeleteContext, ViewRowPostDeleteInterceptor, ViewRowPostInsertContext,
71		ViewRowPostInsertInterceptor, ViewRowPostUpdateContext, ViewRowPostUpdateInterceptor,
72		ViewRowPreDeleteContext, ViewRowPreDeleteInterceptor, ViewRowPreInsertContext,
73		ViewRowPreInsertInterceptor, ViewRowPreUpdateContext, ViewRowPreUpdateInterceptor,
74	},
75};
76
77/// Macro to generate filtered interceptor wrapper types.
78///
79/// The 4-arg form accesses the entity name via `ctx.$entity_field.name` (for struct types).
80/// The 5-arg form accesses it via `ctx.$entity_field.$name_method()` (for enum types like Namespace).
81macro_rules! define_filtered_interceptor {
82	(
83		$wrapper_name:ident,
84		$trait_name:ident,
85		$context_type:ident,
86		$entity_field:ident
87	) => {
88		/// Filtered interceptor wrapper that checks entity name before executing.
89		pub struct $wrapper_name<F>
90		where
91			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
92		{
93			filter: InterceptFilter,
94			handler: F,
95		}
96
97		impl<F> $wrapper_name<F>
98		where
99			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
100		{
101			pub fn new(filter: InterceptFilter, handler: F) -> Self {
102				Self {
103					filter,
104					handler,
105				}
106			}
107		}
108
109		impl<F> Clone for $wrapper_name<F>
110		where
111			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync + Clone,
112		{
113			fn clone(&self) -> Self {
114				Self {
115					filter: self.filter.clone(),
116					handler: self.handler.clone(),
117				}
118			}
119		}
120
121		impl<F> $trait_name for $wrapper_name<F>
122		where
123			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
124		{
125			fn intercept<'a>(&self, ctx: &mut $context_type<'a>) -> Result<()> {
126				let entity_name = ctx.$entity_field.name();
127				let name_matches =
128					self.filter.name.as_ref().map_or(true, |n| n.as_str() == entity_name);
129				if name_matches {
130					(self.handler)(ctx)
131				} else {
132					Ok(())
133				}
134			}
135		}
136	};
137	(
138		$wrapper_name:ident,
139		$trait_name:ident,
140		$context_type:ident,
141		$entity_field:ident,
142		$name_method:ident
143	) => {
144		/// Filtered interceptor wrapper that checks entity name before executing.
145		pub struct $wrapper_name<F>
146		where
147			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
148		{
149			filter: InterceptFilter,
150			handler: F,
151		}
152
153		impl<F> $wrapper_name<F>
154		where
155			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
156		{
157			pub fn new(filter: InterceptFilter, handler: F) -> Self {
158				Self {
159					filter,
160					handler,
161				}
162			}
163		}
164
165		impl<F> Clone for $wrapper_name<F>
166		where
167			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync + Clone,
168		{
169			fn clone(&self) -> Self {
170				Self {
171					filter: self.filter.clone(),
172					handler: self.handler.clone(),
173				}
174			}
175		}
176
177		impl<F> $trait_name for $wrapper_name<F>
178		where
179			F: for<'a> Fn(&mut $context_type<'a>) -> Result<()> + Send + Sync,
180		{
181			fn intercept<'a>(&self, ctx: &mut $context_type<'a>) -> Result<()> {
182				let entity_name = ctx.$entity_field.$name_method();
183				let name_matches =
184					self.filter.name.as_ref().map_or(true, |n| n.as_str() == entity_name);
185				if name_matches {
186					(self.handler)(ctx)
187				} else {
188					Ok(())
189				}
190			}
191		}
192	};
193}
194
195define_filtered_interceptor!(
196	FilteredTableRowPreInsertInterceptor,
197	TableRowPreInsertInterceptor,
198	TableRowPreInsertContext,
199	table
200);
201
202define_filtered_interceptor!(
203	FilteredTableRowPostInsertInterceptor,
204	TableRowPostInsertInterceptor,
205	TableRowPostInsertContext,
206	table
207);
208
209define_filtered_interceptor!(
210	FilteredTableRowPreUpdateInterceptor,
211	TableRowPreUpdateInterceptor,
212	TableRowPreUpdateContext,
213	table
214);
215
216define_filtered_interceptor!(
217	FilteredTableRowPostUpdateInterceptor,
218	TableRowPostUpdateInterceptor,
219	TableRowPostUpdateContext,
220	table
221);
222
223define_filtered_interceptor!(
224	FilteredTableRowPreDeleteInterceptor,
225	TableRowPreDeleteInterceptor,
226	TableRowPreDeleteContext,
227	table
228);
229
230define_filtered_interceptor!(
231	FilteredTableRowPostDeleteInterceptor,
232	TableRowPostDeleteInterceptor,
233	TableRowPostDeleteContext,
234	table
235);
236
237define_filtered_interceptor!(
238	FilteredRingBufferRowPreInsertInterceptor,
239	RingBufferRowPreInsertInterceptor,
240	RingBufferRowPreInsertContext,
241	ringbuffer
242);
243
244define_filtered_interceptor!(
245	FilteredRingBufferRowPostInsertInterceptor,
246	RingBufferRowPostInsertInterceptor,
247	RingBufferRowPostInsertContext,
248	ringbuffer
249);
250
251define_filtered_interceptor!(
252	FilteredRingBufferRowPreUpdateInterceptor,
253	RingBufferRowPreUpdateInterceptor,
254	RingBufferRowPreUpdateContext,
255	ringbuffer
256);
257
258define_filtered_interceptor!(
259	FilteredRingBufferRowPostUpdateInterceptor,
260	RingBufferRowPostUpdateInterceptor,
261	RingBufferRowPostUpdateContext,
262	ringbuffer
263);
264
265define_filtered_interceptor!(
266	FilteredRingBufferRowPreDeleteInterceptor,
267	RingBufferRowPreDeleteInterceptor,
268	RingBufferRowPreDeleteContext,
269	ringbuffer
270);
271
272define_filtered_interceptor!(
273	FilteredRingBufferRowPostDeleteInterceptor,
274	RingBufferRowPostDeleteInterceptor,
275	RingBufferRowPostDeleteContext,
276	ringbuffer
277);
278
279define_filtered_interceptor!(
280	FilteredViewRowPreInsertInterceptor,
281	ViewRowPreInsertInterceptor,
282	ViewRowPreInsertContext,
283	view
284);
285
286define_filtered_interceptor!(
287	FilteredViewRowPostInsertInterceptor,
288	ViewRowPostInsertInterceptor,
289	ViewRowPostInsertContext,
290	view
291);
292
293define_filtered_interceptor!(
294	FilteredViewRowPreUpdateInterceptor,
295	ViewRowPreUpdateInterceptor,
296	ViewRowPreUpdateContext,
297	view
298);
299
300define_filtered_interceptor!(
301	FilteredViewRowPostUpdateInterceptor,
302	ViewRowPostUpdateInterceptor,
303	ViewRowPostUpdateContext,
304	view
305);
306
307define_filtered_interceptor!(
308	FilteredViewRowPreDeleteInterceptor,
309	ViewRowPreDeleteInterceptor,
310	ViewRowPreDeleteContext,
311	view
312);
313
314define_filtered_interceptor!(
315	FilteredViewRowPostDeleteInterceptor,
316	ViewRowPostDeleteInterceptor,
317	ViewRowPostDeleteContext,
318	view
319);
320
321define_filtered_interceptor!(FilteredViewPostCreateInterceptor, ViewPostCreateInterceptor, ViewPostCreateContext, post);
322
323define_filtered_interceptor!(FilteredViewPreUpdateInterceptor, ViewPreUpdateInterceptor, ViewPreUpdateContext, pre);
324
325define_filtered_interceptor!(FilteredViewPostUpdateInterceptor, ViewPostUpdateInterceptor, ViewPostUpdateContext, pre);
326
327define_filtered_interceptor!(FilteredViewPreDeleteInterceptor, ViewPreDeleteInterceptor, ViewPreDeleteContext, pre);
328
329define_filtered_interceptor!(
330	FilteredTablePostCreateInterceptor,
331	TablePostCreateInterceptor,
332	TablePostCreateContext,
333	post
334);
335
336define_filtered_interceptor!(FilteredTablePreUpdateInterceptor, TablePreUpdateInterceptor, TablePreUpdateContext, pre);
337
338define_filtered_interceptor!(
339	FilteredTablePostUpdateInterceptor,
340	TablePostUpdateInterceptor,
341	TablePostUpdateContext,
342	pre
343);
344
345define_filtered_interceptor!(FilteredTablePreDeleteInterceptor, TablePreDeleteInterceptor, TablePreDeleteContext, pre);
346
347define_filtered_interceptor!(
348	FilteredRingBufferPostCreateInterceptor,
349	RingBufferPostCreateInterceptor,
350	RingBufferPostCreateContext,
351	post
352);
353
354define_filtered_interceptor!(
355	FilteredRingBufferPreUpdateInterceptor,
356	RingBufferPreUpdateInterceptor,
357	RingBufferPreUpdateContext,
358	pre
359);
360
361define_filtered_interceptor!(
362	FilteredRingBufferPostUpdateInterceptor,
363	RingBufferPostUpdateInterceptor,
364	RingBufferPostUpdateContext,
365	pre
366);
367
368define_filtered_interceptor!(
369	FilteredRingBufferPreDeleteInterceptor,
370	RingBufferPreDeleteInterceptor,
371	RingBufferPreDeleteContext,
372	pre
373);
374
375define_filtered_interceptor!(
376	FilteredSeriesRowPreInsertInterceptor,
377	SeriesRowPreInsertInterceptor,
378	SeriesRowPreInsertContext,
379	series
380);
381
382define_filtered_interceptor!(
383	FilteredSeriesRowPostInsertInterceptor,
384	SeriesRowPostInsertInterceptor,
385	SeriesRowPostInsertContext,
386	series
387);
388
389define_filtered_interceptor!(
390	FilteredSeriesRowPreUpdateInterceptor,
391	SeriesRowPreUpdateInterceptor,
392	SeriesRowPreUpdateContext,
393	series
394);
395
396define_filtered_interceptor!(
397	FilteredSeriesRowPostUpdateInterceptor,
398	SeriesRowPostUpdateInterceptor,
399	SeriesRowPostUpdateContext,
400	series
401);
402
403define_filtered_interceptor!(
404	FilteredSeriesRowPreDeleteInterceptor,
405	SeriesRowPreDeleteInterceptor,
406	SeriesRowPreDeleteContext,
407	series
408);
409
410define_filtered_interceptor!(
411	FilteredSeriesRowPostDeleteInterceptor,
412	SeriesRowPostDeleteInterceptor,
413	SeriesRowPostDeleteContext,
414	series
415);
416
417define_filtered_interceptor!(
418	FilteredSeriesPostCreateInterceptor,
419	SeriesPostCreateInterceptor,
420	SeriesPostCreateContext,
421	post
422);
423
424define_filtered_interceptor!(
425	FilteredSeriesPreUpdateInterceptor,
426	SeriesPreUpdateInterceptor,
427	SeriesPreUpdateContext,
428	pre
429);
430
431define_filtered_interceptor!(
432	FilteredSeriesPostUpdateInterceptor,
433	SeriesPostUpdateInterceptor,
434	SeriesPostUpdateContext,
435	pre
436);
437
438define_filtered_interceptor!(
439	FilteredSeriesPreDeleteInterceptor,
440	SeriesPreDeleteInterceptor,
441	SeriesPreDeleteContext,
442	pre
443);
444
445define_filtered_interceptor!(
446	FilteredDictionaryRowPreInsertInterceptor,
447	DictionaryRowPreInsertInterceptor,
448	DictionaryRowPreInsertContext,
449	dictionary
450);
451
452define_filtered_interceptor!(
453	FilteredDictionaryRowPostInsertInterceptor,
454	DictionaryRowPostInsertInterceptor,
455	DictionaryRowPostInsertContext,
456	dictionary
457);
458
459define_filtered_interceptor!(
460	FilteredDictionaryRowPreUpdateInterceptor,
461	DictionaryRowPreUpdateInterceptor,
462	DictionaryRowPreUpdateContext,
463	dictionary
464);
465
466define_filtered_interceptor!(
467	FilteredDictionaryRowPostUpdateInterceptor,
468	DictionaryRowPostUpdateInterceptor,
469	DictionaryRowPostUpdateContext,
470	dictionary
471);
472
473define_filtered_interceptor!(
474	FilteredDictionaryRowPreDeleteInterceptor,
475	DictionaryRowPreDeleteInterceptor,
476	DictionaryRowPreDeleteContext,
477	dictionary
478);
479
480define_filtered_interceptor!(
481	FilteredDictionaryRowPostDeleteInterceptor,
482	DictionaryRowPostDeleteInterceptor,
483	DictionaryRowPostDeleteContext,
484	dictionary
485);
486
487define_filtered_interceptor!(
488	FilteredDictionaryPostCreateInterceptor,
489	DictionaryPostCreateInterceptor,
490	DictionaryPostCreateContext,
491	post
492);
493
494define_filtered_interceptor!(
495	FilteredDictionaryPreUpdateInterceptor,
496	DictionaryPreUpdateInterceptor,
497	DictionaryPreUpdateContext,
498	pre
499);
500
501define_filtered_interceptor!(
502	FilteredDictionaryPostUpdateInterceptor,
503	DictionaryPostUpdateInterceptor,
504	DictionaryPostUpdateContext,
505	pre
506);
507
508define_filtered_interceptor!(
509	FilteredDictionaryPreDeleteInterceptor,
510	DictionaryPreDeleteInterceptor,
511	DictionaryPreDeleteContext,
512	pre
513);
514
515define_filtered_interceptor!(
516	FilteredNamespacePostCreateInterceptor,
517	NamespacePostCreateInterceptor,
518	NamespacePostCreateContext,
519	post,
520	name
521);
522
523define_filtered_interceptor!(
524	FilteredNamespacePreUpdateInterceptor,
525	NamespacePreUpdateInterceptor,
526	NamespacePreUpdateContext,
527	pre,
528	name
529);
530
531define_filtered_interceptor!(
532	FilteredNamespacePostUpdateInterceptor,
533	NamespacePostUpdateInterceptor,
534	NamespacePostUpdateContext,
535	pre,
536	name
537);
538
539define_filtered_interceptor!(
540	FilteredNamespacePreDeleteInterceptor,
541	NamespacePreDeleteInterceptor,
542	NamespacePreDeleteContext,
543	pre,
544	name
545);
546
547define_filtered_interceptor!(
548	FilteredIdentityPostCreateInterceptor,
549	IdentityPostCreateInterceptor,
550	IdentityPostCreateContext,
551	post
552);
553
554define_filtered_interceptor!(
555	FilteredIdentityPreUpdateInterceptor,
556	IdentityPreUpdateInterceptor,
557	IdentityPreUpdateContext,
558	pre
559);
560
561define_filtered_interceptor!(
562	FilteredIdentityPostUpdateInterceptor,
563	IdentityPostUpdateInterceptor,
564	IdentityPostUpdateContext,
565	pre
566);
567
568define_filtered_interceptor!(
569	FilteredIdentityPreDeleteInterceptor,
570	IdentityPreDeleteInterceptor,
571	IdentityPreDeleteContext,
572	pre
573);
574
575define_filtered_interceptor!(FilteredRolePostCreateInterceptor, RolePostCreateInterceptor, RolePostCreateContext, post);
576
577define_filtered_interceptor!(FilteredRolePreUpdateInterceptor, RolePreUpdateInterceptor, RolePreUpdateContext, pre);
578
579define_filtered_interceptor!(FilteredRolePostUpdateInterceptor, RolePostUpdateInterceptor, RolePostUpdateContext, pre);
580
581define_filtered_interceptor!(FilteredRolePreDeleteInterceptor, RolePreDeleteInterceptor, RolePreDeleteContext, pre);