oximedia-workflow 0.2.0

Comprehensive workflow orchestration engine for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Conditional step execution for workflow orchestration.
//!
//! [`StepCondition`] variants cover static conditions (`Always`), success/failure
//! guards, direct field lookups, expression strings (`"field op value"`), and
//! recursive logical combinators (`And`, `Or`, `Not`).
//!
//! Use [`ConditionEvaluator::evaluate`] to test a condition against a
//! [`StepContext`] gathered at runtime.

#![allow(dead_code)]

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ── StepCondition ─────────────────────────────────────────────────────────────

/// A condition that gates whether a workflow step should execute.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StepCondition {
    /// Always execute — the step is unconditional.
    Always,
    /// Execute only when the immediately preceding step succeeded.
    OnPreviousSuccess,
    /// Execute only when the immediately preceding step failed.
    OnPreviousFailure,
    /// Evaluate a simple expression of the form `"field op value"`.
    ///
    /// Supported operators: `>`, `<`, `==`, `!=`, `>=`, `<=`, `contains`.
    Expression(String),
    /// Execute when `ctx.previous_output[field] == value`.
    FieldEquals {
        /// Output field key.
        field: String,
        /// Expected value.
        value: String,
    },
    /// Execute when `ctx.previous_output[field]` contains `substring`.
    FieldContains {
        /// Output field key.
        field: String,
        /// Required substring.
        substring: String,
    },
    /// All sub-conditions must be true (short-circuit).
    And(Vec<StepCondition>),
    /// At least one sub-condition must be true (short-circuit).
    Or(Vec<StepCondition>),
    /// Negation of the inner condition.
    Not(Box<StepCondition>),
}

// ── StepContext ───────────────────────────────────────────────────────────────

/// Runtime context supplied to [`ConditionEvaluator::evaluate`].
#[derive(Debug, Clone)]
pub struct StepContext {
    /// Identifier of the step whose condition is being evaluated.
    pub step_id: String,
    /// Whether the immediately preceding step completed successfully.
    pub previous_success: bool,
    /// Key/value output produced by the preceding step.
    pub previous_output: HashMap<String, String>,
}

impl StepContext {
    /// Create a context representing a successful previous step with no output.
    #[must_use]
    pub fn new(step_id: impl Into<String>) -> Self {
        Self {
            step_id: step_id.into(),
            previous_success: true,
            previous_output: HashMap::new(),
        }
    }

    /// Override the success flag.
    #[must_use]
    pub fn with_success(mut self, success: bool) -> Self {
        self.previous_success = success;
        self
    }

    /// Insert an output key/value pair.
    #[must_use]
    pub fn with_output(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.previous_output.insert(key.into(), value.into());
        self
    }
}

// ── ConditionEvaluator ───────────────────────────────────────────────────────

/// Evaluates [`StepCondition`]s against a [`StepContext`].
pub struct ConditionEvaluator;

impl ConditionEvaluator {
    /// Evaluate `condition` using the runtime values in `ctx`.
    ///
    /// `And` and `Or` short-circuit: evaluation stops as soon as the result
    /// is determined.
    #[must_use]
    pub fn evaluate(condition: &StepCondition, ctx: &StepContext) -> bool {
        match condition {
            StepCondition::Always => true,
            StepCondition::OnPreviousSuccess => ctx.previous_success,
            StepCondition::OnPreviousFailure => !ctx.previous_success,
            StepCondition::Expression(expr) => evaluate_expression(expr, ctx),
            StepCondition::FieldEquals { field, value } => {
                ctx.previous_output.get(field).map_or(false, |v| v == value)
            }
            StepCondition::FieldContains { field, substring } => ctx
                .previous_output
                .get(field)
                .map_or(false, |v| v.contains(substring.as_str())),
            StepCondition::And(conditions) => conditions.iter().all(|c| Self::evaluate(c, ctx)),
            StepCondition::Or(conditions) => conditions.iter().any(|c| Self::evaluate(c, ctx)),
            StepCondition::Not(inner) => !Self::evaluate(inner, ctx),
        }
    }
}

