Skip to main content

reifydb_transaction/interceptor/
dictionary.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::interface::catalog::dictionary::Dictionary;
5use reifydb_type::Result;
6
7use crate::interceptor::chain::InterceptorChain;
8
9pub struct DictionaryPostCreateContext<'a> {
10	pub post: &'a Dictionary,
11}
12
13impl<'a> DictionaryPostCreateContext<'a> {
14	pub fn new(post: &'a Dictionary) -> Self {
15		Self {
16			post,
17		}
18	}
19}
20
21pub trait DictionaryPostCreateInterceptor: Send + Sync {
22	fn intercept<'a>(&self, ctx: &mut DictionaryPostCreateContext<'a>) -> Result<()>;
23}
24
25impl InterceptorChain<dyn DictionaryPostCreateInterceptor + Send + Sync> {
26	pub fn execute(&self, mut ctx: DictionaryPostCreateContext) -> Result<()> {
27		for interceptor in &self.interceptors {
28			interceptor.intercept(&mut ctx)?;
29		}
30		Ok(())
31	}
32}
33
34pub struct ClosureDictionaryPostCreateInterceptor<F>
35where
36	F: for<'a> Fn(&mut DictionaryPostCreateContext<'a>) -> Result<()> + Send + Sync,
37{
38	closure: F,
39}
40
41impl<F> ClosureDictionaryPostCreateInterceptor<F>
42where
43	F: for<'a> Fn(&mut DictionaryPostCreateContext<'a>) -> Result<()> + Send + Sync,
44{
45	pub fn new(closure: F) -> Self {
46		Self {
47			closure,
48		}
49	}
50}
51
52impl<F> Clone for ClosureDictionaryPostCreateInterceptor<F>
53where
54	F: for<'a> Fn(&mut DictionaryPostCreateContext<'a>) -> Result<()> + Send + Sync + Clone,
55{
56	fn clone(&self) -> Self {
57		Self {
58			closure: self.closure.clone(),
59		}
60	}
61}
62
63impl<F> DictionaryPostCreateInterceptor for ClosureDictionaryPostCreateInterceptor<F>
64where
65	F: for<'a> Fn(&mut DictionaryPostCreateContext<'a>) -> Result<()> + Send + Sync,
66{
67	fn intercept<'a>(&self, ctx: &mut DictionaryPostCreateContext<'a>) -> Result<()> {
68		(self.closure)(ctx)
69	}
70}
71
72pub fn dictionary_post_create<F>(f: F) -> ClosureDictionaryPostCreateInterceptor<F>
73where
74	F: for<'a> Fn(&mut DictionaryPostCreateContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
75{
76	ClosureDictionaryPostCreateInterceptor::new(f)
77}
78
79pub struct DictionaryPreUpdateContext<'a> {
80	pub pre: &'a Dictionary,
81}
82
83impl<'a> DictionaryPreUpdateContext<'a> {
84	pub fn new(pre: &'a Dictionary) -> Self {
85		Self {
86			pre,
87		}
88	}
89}
90
91pub trait DictionaryPreUpdateInterceptor: Send + Sync {
92	fn intercept<'a>(&self, ctx: &mut DictionaryPreUpdateContext<'a>) -> Result<()>;
93}
94
95impl InterceptorChain<dyn DictionaryPreUpdateInterceptor + Send + Sync> {
96	pub fn execute(&self, mut ctx: DictionaryPreUpdateContext) -> Result<()> {
97		for interceptor in &self.interceptors {
98			interceptor.intercept(&mut ctx)?;
99		}
100		Ok(())
101	}
102}
103
104pub struct ClosureDictionaryPreUpdateInterceptor<F>
105where
106	F: for<'a> Fn(&mut DictionaryPreUpdateContext<'a>) -> Result<()> + Send + Sync,
107{
108	closure: F,
109}
110
111impl<F> ClosureDictionaryPreUpdateInterceptor<F>
112where
113	F: for<'a> Fn(&mut DictionaryPreUpdateContext<'a>) -> Result<()> + Send + Sync,
114{
115	pub fn new(closure: F) -> Self {
116		Self {
117			closure,
118		}
119	}
120}
121
122impl<F> Clone for ClosureDictionaryPreUpdateInterceptor<F>
123where
124	F: for<'a> Fn(&mut DictionaryPreUpdateContext<'a>) -> Result<()> + Send + Sync + Clone,
125{
126	fn clone(&self) -> Self {
127		Self {
128			closure: self.closure.clone(),
129		}
130	}
131}
132
133impl<F> DictionaryPreUpdateInterceptor for ClosureDictionaryPreUpdateInterceptor<F>
134where
135	F: for<'a> Fn(&mut DictionaryPreUpdateContext<'a>) -> Result<()> + Send + Sync,
136{
137	fn intercept<'a>(&self, ctx: &mut DictionaryPreUpdateContext<'a>) -> Result<()> {
138		(self.closure)(ctx)
139	}
140}
141
142pub fn dictionary_pre_update<F>(f: F) -> ClosureDictionaryPreUpdateInterceptor<F>
143where
144	F: for<'a> Fn(&mut DictionaryPreUpdateContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
145{
146	ClosureDictionaryPreUpdateInterceptor::new(f)
147}
148
149pub struct DictionaryPostUpdateContext<'a> {
150	pub pre: &'a Dictionary,
151	pub post: &'a Dictionary,
152}
153
154impl<'a> DictionaryPostUpdateContext<'a> {
155	pub fn new(pre: &'a Dictionary, post: &'a Dictionary) -> Self {
156		Self {
157			pre,
158			post,
159		}
160	}
161}
162
163pub trait DictionaryPostUpdateInterceptor: Send + Sync {
164	fn intercept<'a>(&self, ctx: &mut DictionaryPostUpdateContext<'a>) -> Result<()>;
165}
166
167impl InterceptorChain<dyn DictionaryPostUpdateInterceptor + Send + Sync> {
168	pub fn execute(&self, mut ctx: DictionaryPostUpdateContext) -> Result<()> {
169		for interceptor in &self.interceptors {
170			interceptor.intercept(&mut ctx)?;
171		}
172		Ok(())
173	}
174}
175
176pub struct ClosureDictionaryPostUpdateInterceptor<F>
177where
178	F: for<'a> Fn(&mut DictionaryPostUpdateContext<'a>) -> Result<()> + Send + Sync,
179{
180	closure: F,
181}
182
183impl<F> ClosureDictionaryPostUpdateInterceptor<F>
184where
185	F: for<'a> Fn(&mut DictionaryPostUpdateContext<'a>) -> Result<()> + Send + Sync,
186{
187	pub fn new(closure: F) -> Self {
188		Self {
189			closure,
190		}
191	}
192}
193
194impl<F> Clone for ClosureDictionaryPostUpdateInterceptor<F>
195where
196	F: for<'a> Fn(&mut DictionaryPostUpdateContext<'a>) -> Result<()> + Send + Sync + Clone,
197{
198	fn clone(&self) -> Self {
199		Self {
200			closure: self.closure.clone(),
201		}
202	}
203}
204
205impl<F> DictionaryPostUpdateInterceptor for ClosureDictionaryPostUpdateInterceptor<F>
206where
207	F: for<'a> Fn(&mut DictionaryPostUpdateContext<'a>) -> Result<()> + Send + Sync,
208{
209	fn intercept<'a>(&self, ctx: &mut DictionaryPostUpdateContext<'a>) -> Result<()> {
210		(self.closure)(ctx)
211	}
212}
213
214pub fn dictionary_post_update<F>(f: F) -> ClosureDictionaryPostUpdateInterceptor<F>
215where
216	F: for<'a> Fn(&mut DictionaryPostUpdateContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
217{
218	ClosureDictionaryPostUpdateInterceptor::new(f)
219}
220
221pub struct DictionaryPreDeleteContext<'a> {
222	pub pre: &'a Dictionary,
223}
224
225impl<'a> DictionaryPreDeleteContext<'a> {
226	pub fn new(pre: &'a Dictionary) -> Self {
227		Self {
228			pre,
229		}
230	}
231}
232
233pub trait DictionaryPreDeleteInterceptor: Send + Sync {
234	fn intercept<'a>(&self, ctx: &mut DictionaryPreDeleteContext<'a>) -> Result<()>;
235}
236
237impl InterceptorChain<dyn DictionaryPreDeleteInterceptor + Send + Sync> {
238	pub fn execute(&self, mut ctx: DictionaryPreDeleteContext) -> Result<()> {
239		for interceptor in &self.interceptors {
240			interceptor.intercept(&mut ctx)?;
241		}
242		Ok(())
243	}
244}
245
246pub struct ClosureDictionaryPreDeleteInterceptor<F>
247where
248	F: for<'a> Fn(&mut DictionaryPreDeleteContext<'a>) -> Result<()> + Send + Sync,
249{
250	closure: F,
251}
252
253impl<F> ClosureDictionaryPreDeleteInterceptor<F>
254where
255	F: for<'a> Fn(&mut DictionaryPreDeleteContext<'a>) -> Result<()> + Send + Sync,
256{
257	pub fn new(closure: F) -> Self {
258		Self {
259			closure,
260		}
261	}
262}
263
264impl<F> Clone for ClosureDictionaryPreDeleteInterceptor<F>
265where
266	F: for<'a> Fn(&mut DictionaryPreDeleteContext<'a>) -> Result<()> + Send + Sync + Clone,
267{
268	fn clone(&self) -> Self {
269		Self {
270			closure: self.closure.clone(),
271		}
272	}
273}
274
275impl<F> DictionaryPreDeleteInterceptor for ClosureDictionaryPreDeleteInterceptor<F>
276where
277	F: for<'a> Fn(&mut DictionaryPreDeleteContext<'a>) -> Result<()> + Send + Sync,
278{
279	fn intercept<'a>(&self, ctx: &mut DictionaryPreDeleteContext<'a>) -> Result<()> {
280		(self.closure)(ctx)
281	}
282}
283
284pub fn dictionary_pre_delete<F>(f: F) -> ClosureDictionaryPreDeleteInterceptor<F>
285where
286	F: for<'a> Fn(&mut DictionaryPreDeleteContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
287{
288	ClosureDictionaryPreDeleteInterceptor::new(f)
289}