Skip to main content

substitute_h

Function substitute_h 

Source
pub fn substitute_h(expr: &str, seed: &str) -> String
Expand description

Resolve Jenkins-style H tokens in a 5-field cron expression to concrete, deterministic values derived from seed. Each field’s H maps into that field’s natural range (minute 0–59, hour 0–23, day-of-month 1–28, month 1–12, day-of-week 0–6) using a distinct mix of the seed hash, so two Hs in the same expression don’t collapse to the same number.

Returns the rewritten expression. Non-H fields pass through unchanged. If the expression isn’t 5 whitespace-separated fields it is returned unchanged (shape validation is crate::validate::validate_cron’s job).

use kopiur_api::substitute_h;

// Each `H` resolves to a concrete, deterministic value in the field's range;
// non-`H` fields are untouched.
let out = substitute_h("H 2 * * *", "schedule-uid");
let fields: Vec<&str> = out.split_whitespace().collect();
let minute: u64 = fields[0].parse().expect("H -> a minute");
assert!(minute < 60);
assert_eq!(&fields[1..], &["2", "*", "*", "*"]);

// Deterministic per seed; different schedules land in different minutes.
assert_eq!(substitute_h("H 2 * * *", "uid"), substitute_h("H 2 * * *", "uid"));
assert_ne!(substitute_h("H 2 * * *", "uid-a"), substitute_h("H 2 * * *", "uid-b"));