// ── Expression parser / evaluator ────────────────────────────────────────────

/// Parse and evaluate a simple expression `"field op value"` against `ctx`.
///
/// Supported `op` tokens: `>=`, `<=`, `!=`, `==`, `>`, `<`, `contains`.
/// The `field` is looked up in `ctx.previous_output`; the comparison is
/// attempted numerically first, then as a string equality/containment.
fn evaluate_expression(expr: &str, ctx: &StepContext) -> bool {
    let expr = expr.trim();

    // Ordered by length (longest first) so `>=` is matched before `>`.
    const OPS: &[&str] = &[">=", "<=", "!=", "==", ">", "<", "contains"];

    for &op in OPS {
        if let Some(op_pos) = find_op(expr, op) {
            let field = expr[..op_pos].trim();
            let rhs = expr[op_pos + op.len()..].trim();

            let lhs_raw = match ctx.previous_output.get(field) {
                Some(v) => v.as_str(),
                None => return false,
            };

            return compare_str(lhs_raw, op, rhs);
        }
    }

    // No operator found — treat as boolean field test.
    ctx.previous_output
        .get(expr)
        .map_or(false, |v| is_truthy(v))
}

/// Find the byte offset of `op` in `expr`, skipping occurrences inside longer
/// operators (e.g. avoid matching `>` inside `>=`).
fn find_op(expr: &str, op: &str) -> Option<usize> {
    // We search left-to-right and ensure the character immediately following
    // the found position is not part of a longer operator.
    let bytes = expr.as_bytes();
    let op_bytes = op.as_bytes();
    let len = op_bytes.len();

    let mut i = 0usize;
    while i + len <= bytes.len() {
        if &bytes[i..i + len] == op_bytes {
            // For single-char ops like `>` or `<`, ensure the next char is
            // not `=` (which would make it `>=` / `<=`).
            let next_ok = if len == 1 {
                bytes.get(i + 1) != Some(&b'=')
            } else {
                true
            };
            if next_ok {
                return Some(i);
            }
        }
        i += 1;
    }
    None
}

/// Compare `lhs` (raw string from output) to `rhs` (literal from expression)
/// using operator `op`.
fn compare_str(lhs: &str, op: &str, rhs: &str) -> bool {
    // Attempt numeric comparison first.
    if let (Ok(l), Ok(r)) = (lhs.parse::<f64>(), rhs.parse::<f64>()) {
        return match op {
            "==" => (l - r).abs() < f64::EPSILON,
            "!=" => (l - r).abs() >= f64::EPSILON,
            ">" => l > r,
            "<" => l < r,
            ">=" => l >= r,
            "<=" => l <= r,
            "contains" => lhs.contains(rhs),
            _ => false,
        };
    }

    // String comparison.
    match op {
        "==" => lhs == rhs,
        "!=" => lhs != rhs,
        "contains" => lhs.contains(rhs),
        // Lexicographic ordering for non-numeric strings.
        ">" => lhs > rhs,
        "<" => lhs < rhs,
        ">=" => lhs >= rhs,
        "<=" => lhs <= rhs,
        _ => false,
    }
}

/// Interpret a string value as boolean.
fn is_truthy(v: &str) -> bool {
    matches!(v.to_lowercase().as_str(), "true" | "yes" | "1")
}

// ── StepCondition::parse ─────────────────────────────────────────────────────

