use std::marker::PhantomData;
use std::time::Duration;
use crate::attribute::AttributeLock;
use crate::attribute::AttributeMapLoad;
use crate::channel::{ChannelLoad, ChannelMapLoad};
use crate::{AttributeMap, Channel, ChannelMap, RetryPolicy, Step, Value};
pub struct StepOptions<Input> {
pub(crate) wait_for_method_timeout: Option<Duration>,
pub(crate) execute_method_timeout: Option<Duration>,
pub(crate) heartbeat_timeout: Option<Duration>,
pub(crate) wait_for_retry: Option<RetryPolicy>,
pub(crate) execute_retry: Option<RetryPolicy>,
pub(crate) wait_for_failure: WaitForFailurePolicy,
pub(crate) wait_for_durability: StepDurability,
pub(crate) execute_durability: StepDurability,
pub(crate) wait_for_locks: Vec<AttributeLock>,
pub(crate) execute_locks: Vec<AttributeLock>,
pub(crate) wait_for_load_attribute_maps: Vec<AttributeMapLoad>,
pub(crate) wait_for_load_channels: Vec<ChannelLoad>,
pub(crate) wait_for_load_channel_maps: Vec<ChannelMapLoad>,
pub(crate) execute_load_attribute_maps: Vec<AttributeMapLoad>,
pub(crate) execute_load_channels: Vec<ChannelLoad>,
pub(crate) execute_load_channel_maps: Vec<ChannelMapLoad>,
pub(crate) execute_failure_step: Option<&'static str>,
marker: PhantomData<fn(Input)>,
}
#[derive(Clone)]
pub(crate) struct ErasedStepOptions {
pub(crate) wait_for_method_timeout: Option<Duration>,
pub(crate) execute_method_timeout: Option<Duration>,
pub(crate) heartbeat_timeout: Option<Duration>,
pub(crate) wait_for_retry: Option<RetryPolicy>,
pub(crate) execute_retry: Option<RetryPolicy>,
pub(crate) wait_for_failure: WaitForFailurePolicy,
pub(crate) wait_for_durability: StepDurability,
pub(crate) execute_durability: StepDurability,
pub(crate) wait_for_locks: Vec<AttributeLock>,
pub(crate) execute_locks: Vec<AttributeLock>,
pub(crate) wait_for_load_attribute_maps: Vec<AttributeMapLoad>,
pub(crate) wait_for_load_channels: Vec<ChannelLoad>,
pub(crate) wait_for_load_channel_maps: Vec<ChannelMapLoad>,
pub(crate) execute_load_attribute_maps: Vec<AttributeMapLoad>,
pub(crate) execute_load_channels: Vec<ChannelLoad>,
pub(crate) execute_load_channel_maps: Vec<ChannelMapLoad>,
pub(crate) execute_failure_step: Option<&'static str>,
}
impl<Input> From<StepOptions<Input>> for ErasedStepOptions {
fn from(options: StepOptions<Input>) -> Self {
Self {
wait_for_method_timeout: options.wait_for_method_timeout,
execute_method_timeout: options.execute_method_timeout,
heartbeat_timeout: options.heartbeat_timeout,
wait_for_retry: options.wait_for_retry,
execute_retry: options.execute_retry,
wait_for_failure: options.wait_for_failure,
wait_for_durability: options.wait_for_durability,
execute_durability: options.execute_durability,
wait_for_locks: options.wait_for_locks,
execute_locks: options.execute_locks,
wait_for_load_attribute_maps: options.wait_for_load_attribute_maps,
wait_for_load_channels: options.wait_for_load_channels,
wait_for_load_channel_maps: options.wait_for_load_channel_maps,
execute_load_attribute_maps: options.execute_load_attribute_maps,
execute_load_channels: options.execute_load_channels,
execute_load_channel_maps: options.execute_load_channel_maps,
execute_failure_step: options.execute_failure_step,
}
}
}
impl<Input: Value> StepOptions<Input> {
pub fn new() -> Self {
Self {
wait_for_method_timeout: None,
execute_method_timeout: None,
heartbeat_timeout: None,
wait_for_retry: None,
execute_retry: None,
wait_for_failure: WaitForFailurePolicy::FailFlow,
wait_for_durability: StepDurability::Default,
execute_durability: StepDurability::Default,
wait_for_locks: Vec::new(),
execute_locks: Vec::new(),
wait_for_load_attribute_maps: Vec::new(),
wait_for_load_channels: Vec::new(),
wait_for_load_channel_maps: Vec::new(),
execute_load_attribute_maps: Vec::new(),
execute_load_channels: Vec::new(),
execute_load_channel_maps: Vec::new(),
execute_failure_step: None,
marker: PhantomData,
}
}
pub fn wait_for_method_timeout(mut self, value: Duration) -> Self {
self.wait_for_method_timeout = Some(value);
self
}
pub fn execute_method_timeout(mut self, value: Duration) -> Self {
self.execute_method_timeout = Some(value);
self
}
pub fn heartbeat_timeout(mut self, value: Duration) -> Self {
self.heartbeat_timeout = Some(value);
self
}
pub fn wait_for_retry(mut self, value: RetryPolicy) -> Self {
self.wait_for_retry = Some(value);
self
}
pub fn execute_retry(mut self, value: RetryPolicy) -> Self {
self.execute_retry = Some(value);
self
}
pub fn wait_for_failure(mut self, value: WaitForFailurePolicy) -> Self {
self.wait_for_failure = value;
self
}
pub fn on_execute_failure_proceed_to<RecoveryStep>(mut self, step: &RecoveryStep) -> Self
where
RecoveryStep: Step<Input = Input>,
{
self.execute_failure_step = Some(step.step_type());
self
}
pub fn wait_for_durability(mut self, value: StepDurability) -> Self {
self.wait_for_durability = value;
self
}
pub fn execute_durability(mut self, value: StepDurability) -> Self {
self.execute_durability = value;
self
}
pub fn wait_for_lock(mut self, value: AttributeLock) -> Self {
self.wait_for_locks.push(value);
self
}
pub fn execute_lock(mut self, value: AttributeLock) -> Self {
self.execute_locks.push(value);
self
}
pub fn wait_for_load_attribute_map<T>(mut self, attribute_map: &AttributeMap<T>) -> Self {
self.wait_for_load_attribute_maps.push(AttributeMapLoad {
name: attribute_map.name().to_owned(),
instance: None,
});
self
}
pub fn wait_for_load_attribute_map_instance(mut self, load: AttributeMapLoad) -> Self {
self.wait_for_load_attribute_maps.push(load);
self
}
pub fn wait_for_load_channel<T>(mut self, channel: &Channel<T>) -> Self {
self.wait_for_load_channels.push(ChannelLoad {
name: channel.name().to_owned(),
});
self
}
pub fn wait_for_load_channel_map<T>(mut self, channel_map: &ChannelMap<T>) -> Self {
self.wait_for_load_channel_maps.push(ChannelMapLoad {
name: channel_map.name().to_owned(),
instance: None,
});
self
}
pub fn wait_for_load_channel_map_instance(mut self, load: ChannelMapLoad) -> Self {
self.wait_for_load_channel_maps.push(load);
self
}
pub fn execute_load_attribute_map<T>(mut self, attribute_map: &AttributeMap<T>) -> Self {
self.execute_load_attribute_maps.push(AttributeMapLoad {
name: attribute_map.name().to_owned(),
instance: None,
});
self
}
pub fn execute_load_attribute_map_instance(mut self, load: AttributeMapLoad) -> Self {
self.execute_load_attribute_maps.push(load);
self
}
pub fn execute_load_channel<T>(mut self, channel: &Channel<T>) -> Self {
self.execute_load_channels.push(ChannelLoad {
name: channel.name().to_owned(),
});
self
}
pub fn execute_load_channel_map<T>(mut self, channel_map: &ChannelMap<T>) -> Self {
self.execute_load_channel_maps.push(ChannelMapLoad {
name: channel_map.name().to_owned(),
instance: None,
});
self
}
pub fn execute_load_channel_map_instance(mut self, load: ChannelMapLoad) -> Self {
self.execute_load_channel_maps.push(load);
self
}
}
impl<Input: Value> Default for StepOptions<Input> {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum WaitForFailurePolicy {
FailFlow,
Proceed,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum StepDurability {
Default,
Sync,
Async,
}