reifydb_core/interceptor/
factory.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use crate::{interceptor::Interceptors, interface::CommandTransaction};
5
6/// Factory trait for creating interceptor instances for each CommandTransaction
7pub trait InterceptorFactory<CT: CommandTransaction>: Send + Sync {
8	/// Create a new instance of interceptors for a CommandTransaction
9	fn create(&self) -> Interceptors<CT>;
10}
11
12/// Standard implementation of InterceptorFactory that stores factory functions
13/// This allows the factory to be Send+Sync while creating non-Send/Sync
14/// interceptors
15pub struct StandardInterceptorFactory<CT: CommandTransaction> {
16	pub(crate) factories: Vec<Box<dyn Fn(&mut Interceptors<CT>) + Send + Sync>>,
17}
18
19impl<CT: CommandTransaction> Default for StandardInterceptorFactory<CT> {
20	fn default() -> Self {
21		Self {
22			factories: Vec::new(),
23		}
24	}
25}
26
27impl<CT: CommandTransaction> StandardInterceptorFactory<CT> {
28	/// Add a custom factory that directly registers interceptors
29	pub fn add(&mut self, factory: Box<dyn Fn(&mut Interceptors<CT>) + Send + Sync>) {
30		self.factories.push(factory);
31	}
32}
33
34impl<CT: CommandTransaction> InterceptorFactory<CT> for StandardInterceptorFactory<CT> {
35	fn create(&self) -> Interceptors<CT> {
36		let mut interceptors = Interceptors::new();
37
38		for factory in &self.factories {
39			factory(&mut interceptors);
40		}
41
42		interceptors
43	}
44}