impl StepCondition {
    /// Parse a [`StepCondition`] from its string representation.
    ///
    /// Supported forms:
    /// - `"always"` → [`StepCondition::Always`]
    /// - `"on_success"` / `"on_previous_success"` → [`StepCondition::OnPreviousSuccess`]
    /// - `"on_failure"` / `"on_previous_failure"` → [`StepCondition::OnPreviousFailure`]
    /// - `"expr: <expression>"` → [`StepCondition::Expression`]
    /// - `"field_equals: <field>=<value>"` → [`StepCondition::FieldEquals`]
    /// - `"field_contains: <field>=<substring>"` → [`StepCondition::FieldContains`]
    ///
    /// # Errors
    ///
    /// Returns an error string for unrecognised or malformed input.
    pub fn parse(s: &str) -> Result<Self, String> {
        let s = s.trim();

        if s.eq_ignore_ascii_case("always") {
            return Ok(Self::Always);
        }
        if s.eq_ignore_ascii_case("on_success") || s.eq_ignore_ascii_case("on_previous_success") {
            return Ok(Self::OnPreviousSuccess);
        }
        if s.eq_ignore_ascii_case("on_failure") || s.eq_ignore_ascii_case("on_previous_failure") {
            return Ok(Self::OnPreviousFailure);
        }

        if let Some(rest) = s.strip_prefix("expr:") {
            return Ok(Self::Expression(rest.trim().to_string()));
        }

        if let Some(rest) = s.strip_prefix("field_equals:") {
            return parse_field_value_pair(rest.trim())
                .map(|(f, v)| Self::FieldEquals { field: f, value: v });
        }

        if let Some(rest) = s.strip_prefix("field_contains:") {
            return parse_field_value_pair(rest.trim()).map(|(f, v)| Self::FieldContains {
                field: f,
                substring: v,
            });
        }

        Err(format!("Unrecognised condition string: '{s}'"))
    }
}

