Skip to main content

reifydb_transaction/interceptor/
namespace.rs

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