sabi/lib.rs
1// Copyright (C) 2024-2026 Takayuki Sato. All Rights Reserved.
2// This program is free software under MIT License.
3// See the file LICENSE in this distribution for more details.
4
5//! This crate provides a small framework for Rust, designed to separate application logic
6//! from data access.
7//!
8//! In this framework, the logic exclusively takes a data access trait as its argument,
9//! and all necessary data access is defined by a single data access trait.
10//! Conversely, the concrete implementations of data access methods are provided as default methods
11//! of `DataAcc` derived traits, allowing for flexible grouping, often by data service.
12//!
13//! The `DataHub` bridges these two parts.
14//! It attaches all `DataAcc` derived traits, and then, using the
15//! [override_macro](https://github.com/sttk/override_macro-rust) crate, it overrides
16//! the methods of the data access trait used by the logic to point to the implementations
17//! found in the `DataAcc` derived traits.
18//! This clever use of this macro compensates for Rust's lack of native method overriding,
19//! allowing the logic to interact with data through an abstract interface.
20//!
21//! Furthermore, the `DataHub` provides transaction control for data operations performed
22//! within the logic.
23//! You can execute logic functions with transaction control using its [`DataHub::txn`] method,
24//! or without transaction control using its [`DataHub::run`] method.
25//!
26//! This framework brings clear separation and robustness to Rust application design.
27
28#![cfg_attr(docsrs, feature(doc_cfg))]
29
30mod async_group;
31mod data_acc;
32mod data_conn;
33mod data_hub;
34mod data_src;
35mod non_null;
36
37use std::collections::HashMap;
38use std::sync::Arc;
39use std::{any, cell, marker, ptr, thread};
40
41pub use async_group::AsyncGroupError;
42pub use data_conn::DataConnError;
43pub use data_hub::DataHubError;
44pub use data_src::{create_static_data_src_container, setup, setup_with_order, uses, DataSrcError};
45
46#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
47#[cfg(feature = "tokio")]
48pub mod tokio;
49
50/// The structure that allows for the concurrent execution of multiple functions
51/// using `std::thread` and waits for all of them to complete.
52///
53/// Functions are added using the `add` method and are then run concurrently in separate threads.
54/// The `AsyncGroup` ensures that all tasks finish before proceeding,
55/// and can collect any errors that occur.
56/// This structure implements `Send` and `Sync`.
57pub struct AsyncGroup {
58 handlers: Vec<(Arc<str>, thread::JoinHandle<errs::Result<()>>)>,
59 pub(crate) _name: Arc<str>,
60}
61
62/// The trait that abstracts a connection per session to an external data service,
63/// such as a database, file system, or messaging service.
64///
65/// Its primary purpose is to enable cohesive transaction operations across multiple
66/// external data services within a single transaction context. Implementations of this
67/// trait provide the concrete input/output operations for their respective data services.
68///
69/// Methods declared within this trait are designed to handle transactional logic.
70/// The [`AsyncGroup`] parameter in various methods allows for concurrent processing
71/// when commit or rollback operations are time-consuming.
72#[allow(unused_variables)] // rustdoc
73pub trait DataConn {
74 /// Attempts to commit the changes made within this data connection's transaction.
75 ///
76 /// This method should encapsulate the logic required to finalize the transaction
77 /// for the specific external data service.
78 ///
79 /// # Parameters
80 ///
81 /// * `ag`: A mutable reference to an [`AsyncGroup`] for potentially offloading
82 /// time-consuming commit operations to a separate thread.
83 ///
84 /// # Returns
85 ///
86 /// * `errs::Result<()>`: `Ok(())` if the commit is successful, or an [`errs::Err`]
87 /// if the commit fails.
88 fn commit(&mut self, ag: &mut AsyncGroup) -> errs::Result<()>;
89
90 /// This method is executed before the transaction commit process for all [`DataConn`] instances
91 /// involved in the transaction.
92 ///
93 /// This method provides a timing to execute unusual commit processes or update operations not
94 /// supported by transactions beforehand.
95 /// This allows other update operations to be rolled back if the operations in this method
96 /// fail.
97 fn pre_commit(&mut self, ag: &mut AsyncGroup) -> errs::Result<()> {
98 Ok(())
99 }
100
101 /// This method is executed after the transaction commit process has successfully completed
102 /// for all [`DataConn`] instances involved in the transaction.
103 ///
104 /// It provides a moment to perform follow-up actions that depend on a successful commit.
105 /// For example, after a database commit, a messaging service's [`DataConn`] might use this
106 /// method to send a "transaction completed" message.
107 ///
108 /// # Parameters
109 ///
110 /// * `ag`: A mutable reference to an [`AsyncGroup`] for potentially offloading
111 /// concurrent post-commit operations.
112 fn post_commit(&mut self, ag: &mut AsyncGroup) {}
113
114 /// Determines whether a "force back" operation is required for this data connection.
115 ///
116 /// A force back is typically executed if one external data service successfully commits
117 /// its changes, but a subsequent external data service within the same transaction fails
118 /// its commit. This method indicates if the committed changes of *this* data service
119 /// need to be undone (forced back).
120 ///
121 /// # Returns
122 ///
123 /// * `bool`: `true` if a force back is needed for this connection, `false` otherwise.
124 fn should_force_back(&self) -> bool {
125 false
126 }
127
128 /// Rolls back any changes made within this data connection's transaction.
129 ///
130 /// This method undoes all operations performed since the beginning of the transaction,
131 /// restoring the data service to its state before the transaction began.
132 ///
133 /// # Parameters
134 ///
135 /// * `ag`: A mutable reference to an [`AsyncGroup`] for potentially offloading
136 /// time-consuming rollback operations to a separate thread.
137 fn rollback(&mut self, ag: &mut AsyncGroup);
138
139 /// Executes an operation to revert committed changes.
140 ///
141 /// This method provides an opportunity to undo changes that were successfully committed
142 /// to this external data service, typically when a commit fails for *another* data service
143 /// within the same distributed transaction, necessitating a rollback of already committed
144 /// changes.
145 ///
146 /// # Parameters
147 ///
148 /// * `ag`: A mutable reference to an [`AsyncGroup`] for potentially offloading
149 /// concurrent force back operations.
150 fn force_back(&mut self, ag: &mut AsyncGroup) {}
151
152 /// Closes the connection to the external data service.
153 ///
154 /// This method should release any resources held by the data connection, ensuring
155 /// a graceful shutdown of the connection.
156 fn close(&mut self);
157}
158
159struct NoopDataConn {}
160
161impl DataConn for NoopDataConn {
162 fn commit(&mut self, _ag: &mut AsyncGroup) -> errs::Result<()> {
163 Ok(())
164 }
165 fn rollback(&mut self, _ag: &mut AsyncGroup) {}
166 fn close(&mut self) {}
167}
168
169#[repr(C)]
170struct DataConnContainer<C = NoopDataConn>
171where
172 C: DataConn + 'static,
173{
174 drop_fn: fn(*const DataConnContainer),
175 is_fn: fn(any::TypeId) -> bool,
176 commit_fn: fn(*const DataConnContainer, &mut AsyncGroup) -> errs::Result<()>,
177 pre_commit_fn: fn(*const DataConnContainer, &mut AsyncGroup) -> errs::Result<()>,
178 post_commit_fn: fn(*const DataConnContainer, &mut AsyncGroup),
179 should_force_back_fn: fn(*const DataConnContainer) -> bool,
180 rollback_fn: fn(*const DataConnContainer, &mut AsyncGroup),
181 force_back_fn: fn(*const DataConnContainer, &mut AsyncGroup),
182 close_fn: fn(*const DataConnContainer),
183
184 name: Arc<str>,
185 data_conn: Box<C>,
186}
187
188struct DataConnManager {
189 vec: Vec<Option<SendSyncNonNull<DataConnContainer>>>,
190 index_map: HashMap<Arc<str>, usize>,
191}
192
193/// The trait that abstracts a data source responsible for managing connections
194/// to external data services, such as databases, file systems, or messaging services.
195///
196/// It receives configuration for connecting to an external data service and then
197/// creates and supplies [`DataConn`] instance, representing a single session connection.
198#[allow(unused_variables)] // for rustdoc
199pub trait DataSrc<C>
200where
201 C: DataConn + 'static,
202{
203 /// Performs the setup process for the data source.
204 ///
205 /// This method is responsible for establishing global connections, configuring
206 /// connection pools, or performing any necessary initializations required
207 /// before [`DataConn`] instances can be created.
208 ///
209 /// # Parameters
210 ///
211 /// * `ag`: A mutable reference to an [`AsyncGroup`]. This is used if the setup
212 /// process is potentially time-consuming and can benefit from concurrent
213 /// execution in a separate thread.
214 ///
215 /// # Returns
216 ///
217 /// * `errs::Result<()>`: `Ok(())` if the setup is successful, or an [`errs::Err`]
218 /// if any part of the setup fails.
219 fn setup(&mut self, ag: &mut AsyncGroup) -> errs::Result<()>;
220
221 /// Closes the data source and releases any globally held resources.
222 ///
223 /// This method should perform cleanup operations, such as closing global connections
224 /// or shutting down connection pools, that were established during the setup process.
225 fn close(&mut self);
226
227 /// Creates a new [`DataConn`] instance which is a connection per session.
228 ///
229 /// Each call to this method should yield a distinct [`DataConn`] object tailored
230 /// for a single session's operations.
231 ///
232 /// # Returns
233 ///
234 /// * `errs::Result<Box<C>>`: `Ok(Box<C>)` containing the newly created [`DataConn`]
235 /// if successful, or an [`errs::Err`] if the connection could not be created.
236 fn create_data_conn(&mut self) -> errs::Result<Box<C>>;
237}
238
239struct NoopDataSrc {}
240
241impl DataSrc<NoopDataConn> for NoopDataSrc {
242 fn setup(&mut self, _ag: &mut AsyncGroup) -> errs::Result<()> {
243 Ok(())
244 }
245 fn close(&mut self) {}
246 fn create_data_conn(&mut self) -> errs::Result<Box<NoopDataConn>> {
247 Ok(Box::new(NoopDataConn {}))
248 }
249}
250
251#[repr(C)]
252struct DataSrcContainer<S = NoopDataSrc, C = NoopDataConn>
253where
254 S: DataSrc<C>,
255 C: DataConn + 'static,
256{
257 drop_fn: fn(*const DataSrcContainer),
258 setup_fn: fn(*const DataSrcContainer, &mut AsyncGroup) -> errs::Result<()>,
259 close_fn: fn(*const DataSrcContainer),
260 create_data_conn_fn: fn(*const DataSrcContainer) -> errs::Result<Box<DataConnContainer<C>>>,
261 is_data_conn_fn: fn(any::TypeId) -> bool,
262
263 local: bool,
264 name: Arc<str>,
265 data_src: S,
266}
267
268struct DataSrcManager {
269 vec_unready: Vec<SendSyncNonNull<DataSrcContainer>>,
270 vec_ready: Vec<SendSyncNonNull<DataSrcContainer>>,
271 local: bool,
272}
273
274/// A utility struct that ensures to close and drop global data sources when it goes out of scope.
275///
276/// This struct implements the `Drop` trait, and its `drop` method handles the closing and
277/// dropping of registered global data sources.
278/// Therefore, this ensures that these operations are automatically executed at the end of
279/// the scope.
280///
281/// **NOTE:** Do not receive an instance of this struct into an anonymous variable
282/// (`let _ = ...`), because an anonymous variable is dropped immediately at that point.
283pub struct AutoShutdown {}
284
285/// The struct that acts as a central hub for data input/output operations, integrating
286/// multiple *Data* traits (which are passed to business logic functions as their arguments) with
287/// [`DataAcc`] traits (which implement default data I/O methods for external services).
288///
289/// It facilitates data access by providing [`DataConn`] objects, created from
290/// both global data sources (registered via the global [`uses!`] macro) and
291/// session-local data sources (registered via [`DataHub::uses`] method).
292///
293/// The [`DataHub`] is capable of performing aggregated transactional operations
294/// on all [`DataConn`] objects created from its registered [`DataSrc`] instances.
295///
296/// This structure implements `Send`.
297pub struct DataHub {
298 local_data_src_manager: DataSrcManager,
299 data_src_map: HashMap<Arc<str>, (bool, usize)>,
300 data_conn_manager: DataConnManager,
301 fixed: bool,
302}
303
304/// This trait provides a mechanism to retrieve a mutable reference to a [`DataConn`] object
305/// by name, creating it if necessary.
306///
307/// It is typically implemented as a derived trait with default methods (using
308/// the `override_macro` crate) on [`DataHub`], allowing application logic to
309/// interact with data services through an abstract interface.
310pub trait DataAcc {
311 /// Retrieves a mutable reference to a [`DataConn`] object by name, creating it if necessary.
312 ///
313 /// This is the core method used by [`DataAcc`] implementations to obtain connections
314 /// to external data services. It first checks if a [`DataConn`] with the given name
315 /// already exists in the current session. If not, it attempts to find a
316 /// corresponding [`DataSrc`] and create a new [`DataConn`] from it.
317 ///
318 /// # Type Parameters
319 ///
320 /// * `C`: The concrete type of [`DataConn`] expected.
321 ///
322 /// # Parameters
323 ///
324 /// * `name`: The name of the data source/connection to retrieve.
325 ///
326 /// # Returns
327 ///
328 /// * `errs::Result<&mut C>`: A mutable reference to the [`DataConn`] instance if successful,
329 /// or an [`errs::Err`] if the data source is not found, or if the retrieved/created
330 /// [`DataConn`] cannot be cast to the specified type `C`.
331 fn get_data_conn<C: DataConn + 'static>(
332 &mut self,
333 name: impl AsRef<str>,
334 ) -> errs::Result<&mut C>;
335}
336
337#[doc(hidden)]
338pub struct StaticDataSrcContainer {
339 ssnnptr: SendSyncNonNull<DataSrcContainer>,
340}
341
342#[doc(hidden)]
343pub struct StaticDataSrcRegistration {
344 factory: fn() -> StaticDataSrcContainer,
345}
346
347struct SendSyncNonNull<T: Send + Sync> {
348 non_null_ptr: ptr::NonNull<T>,
349 _phantom: marker::PhantomData<cell::Cell<T>>,
350}