use std::sync::Arc;
use std::time::Duration;
use crate::value::VmClosure;
#[derive(Clone)]
pub struct TriggerExpressionSpec {
pub raw: String,
pub closure: Arc<VmClosure>,
}
impl std::fmt::Debug for TriggerExpressionSpec {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TriggerExpressionSpec")
.field("raw", &self.raw)
.finish()
}
}
#[derive(Clone, Debug)]
pub struct TriggerConcurrencyConfig {
pub key: Option<TriggerExpressionSpec>,
pub max: u32,
}
#[derive(Clone, Debug)]
pub struct TriggerThrottleConfig {
pub key: Option<TriggerExpressionSpec>,
pub period: Duration,
pub max: u32,
}
#[derive(Clone, Debug)]
pub struct TriggerRateLimitConfig {
pub key: Option<TriggerExpressionSpec>,
pub period: Duration,
pub max: u32,
}
#[derive(Clone, Debug)]
pub struct TriggerDebounceConfig {
pub key: TriggerExpressionSpec,
pub period: Duration,
}
#[derive(Clone, Debug)]
pub struct TriggerSingletonConfig {
pub key: Option<TriggerExpressionSpec>,
}
#[derive(Clone, Debug)]
pub struct TriggerBatchConfig {
pub key: Option<TriggerExpressionSpec>,
pub size: u32,
pub timeout: Duration,
}
#[derive(Clone, Debug)]
pub struct TriggerPriorityOrderConfig {
pub key: TriggerExpressionSpec,
pub order: Vec<String>,
}
#[derive(Clone, Debug, Default)]
pub struct TriggerFlowControlConfig {
pub concurrency: Option<TriggerConcurrencyConfig>,
pub throttle: Option<TriggerThrottleConfig>,
pub rate_limit: Option<TriggerRateLimitConfig>,
pub debounce: Option<TriggerDebounceConfig>,
pub singleton: Option<TriggerSingletonConfig>,
pub batch: Option<TriggerBatchConfig>,
pub priority: Option<TriggerPriorityOrderConfig>,
}
pub fn parse_flow_control_duration(raw: &str) -> Result<Duration, String> {
let Some((amount, unit)) = crate::duration_parse::split_amount_unit(raw) else {
return Err(format!("invalid duration '{raw}': expected <int><unit>"));
};
if unit.is_empty() {
return Err(format!("invalid duration '{raw}': missing unit suffix"));
}
let value = amount
.parse::<u64>()
.map_err(|_| format!("invalid duration '{raw}': expected integer prefix"))?;
if value == 0 {
return Err(format!(
"invalid duration '{raw}': duration must be positive"
));
}
let secs_factor = match unit.as_str() {
"s" | "m" | "h" | "d" | "w" => {
crate::duration_parse::unit_to_millis(&unit).unwrap() / 1_000
}
other => {
return Err(format!(
"invalid duration '{raw}': unsupported unit '{other}', expected s/m/h/d/w"
))
}
};
Ok(Duration::from_secs(value.saturating_mul(secs_factor)))
}