qubit_dcl/double_checked/executor_builder.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Initial builder for [`super::DoubleCheckedLockExecutor`].
11//!
12
13use std::marker::PhantomData;
14
15use super::{
16 ExecutionLogger,
17 executor_lock_builder::ExecutorLockBuilder,
18};
19use qubit_lock::Lock;
20
21/// Initial builder for [`super::DoubleCheckedLockExecutor`].
22///
23/// This state has no lock yet. Call [`Self::on`] to attach the lock.
24///
25#[derive(Debug, Default, Clone)]
26pub struct ExecutorBuilder {
27 /// Logger carried forward to later builder states.
28 logger: ExecutionLogger,
29
30 /// Whether panics from tester, callbacks, and task are captured as result errors.
31 catch_panics: bool,
32}
33
34impl ExecutorBuilder {
35 /// Configures logging when the double-checked condition is not met.
36 ///
37 /// # Parameters
38 ///
39 /// * `level` - Log level used for unmet-condition messages.
40 /// * `message` - Full log message emitted when the condition is not met.
41 ///
42 /// # Returns
43 ///
44 /// This builder with unmet-condition logging configured.
45 #[inline]
46 #[must_use = "assign or chain the returned builder"]
47 pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
48 self.logger.set_unmet_condition(Some(level), message);
49 self
50 }
51
52 /// Disables logging when the double-checked condition is not met.
53 ///
54 /// # Returns
55 ///
56 /// This builder with unmet-condition logging disabled.
57 #[inline]
58 #[must_use = "assign or chain the returned builder"]
59 pub fn disable_unmet_condition_logging(mut self) -> Self {
60 self.logger.disable_unmet_condition();
61 self
62 }
63
64 /// Configures logging when the prepare action fails.
65 ///
66 /// # Parameters
67 ///
68 /// * `level` - Log level used for prepare failure messages.
69 /// * `message_prefix` - Prefix placed before the prepare failure text.
70 ///
71 /// # Returns
72 ///
73 /// This builder with prepare failure logging configured.
74 #[inline]
75 #[must_use = "assign or chain the returned builder"]
76 pub fn log_prepare_failure(mut self, level: log::Level, message_prefix: impl Into<String>) -> Self {
77 self.logger.set_prepare_failure(Some(level), message_prefix);
78 self
79 }
80
81 /// Disables logging when the prepare action fails.
82 ///
83 /// # Returns
84 ///
85 /// This builder with prepare failure logging disabled.
86 #[inline]
87 #[must_use = "assign or chain the returned builder"]
88 pub fn disable_prepare_failure_logging(mut self) -> Self {
89 self.logger.disable_prepare_failure();
90 self
91 }
92
93 /// Configures logging when the prepare commit action fails.
94 ///
95 /// # Parameters
96 ///
97 /// * `level` - Log level used for prepare-commit failure messages.
98 /// * `message_prefix` - Prefix placed before the prepare-commit failure
99 /// text.
100 ///
101 /// # Returns
102 ///
103 /// This builder with prepare-commit failure logging configured.
104 #[inline]
105 #[must_use = "assign or chain the returned builder"]
106 pub fn log_prepare_commit_failure(mut self, level: log::Level, message_prefix: impl Into<String>) -> Self {
107 self.logger.set_prepare_commit_failure(Some(level), message_prefix);
108 self
109 }
110
111 /// Disables logging when the prepare commit action fails.
112 ///
113 /// # Returns
114 ///
115 /// This builder with prepare-commit failure logging disabled.
116 #[inline]
117 #[must_use = "assign or chain the returned builder"]
118 pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
119 self.logger.disable_prepare_commit_failure();
120 self
121 }
122
123 /// Configures logging when the prepare rollback action fails.
124 ///
125 /// # Parameters
126 ///
127 /// * `level` - Log level used for prepare-rollback failure messages.
128 /// * `message_prefix` - Prefix placed before the prepare-rollback failure
129 /// text.
130 ///
131 /// # Returns
132 ///
133 /// This builder with prepare-rollback failure logging configured.
134 #[inline]
135 #[must_use = "assign or chain the returned builder"]
136 pub fn log_prepare_rollback_failure(mut self, level: log::Level, message_prefix: impl Into<String>) -> Self {
137 self.logger.set_prepare_rollback_failure(Some(level), message_prefix);
138 self
139 }
140
141 /// Disables logging when the prepare rollback action fails.
142 ///
143 /// # Returns
144 ///
145 /// This builder with prepare-rollback failure logging disabled.
146 #[inline]
147 #[must_use = "assign or chain the returned builder"]
148 pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
149 self.logger.disable_prepare_rollback_failure();
150 self
151 }
152
153 /// Attaches the lock protected by this executor.
154 ///
155 /// # Parameters
156 ///
157 /// * `lock` - The lock handle. Arc-based lock wrappers can be cloned and
158 /// stored here for reusable execution.
159 ///
160 /// # Returns
161 ///
162 /// The builder state that can configure the required tester.
163 #[inline]
164 #[must_use = "assign or chain the returned builder"]
165 pub fn on<L, T>(self, lock: L) -> ExecutorLockBuilder<L, T>
166 where
167 L: Lock<T>,
168 {
169 ExecutorLockBuilder {
170 lock,
171 logger: self.logger,
172 catch_panics: self.catch_panics,
173 _phantom: PhantomData,
174 }
175 }
176
177 /// Enables panic capture for tester, prepare callbacks, and task execution.
178 ///
179 /// # Returns
180 ///
181 /// This builder with panic capture enabled.
182 #[inline]
183 #[must_use = "assign or chain the returned builder"]
184 pub fn catch_panics(mut self) -> Self {
185 self.catch_panics = true;
186 self
187 }
188
189 /// Derives a builder with panic capture enabled or disabled for tester,
190 /// prepare callbacks, and task execution.
191 ///
192 /// # Parameters
193 ///
194 /// * `catch_panics` - `true` to capture panics as execution errors, or
195 /// `false` to let panics unwind.
196 ///
197 /// # Returns
198 ///
199 /// A reconfigured builder with the updated panic-capture setting.
200 #[inline]
201 #[must_use = "assign or chain the returned builder"]
202 pub fn with_panic_capture(mut self, catch_panics: bool) -> Self {
203 self.catch_panics = catch_panics;
204 self
205 }
206
207 /// Disables panic capture for tester, prepare callbacks, and task execution.
208 ///
209 /// # Returns
210 ///
211 /// This builder with panic capture disabled.
212 #[inline]
213 #[must_use = "assign or chain the returned builder"]
214 pub fn disable_catch_panics(mut self) -> Self {
215 self.catch_panics = false;
216 self
217 }
218}