use std::{
fmt::Display,
marker::PhantomData,
};
use qubit_function::{
ArcRunnable,
ArcTester,
Runnable,
};
use super::{
ExecutionLogger,
double_checked_lock_executor::DoubleCheckedLockExecutor,
};
use crate::lock::Lock;
#[derive(Clone)]
pub struct ExecutorReadyBuilder<L, T> {
pub(in crate::double_checked) lock: L,
pub(in crate::double_checked) tester: ArcTester,
pub(in crate::double_checked) logger: ExecutionLogger,
pub(in crate::double_checked) prepare_action: Option<ArcRunnable<String>>,
pub(in crate::double_checked) rollback_prepare_action: Option<ArcRunnable<String>>,
pub(in crate::double_checked) commit_prepare_action: Option<ArcRunnable<String>>,
pub(in crate::double_checked) _phantom: PhantomData<fn() -> T>,
}
impl<L, T> ExecutorReadyBuilder<L, T>
where
L: Lock<T>,
{
#[inline]
pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
self.logger.set_unmet_condition(Some(level), message);
self
}
#[inline]
pub fn log_prepare_failure(
mut self,
level: log::Level,
message_prefix: impl Into<String>,
) -> Self {
self.logger.set_prepare_failure(Some(level), message_prefix);
self
}
#[inline]
pub fn log_prepare_commit_failure(
mut self,
level: log::Level,
message_prefix: impl Into<String>,
) -> Self {
self.logger
.set_prepare_commit_failure(Some(level), message_prefix);
self
}
#[inline]
pub fn log_prepare_rollback_failure(
mut self,
level: log::Level,
message_prefix: impl Into<String>,
) -> Self {
self.logger
.set_prepare_rollback_failure(Some(level), message_prefix);
self
}
#[inline]
pub fn prepare<Rn, E>(mut self, prepare_action: Rn) -> Self
where
Rn: Runnable<E> + Send + 'static,
E: Display + Send + 'static,
{
let mut action = prepare_action;
self.prepare_action = Some(ArcRunnable::new(move || {
action.run().map_err(|error| error.to_string())
}));
self
}
#[inline]
pub fn rollback_prepare<Rn, E>(mut self, rollback_prepare_action: Rn) -> Self
where
Rn: Runnable<E> + Send + 'static,
E: Display + Send + 'static,
{
let mut action = rollback_prepare_action;
self.rollback_prepare_action = Some(ArcRunnable::new(move || {
action.run().map_err(|error| error.to_string())
}));
self
}
#[inline]
pub fn commit_prepare<Rn, E>(mut self, commit_prepare_action: Rn) -> Self
where
Rn: Runnable<E> + Send + 'static,
E: Display + Send + 'static,
{
let mut action = commit_prepare_action;
self.commit_prepare_action = Some(ArcRunnable::new(move || {
action.run().map_err(|error| error.to_string())
}));
self
}
#[inline]
pub fn build(self) -> DoubleCheckedLockExecutor<L, T> {
DoubleCheckedLockExecutor::new(self)
}
}