/// Split `"field=value"` into `(field, value)`.
fn parse_field_value_pair(s: &str) -> Result<(String, String), String> {
    if let Some(eq_pos) = s.find('=') {
        let field = s[..eq_pos].trim().to_string();
        let value = s[eq_pos + 1..].trim().to_string();
        if field.is_empty() {
            return Err("Field name must not be empty".to_string());
        }
        Ok((field, value))
    } else {
        Err(format!("Expected 'field=value' in: '{s}'"))
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    fn ctx_success() -> StepContext {
        StepContext::new("step-a")
            .with_success(true)
            .with_output("size_mb", "250")
            .with_output("status", "ok")
            .with_output("codec", "h264")
    }

    fn ctx_failure() -> StepContext {
        StepContext::new("step-b").with_success(false)
    }

    // ── Basic variants ────────────────────────────────────────────────────────

    #[test]
    fn test_always_is_true() {
        let ctx = ctx_failure();
        assert!(ConditionEvaluator::evaluate(&StepCondition::Always, &ctx));
    }

    #[test]
    fn test_on_previous_success_true_when_succeeded() {
        assert!(ConditionEvaluator::evaluate(
            &StepCondition::OnPreviousSuccess,
            &ctx_success()
        ));
    }

    #[test]
    fn test_on_previous_success_false_when_failed() {
        assert!(!ConditionEvaluator::evaluate(
            &StepCondition::OnPreviousSuccess,
            &ctx_failure()
        ));
    }

    #[test]
    fn test_on_previous_failure_true_when_failed() {
        assert!(ConditionEvaluator::evaluate(
            &StepCondition::OnPreviousFailure,
            &ctx_failure()
        ));
    }

    #[test]
    fn test_on_previous_failure_false_when_succeeded() {
        assert!(!ConditionEvaluator::evaluate(
            &StepCondition::OnPreviousFailure,
            &ctx_success()
        ));
    }

    // ── FieldEquals / FieldContains ───────────────────────────────────────────

    #[test]
    fn test_field_equals_match() {
        let cond = StepCondition::FieldEquals {
            field: "status".into(),
            value: "ok".into(),
        };
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_field_equals_no_match() {
        let cond = StepCondition::FieldEquals {
            field: "status".into(),
            value: "fail".into(),
        };
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_field_equals_missing_field() {
        let cond = StepCondition::FieldEquals {
            field: "nonexistent".into(),
            value: "x".into(),
        };
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_field_contains_match() {
        let cond = StepCondition::FieldContains {
            field: "codec".into(),
            substring: "264".into(),
        };
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_field_contains_no_match() {
        let cond = StepCondition::FieldContains {
            field: "codec".into(),
            substring: "vp9".into(),
        };
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    // ── Expression evaluation ─────────────────────────────────────────────────

    #[test]
    fn test_expression_gt_numeric() {
        let cond = StepCondition::Expression("size_mb > 100".into());
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_expression_lte_numeric_false() {
        let cond = StepCondition::Expression("size_mb <= 100".into());
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_expression_eq_string() {
        let cond = StepCondition::Expression("status == ok".into());
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_expression_neq_string() {
        let cond = StepCondition::Expression("status != fail".into());
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_expression_contains_op() {
        let cond = StepCondition::Expression("codec contains 264".into());
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_expression_missing_field_returns_false() {
        let cond = StepCondition::Expression("no_such_field > 0".into());
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    // ── And / Or / Not ────────────────────────────────────────────────────────

    #[test]
    fn test_and_all_true() {
        let cond = StepCondition::And(vec![
            StepCondition::Always,
            StepCondition::OnPreviousSuccess,
        ]);
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_and_short_circuits_on_false() {
        let cond = StepCondition::And(vec![
            StepCondition::OnPreviousFailure, // false for success ctx
            StepCondition::Always,
        ]);
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_or_true_when_any_true() {
        let cond = StepCondition::Or(vec![
            StepCondition::OnPreviousFailure, // false
            StepCondition::OnPreviousSuccess, // true
        ]);
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_or_false_when_all_false() {
        let cond = StepCondition::Or(vec![
            StepCondition::OnPreviousFailure,
            StepCondition::OnPreviousFailure,
        ]);
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    #[test]
    fn test_not_negates() {
        let cond = StepCondition::Not(Box::new(StepCondition::OnPreviousSuccess));
        assert!(!ConditionEvaluator::evaluate(&cond, &ctx_success()));
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_failure()));
    }

    #[test]
    fn test_nested_and_or_not() {
        // (size_mb > 100 AND status == ok) OR NOT(on_failure)
        let cond = StepCondition::Or(vec![
            StepCondition::And(vec![
                StepCondition::Expression("size_mb > 100".into()),
                StepCondition::FieldEquals {
                    field: "status".into(),
                    value: "ok".into(),
                },
            ]),
            StepCondition::Not(Box::new(StepCondition::OnPreviousFailure)),
        ]);
        assert!(ConditionEvaluator::evaluate(&cond, &ctx_success()));
    }

    // ── StepCondition::parse ──────────────────────────────────────────────────

    #[test]
    fn test_parse_always() {
        let c = StepCondition::parse("always").expect("parse should succeed");
        assert!(matches!(c, StepCondition::Always));
    }

    #[test]
    fn test_parse_on_success_short_form() {
        let c = StepCondition::parse("on_success").expect("parse should succeed");
        assert!(matches!(c, StepCondition::OnPreviousSuccess));
    }

    #[test]
    fn test_parse_on_failure_long_form() {
        let c = StepCondition::parse("on_previous_failure").expect("parse should succeed");
        assert!(matches!(c, StepCondition::OnPreviousFailure));
    }

    #[test]
    fn test_parse_expr() {
        let c = StepCondition::parse("expr: size_mb > 100").expect("parse should succeed");
        assert!(matches!(c, StepCondition::Expression(_)));
    }

    #[test]
    fn test_parse_field_equals() {
        let c = StepCondition::parse("field_equals: status=ok").expect("parse should succeed");
        if let StepCondition::FieldEquals { field, value } = c {
            assert_eq!(field, "status");
            assert_eq!(value, "ok");
        } else {
            panic!("expected FieldEquals");
        }
    }

    #[test]
    fn test_parse_field_contains() {
        let c = StepCondition::parse("field_contains: codec=264").expect("parse should succeed");
        if let StepCondition::FieldContains { field, substring } = c {
            assert_eq!(field, "codec");
            assert_eq!(substring, "264");
        } else {
            panic!("expected FieldContains");
        }
    }

    #[test]
    fn test_parse_unknown_returns_err() {
        let r = StepCondition::parse("nonsense_condition");
        assert!(r.is_err());
    }
}