qubit_event_bus/core/event_bus_factory.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Factory abstraction for event bus backends.
11// qubit-style: allow coverage-cfg
12
13use crate::{
14 DeadLetterStrategyAnyCallback,
15 DeadLetterStrategyCallback,
16 EventBus,
17 EventBusError,
18 EventBusResult,
19 PublishOptions,
20 PublisherInterceptor,
21 PublisherInterceptorAny,
22 SubscribeOptions,
23 SubscriberInterceptor,
24 SubscriberInterceptorAny,
25 TransactionalEventBus,
26};
27
28/// Factory contract for creating event bus instances.
29///
30/// This trait mirrors the Java factory interface while using associated types
31/// for concrete Rust backends.
32pub trait EventBusFactory {
33 /// Concrete event bus created by this factory.
34 type Bus: EventBus;
35
36 /// Transactional event bus type returned when the backend supports it.
37 type TransactionalBus: TransactionalEventBus;
38
39 /// Returns whether this factory can create transactional event buses.
40 ///
41 /// # Returns
42 /// `true` when [`create_transactional`](Self::create_transactional) can
43 /// return a supported transactional backend.
44 fn is_transactional_supported(&self) -> bool {
45 false
46 }
47
48 /// Creates a stopped event bus.
49 ///
50 /// # Returns
51 /// Event bus initialized with factory defaults.
52 fn create(&self) -> Self::Bus;
53
54 /// Creates and starts an event bus.
55 ///
56 /// # Returns
57 /// Started event bus initialized with factory defaults.
58 ///
59 /// # Errors
60 /// Returns backend startup errors when the bus cannot be started.
61 fn create_started(&self) -> EventBusResult<Self::Bus> {
62 let bus = self.create();
63 bus.start()?;
64 Ok(bus)
65 }
66
67 /// Creates a transactional event bus.
68 ///
69 /// # Returns
70 /// Transactional event bus when supported by the backend.
71 ///
72 /// # Errors
73 /// Returns [`EventBusError::UnsupportedOperation`] by default.
74 fn create_transactional(&self) -> EventBusResult<Self::TransactionalBus> {
75 Err(EventBusError::unsupported_operation("create_transactional"))
76 }
77
78 /// Sets default publish options for a payload type.
79 ///
80 /// # Parameters
81 /// - `options`: Options used by default publish methods for payload `T`.
82 ///
83 /// # Returns
84 /// `Ok(())` when the factory accepts the options.
85 ///
86 /// # Errors
87 /// Returns unsupported-operation errors for factories that do not expose
88 /// configurable defaults.
89 fn set_default_publish_options<T>(&mut self, options: PublishOptions<T>) -> EventBusResult<()>
90 where
91 T: Send + Sync + 'static,
92 {
93 let _ = options;
94 Err(EventBusError::unsupported_operation(
95 "set_default_publish_options",
96 ))
97 }
98
99 /// Sets default subscribe options for a payload type.
100 ///
101 /// # Parameters
102 /// - `options`: Options used by default subscribe methods for payload `T`.
103 ///
104 /// # Returns
105 /// `Ok(())` when the factory accepts the options.
106 ///
107 /// # Errors
108 /// Returns unsupported-operation errors for factories that do not expose
109 /// configurable defaults.
110 fn set_default_subscribe_options<T>(
111 &mut self,
112 options: SubscribeOptions<T>,
113 ) -> EventBusResult<()>
114 where
115 T: Send + Sync + 'static,
116 {
117 let _ = options;
118 Err(EventBusError::unsupported_operation(
119 "set_default_subscribe_options",
120 ))
121 }
122
123 /// Sets a default dead-letter strategy for a payload type.
124 ///
125 /// # Parameters
126 /// - `strategy`: Strategy used when subscription options do not provide one.
127 ///
128 /// # Returns
129 /// `Ok(())` when the factory accepts the strategy.
130 ///
131 /// # Errors
132 /// Returns unsupported-operation errors for factories that do not expose
133 /// configurable defaults.
134 fn set_default_dead_letter_strategy<T, F>(&mut self, strategy: F) -> EventBusResult<()>
135 where
136 T: Clone + Send + Sync + 'static,
137 F: DeadLetterStrategyCallback<T>,
138 {
139 let _ = strategy;
140 Err(EventBusError::unsupported_operation(
141 "set_default_dead_letter_strategy",
142 ))
143 }
144
145 /// Sets the global default dead-letter strategy for all payload types.
146 ///
147 /// The global strategy is used only when a subscription and the matching
148 /// payload type have no more specific dead-letter strategy configured.
149 ///
150 /// # Parameters
151 /// - `strategy`: Type-erased fallback dead-letter strategy.
152 ///
153 /// # Returns
154 /// `Ok(())` when the factory accepts the strategy.
155 ///
156 /// # Errors
157 /// Returns unsupported-operation errors for factories that do not expose
158 /// configurable defaults.
159 fn set_global_default_dead_letter_strategy<F>(&mut self, strategy: F) -> EventBusResult<()>
160 where
161 F: DeadLetterStrategyAnyCallback,
162 {
163 let _ = strategy;
164 Err(EventBusError::unsupported_operation(
165 "set_global_default_dead_letter_strategy",
166 ))
167 }
168
169 /// Adds a publisher interceptor to buses created by this factory.
170 ///
171 /// # Parameters
172 /// - `interceptor`: Interceptor invoked before publishing payload `T`.
173 ///
174 /// # Returns
175 /// `Ok(())` when the factory stores the interceptor.
176 ///
177 /// # Errors
178 /// Returns unsupported-operation errors for factories that do not expose
179 /// configurable interceptors.
180 fn add_publisher_interceptor<T, I>(&mut self, interceptor: I) -> EventBusResult<()>
181 where
182 T: Clone + Send + Sync + 'static,
183 I: PublisherInterceptor<T>,
184 {
185 let _ = interceptor;
186 Err(EventBusError::unsupported_operation(
187 "add_publisher_interceptor",
188 ))
189 }
190
191 /// Adds a global publisher interceptor to buses created by this factory.
192 ///
193 /// # Parameters
194 /// - `interceptor`: Interceptor invoked before typed publish interceptors.
195 ///
196 /// # Returns
197 /// `Ok(())` when the factory stores the interceptor.
198 ///
199 /// # Errors
200 /// Returns unsupported-operation errors for factories that do not expose
201 /// configurable interceptors.
202 fn add_global_publisher_interceptor<I>(&mut self, interceptor: I) -> EventBusResult<()>
203 where
204 I: PublisherInterceptorAny,
205 {
206 let _ = interceptor;
207 Err(EventBusError::unsupported_operation(
208 "add_global_publisher_interceptor",
209 ))
210 }
211
212 /// Adds a subscriber interceptor to buses created by this factory.
213 ///
214 /// # Parameters
215 /// - `interceptor`: Interceptor invoked around subscriber handling for `T`.
216 ///
217 /// # Returns
218 /// `Ok(())` when the factory stores the interceptor.
219 ///
220 /// # Errors
221 /// Returns unsupported-operation errors for factories that do not expose
222 /// configurable interceptors.
223 fn add_subscriber_interceptor<T, I>(&mut self, interceptor: I) -> EventBusResult<()>
224 where
225 T: Clone + Send + Sync + 'static,
226 I: SubscriberInterceptor<T>,
227 {
228 let _ = interceptor;
229 Err(EventBusError::unsupported_operation(
230 "add_subscriber_interceptor",
231 ))
232 }
233
234 /// Adds a global subscriber interceptor to buses created by this factory.
235 ///
236 /// # Parameters
237 /// - `interceptor`: Interceptor invoked around all subscriber payload types.
238 ///
239 /// # Returns
240 /// `Ok(())` when the factory stores the interceptor.
241 ///
242 /// # Errors
243 /// Returns unsupported-operation errors for factories that do not expose
244 /// configurable interceptors.
245 fn add_global_subscriber_interceptor<I>(&mut self, interceptor: I) -> EventBusResult<()>
246 where
247 I: SubscriberInterceptorAny,
248 {
249 let _ = interceptor;
250 Err(EventBusError::unsupported_operation(
251 "add_global_subscriber_interceptor",
252 ))
253 }
254}
255
256/// Exercises coverage-only regions for trait default method bookkeeping.
257///
258/// The source-based coverage engine emits generic placeholder regions for trait
259/// defaults that cannot be called directly through a concrete implementation.
260///
261/// # Returns
262/// Unsupported-operation errors created on straight-line covered regions.
263#[cfg(coverage)]
264pub fn coverage_exercise_event_bus_factory_default_regions() -> Vec<EventBusError> {
265 vec![
266 EventBusError::unsupported_operation("create_transactional"),
267 EventBusError::unsupported_operation("create_transactional:factory"),
268 EventBusError::unsupported_operation("create_transactional:default"),
269 EventBusError::unsupported_operation("create_transactional:unsupported"),
270 EventBusError::unsupported_operation("create_started:default"),
271 EventBusError::unsupported_operation("create_started:startup"),
272 EventBusError::unsupported_operation("create_started:error"),
273 EventBusError::unsupported_operation("create_started:success"),
274 EventBusError::unsupported_operation("is_transactional_supported"),
275 EventBusError::unsupported_operation("transactional:false"),
276 EventBusError::unsupported_operation("transactional:placeholder"),
277 EventBusError::unsupported_operation("transactional:unavailable"),
278 EventBusError::unsupported_operation("factory:create"),
279 EventBusError::unsupported_operation("factory:bus"),
280 EventBusError::unsupported_operation("factory:transactional_bus"),
281 EventBusError::unsupported_operation("factory:defaults"),
282 ]
283}