Skip to main content

reifydb_transaction/interceptor/
identity.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_core::interface::catalog::identity::Identity;
5use reifydb_value::Result;
6
7use crate::interceptor::chain::InterceptorChain;
8
9pub struct IdentityPostCreateContext<'a> {
10	pub post: &'a Identity,
11}
12
13impl<'a> IdentityPostCreateContext<'a> {
14	pub fn new(post: &'a Identity) -> Self {
15		Self {
16			post,
17		}
18	}
19}
20
21pub trait IdentityPostCreateInterceptor: Send + Sync {
22	fn intercept<'a>(&self, ctx: &mut IdentityPostCreateContext<'a>) -> Result<()>;
23}
24
25impl InterceptorChain<dyn IdentityPostCreateInterceptor + Send + Sync> {
26	pub fn execute(&self, mut ctx: IdentityPostCreateContext) -> Result<()> {
27		for interceptor in &self.interceptors {
28			interceptor.intercept(&mut ctx)?;
29		}
30		Ok(())
31	}
32}
33
34pub struct ClosureIdentityPostCreateInterceptor<F>
35where
36	F: for<'a> Fn(&mut IdentityPostCreateContext<'a>) -> Result<()> + Send + Sync,
37{
38	closure: F,
39}
40
41impl<F> ClosureIdentityPostCreateInterceptor<F>
42where
43	F: for<'a> Fn(&mut IdentityPostCreateContext<'a>) -> Result<()> + Send + Sync,
44{
45	pub fn new(closure: F) -> Self {
46		Self {
47			closure,
48		}
49	}
50}
51
52impl<F> Clone for ClosureIdentityPostCreateInterceptor<F>
53where
54	F: for<'a> Fn(&mut IdentityPostCreateContext<'a>) -> Result<()> + Send + Sync + Clone,
55{
56	fn clone(&self) -> Self {
57		Self {
58			closure: self.closure.clone(),
59		}
60	}
61}
62
63impl<F> IdentityPostCreateInterceptor for ClosureIdentityPostCreateInterceptor<F>
64where
65	F: for<'a> Fn(&mut IdentityPostCreateContext<'a>) -> Result<()> + Send + Sync,
66{
67	fn intercept<'a>(&self, ctx: &mut IdentityPostCreateContext<'a>) -> Result<()> {
68		(self.closure)(ctx)
69	}
70}
71
72pub fn identity_post_create<F>(f: F) -> ClosureIdentityPostCreateInterceptor<F>
73where
74	F: for<'a> Fn(&mut IdentityPostCreateContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
75{
76	ClosureIdentityPostCreateInterceptor::new(f)
77}
78
79pub struct IdentityPreDeleteContext<'a> {
80	pub pre: &'a Identity,
81}
82
83impl<'a> IdentityPreDeleteContext<'a> {
84	pub fn new(pre: &'a Identity) -> Self {
85		Self {
86			pre,
87		}
88	}
89}
90
91pub trait IdentityPreDeleteInterceptor: Send + Sync {
92	fn intercept<'a>(&self, ctx: &mut IdentityPreDeleteContext<'a>) -> Result<()>;
93}
94
95impl InterceptorChain<dyn IdentityPreDeleteInterceptor + Send + Sync> {
96	pub fn execute(&self, mut ctx: IdentityPreDeleteContext) -> Result<()> {
97		for interceptor in &self.interceptors {
98			interceptor.intercept(&mut ctx)?;
99		}
100		Ok(())
101	}
102}
103
104pub struct ClosureIdentityPreDeleteInterceptor<F>
105where
106	F: for<'a> Fn(&mut IdentityPreDeleteContext<'a>) -> Result<()> + Send + Sync,
107{
108	closure: F,
109}
110
111impl<F> ClosureIdentityPreDeleteInterceptor<F>
112where
113	F: for<'a> Fn(&mut IdentityPreDeleteContext<'a>) -> Result<()> + Send + Sync,
114{
115	pub fn new(closure: F) -> Self {
116		Self {
117			closure,
118		}
119	}
120}
121
122impl<F> Clone for ClosureIdentityPreDeleteInterceptor<F>
123where
124	F: for<'a> Fn(&mut IdentityPreDeleteContext<'a>) -> Result<()> + Send + Sync + Clone,
125{
126	fn clone(&self) -> Self {
127		Self {
128			closure: self.closure.clone(),
129		}
130	}
131}
132
133impl<F> IdentityPreDeleteInterceptor for ClosureIdentityPreDeleteInterceptor<F>
134where
135	F: for<'a> Fn(&mut IdentityPreDeleteContext<'a>) -> Result<()> + Send + Sync,
136{
137	fn intercept<'a>(&self, ctx: &mut IdentityPreDeleteContext<'a>) -> Result<()> {
138		(self.closure)(ctx)
139	}
140}
141
142pub fn identity_pre_delete<F>(f: F) -> ClosureIdentityPreDeleteInterceptor<F>
143where
144	F: for<'a> Fn(&mut IdentityPreDeleteContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
145{
146	ClosureIdentityPreDeleteInterceptor::new(f)
147}