dre-core 0.2.2

DRE core engine: project parsing, validation and the run path
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
//! Schedule timings: their shape, and the checks shared by `dre validate` and
//! `dre schedule ls`. Core never fires schedules; `occurrences` works out when they would.

use chrono::NaiveTime;
use serde_json::{Map as JsonMap, Value as Json};
use serde_yaml_ng::{Mapping, Value};

pub const SCHEDULE_KEYS: &[&str] = &["cron", "every", "rrule", "starting", "at", "except", "also"];
/// Keys of a timings.yml entry: a timing and its timezone.
pub const TIMING_KEYS: &[&str] = &[
    "cron", "every", "rrule", "starting", "at", "except", "also", "timezone",
];

/// Validate one schedule block (`{cron}`, `{every, starting, at}` or `{rrule, starting, at}`,
/// each optionally with `except` and `also`).
/// Returns human-readable problems; empty means valid. Keys outside `SCHEDULE_KEYS` are the
/// caller's business (schedules.yml entries carry `select`/`report`/`set` next to them).
/// `what` names the block in messages and `forms` its alternatives: "a schedule needs exactly
/// one of `cron`, `every` or `rrule`".
pub fn validate_block(m: &Mapping, what: &str, forms: &str) -> Vec<String> {
    let mut errs = Vec::new();
    let has = |k: &str| m.contains_key(k);
    let found: Vec<&str> = ["cron", "every", "rrule"]
        .into_iter()
        .filter(|k| has(k))
        .collect();
    match found.len() {
        0 => errs.push(format!("{what} needs exactly one of {forms}")),
        1 => {}
        _ => errs.push(format!(
            "{what} needs exactly one of {forms}, found {}",
            found
                .iter()
                .map(|f| format!("`{f}`"))
                .collect::<Vec<_>>()
                .join(" and ")
        )),
    }
    if let Some(v) = m.get("cron") {
        match v.as_str() {
            Some(s) => errs.extend(validate_cron(s).err()),
            None => errs.push("`cron` must be a string".into()),
        }
    }
    if let Some(v) = m.get("rrule") {
        match v.as_str() {
            Some(s) => errs.extend(validate_rrule(s).err()),
            None => errs.push("`rrule` must be a string".into()),
        }
    }
    if let Some(v) = m.get("every") {
        errs.extend(validate_every(v).err());
    }
    for key in ["starting", "at"] {
        if has(key) && has("cron") {
            errs.push(format!(
                "`{key}` doesn't apply to a `cron` schedule; the expression sets its days and time"
            ));
        }
    }
    for key in ["except", "also"] {
        let Some(v) = m.get(key) else { continue };
        let ok = v.as_sequence().is_some_and(|l| {
            l.iter().all(|d| {
                d.as_str()
                    .is_some_and(|d| chrono::NaiveDate::parse_from_str(d, "%Y-%m-%d").is_ok())
            })
        });
        if !ok {
            errs.push(format!("`{key}` must be a list of dates in YYYY-MM-DD form"));
        }
    }
    if has("also") && errs.is_empty() && time_of_day(&crate::project::yaml_map_to_json(m)).is_none() {
        errs.push(
            "`also` needs a schedule that fires at one time of day (e.g. `0 6 * * *` or `at: \"06:00\"`), so the added dates fire then"
                .into(),
        );
    }
    if let Some(v) = m.get("starting") {
        let ok = v
            .as_str()
            .is_some_and(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").is_ok());
        if !ok {
            errs.push("`starting` must be a date in YYYY-MM-DD form".into());
        }
    }
    if let Some(v) = m.get("at") {
        let ok = v
            .as_str()
            .is_some_and(|s| s.len() == 5 && chrono::NaiveTime::parse_from_str(s, "%H:%M").is_ok());
        if !ok {
            errs.push("`at` must be a 24-hour time in HH:MM form".into());
        }
    }
    errs
}

fn validate_every(v: &Value) -> Result<(), String> {
    let Some(m) = v.as_mapping() else {
        return Err("`every` must be a map with one unit, e.g. `{days: 5}`".into());
    };
    if m.len() != 1 {
        return Err("`every` must have exactly one unit: `days`, `weeks` or `months`".into());
    }
    let (k, n) = m.iter().next().unwrap();
    let unit = k.as_str().unwrap_or("");
    if !matches!(unit, "days" | "weeks" | "months") {
        return Err(format!(
            "unknown `every` unit `{unit}`: use `days`, `weeks` or `months`"
        ));
    }
    match n.as_u64() {
        Some(n) if n > 0 => Ok(()),
        _ => Err(format!("`every.{unit}` must be a positive whole number")),
    }
}

const CRON_MACROS: &[&str] = &[
    "@yearly",
    "@annually",
    "@monthly",
    "@weekly",
    "@daily",
    "@midnight",
    "@hourly",
];

pub fn validate_cron(expr: &str) -> Result<(), String> {
    let expr = expr.trim();
    if expr.starts_with('@') {
        return if CRON_MACROS.contains(&expr) {
            Ok(())
        } else {
            Err(format!("invalid cron `{expr}`: unknown macro"))
        };
    }
    let fields: Vec<&str> = expr.split_whitespace().collect();
    if fields.len() != 5 {
        return Err(format!(
            "invalid cron `{expr}`: expected 5 fields (minute hour day-of-month month day-of-week), found {}",
            fields.len()
        ));
    }
    const MONTHS: &[&str] = &[
        "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec",
    ];
    const DAYS: &[&str] = &["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
    let specs: [(&str, u32, u32, &[&str], u32); 5] = [
        ("minute", 0, 59, &[], 0),
        ("hour", 0, 23, &[], 0),
        ("day-of-month", 1, 31, &[], 0),
        ("month", 1, 12, MONTHS, 1),
        ("day-of-week", 0, 7, DAYS, 0),
    ];
    for (field, (name, lo, hi, names, base)) in fields.iter().zip(specs) {
        cron_field(field, lo, hi, names, base)
            .map_err(|e| format!("invalid cron `{expr}`: {name} field `{field}` {e}"))?;
    }
    Ok(())
}

fn cron_field(field: &str, lo: u32, hi: u32, names: &[&str], base: u32) -> Result<(), String> {
    let value = |s: &str| -> Result<u32, String> {
        if let Some(i) = names.iter().position(|n| n.eq_ignore_ascii_case(s)) {
            return Ok(i as u32 + base);
        }
        let n: u32 = s.parse().map_err(|_| format!("has an invalid value `{s}`"))?;
        if n < lo || n > hi {
            return Err(format!("has `{n}` outside {lo}-{hi}"));
        }
        Ok(n)
    };
    for part in field.split(',') {
        let (range, step) = match part.split_once('/') {
            Some((r, s)) => (r, Some(s)),
            None => (part, None),
        };
        if let Some(s) = step {
            match s.parse::<u32>() {
                Ok(n) if n > 0 => {}
                _ => return Err(format!("has an invalid step `{s}`")),
            }
        }
        if range == "*" {
            continue;
        }
        match range.split_once('-') {
            Some((a, b)) => {
                let (a, b) = (value(a)?, value(b)?);
                if a > b {
                    return Err(format!("has a backwards range `{range}`"));
                }
            }
            None => {
                value(range)?;
            }
        }
    }
    Ok(())
}

/// RFC 5545 RRULE value validation (the rule itself, optionally prefixed `RRULE:`).
pub fn validate_rrule(expr: &str) -> Result<(), String> {
    let body = expr.trim();
    let body = body.strip_prefix("RRULE:").unwrap_or(body);
    let err = |m: String| Err(format!("invalid rrule `{expr}`: {m}"));
    let mut seen = std::collections::BTreeSet::new();
    let mut freq = false;
    for part in body.split(';').filter(|p| !p.is_empty()) {
        let Some((k, v)) = part.split_once('=') else {
            return err(format!("`{part}` is not KEY=VALUE"));
        };
        let k = k.to_ascii_uppercase();
        if !seen.insert(k.clone()) {
            return err(format!("`{k}` appears more than once"));
        }
        let list = |lo: i64, hi: i64, signed: bool| -> Result<(), String> {
            for x in v.split(',') {
                let n: i64 = x
                    .parse()
                    .map_err(|_| format!("`{k}` has an invalid value `{x}`"))?;
                let ok = if signed {
                    n != 0 && n.abs() >= lo && n.abs() <= hi
                } else {
                    n >= lo && n <= hi
                };
                if !ok {
                    return Err(format!("`{k}` value `{x}` is out of range"));
                }
            }
            Ok(())
        };
        let r = match k.as_str() {
            "FREQ" => {
                freq = true;
                if [
                    "SECONDLY", "MINUTELY", "HOURLY", "DAILY", "WEEKLY", "MONTHLY", "YEARLY",
                ]
                .contains(&v.to_ascii_uppercase().as_str())
                {
                    Ok(())
                } else {
                    Err(format!("unknown FREQ `{v}`"))
                }
            }
            "INTERVAL" | "COUNT" => match v.parse::<u64>() {
                Ok(n) if n > 0 => Ok(()),
                _ => Err(format!("`{k}` must be a positive whole number")),
            },
            "UNTIL" => {
                let ok = chrono::NaiveDate::parse_from_str(v, "%Y%m%d").is_ok()
                    || chrono::NaiveDateTime::parse_from_str(v.trim_end_matches('Z'), "%Y%m%dT%H%M%S")
                        .is_ok();
                if ok {
                    Ok(())
                } else {
                    Err(format!("`UNTIL` `{v}` is not a DATE or DATE-TIME"))
                }
            }
            "BYSECOND" => list(0, 60, false),
            "BYMINUTE" => list(0, 59, false),
            "BYHOUR" => list(0, 23, false),
            "BYMONTHDAY" => list(1, 31, true),
            "BYYEARDAY" => list(1, 366, true),
            "BYWEEKNO" => list(1, 53, true),
            "BYMONTH" => list(1, 12, false),
            "BYSETPOS" => list(1, 366, true),
            "BYDAY" => v
                .split(',')
                .try_for_each(|d| byday(d).ok_or(format!("`BYDAY` has an invalid day `{d}`"))),
            "WKST" => weekday(v)
                .then_some(())
                .ok_or(format!("`WKST` has an invalid day `{v}`")),
            _ => Err(format!("unknown rule part `{k}`")),
        };
        if let Err(m) = r {
            return err(m);
        }
    }
    if !freq {
        return err("`FREQ` is required".into());
    }
    if seen.contains("UNTIL") && seen.contains("COUNT") {
        return err("`UNTIL` and `COUNT` can't both be set".into());
    }
    Ok(())
}

fn weekday(d: &str) -> bool {
    ["MO", "TU", "WE", "TH", "FR", "SA", "SU"].contains(&d.to_ascii_uppercase().as_str())
}

fn byday(d: &str) -> Option<()> {
    let (num, day) = d.split_at(d.len().checked_sub(2)?);
    if !weekday(day) {
        return None;
    }
    if num.is_empty() {
        return Some(());
    }
    let n: i64 = num.trim_start_matches('+').parse().ok()?;
    (n != 0 && n.abs() <= 53).then_some(())
}

/// A rule's parts as `(KEY, VALUE)`, upper-cased, in the order written.
pub fn rule_parts(expr: &str) -> Result<Vec<(String, String)>, String> {
    validate_rrule(expr)?;
    let body = expr.trim();
    let body = body.strip_prefix("RRULE:").unwrap_or(body);
    Ok(body
        .split(';')
        .filter(|p| !p.is_empty())
        .filter_map(|p| p.split_once('='))
        .map(|(k, v)| (k.to_ascii_uppercase(), v.to_ascii_uppercase()))
        .collect())
}

fn part<'a>(parts: &'a [(String, String)], key: &str) -> Option<&'a str> {
    parts.iter().find(|(k, _)| k == key).map(|(_, v)| v.as_str())
}

/// The one time of day a timing fires at, if it has one: `at`, a cron expression with a single
/// minute and hour, or a rule's single `BYHOUR`/`BYMINUTE` (00:00 when nothing sets it).
pub fn time_of_day(timing: &JsonMap<String, Json>) -> Option<NaiveTime> {
    let s = |k: &str| timing.get(k).and_then(Json::as_str);
    let at = s("at").and_then(|t| NaiveTime::parse_from_str(t, "%H:%M").ok());
    let one = |v: Option<&str>, default: u32| -> Option<u32> {
        match v {
            None => Some(default),
            Some(v) => v.parse().ok(),
        }
    };
    if let Some(expr) = s("cron") {
        let expr = match expr.trim() {
            "@yearly" | "@annually" | "@monthly" | "@weekly" | "@daily" | "@midnight" => "0 0 * * *",
            e => e,
        };
        let f: Vec<&str> = expr.split_whitespace().collect();
        let (m, h) = (f.first()?.parse().ok()?, f.get(1)?.parse().ok()?);
        return NaiveTime::from_hms_opt(h, m, 0);
    }
    if let Some(rule) = s("rrule") {
        let parts = rule_parts(rule).ok()?;
        let (h, m) = (part(&parts, "BYHOUR"), part(&parts, "BYMINUTE"));
        if matches!(part(&parts, "FREQ"), Some("HOURLY" | "MINUTELY" | "SECONDLY")) && h.is_none() {
            return None;
        }
        if h.is_some() || m.is_some() {
            return NaiveTime::from_hms_opt(one(h, 0)?, one(m, 0)?, 0);
        }
        return Some(at.unwrap_or(NaiveTime::MIN));
    }
    Some(at.unwrap_or(NaiveTime::MIN))
}

/// Problems that are warnings on 0.1.x and errors from 0.2.0: a timing whose occurrences would
/// depend on when you look (no anchor), or one finer than a minute. `dre schedule ls` lists such
/// a schedule under `problems`, without occurrences. Each is `(code, message)`.
pub fn strictness(timing: &JsonMap<String, Json>) -> Vec<(&'static str, String)> {
    let mut out = Vec::new();
    let anchored = timing.contains_key("starting");
    if timing.contains_key("every") && !anchored {
        out.push((
            "schedule-needs-anchor",
            "`every` needs `starting` (its first date): it counts from it, so its occurrences would depend on when you look".to_string(),
        ));
    }
    let Some(parts) = timing
        .get("rrule")
        .and_then(Json::as_str)
        .and_then(|r| rule_parts(r).ok())
    else {
        return out;
    };
    let freq = part(&parts, "FREQ").unwrap_or("");
    if matches!(freq, "SECONDLY" | "MINUTELY") {
        out.push((
            "schedule-too-frequent",
            format!("`FREQ={freq}` is finer than DRE schedules go: use `FREQ=HOURLY` with `BYMINUTE`, or a cron expression"),
        ));
    }
    if part(&parts, "BYSECOND").is_some() {
        out.push((
            "schedule-seconds",
            "`BYSECOND` isn't supported: schedules fire on whole minutes".to_string(),
        ));
    }
    if !anchored {
        let interval = part(&parts, "INTERVAL")
            .and_then(|v| v.parse::<u64>().ok())
            .unwrap_or(1);
        let day_from_start = matches!(freq, "WEEKLY" | "MONTHLY" | "YEARLY")
            && ["BYDAY", "BYMONTHDAY", "BYYEARDAY", "BYWEEKNO"]
                .iter()
                .all(|k| part(&parts, k).is_none());
        let why = if interval > 1 {
            Some("`INTERVAL` counts from it")
        } else if part(&parts, "COUNT").is_some() {
            Some("`COUNT` counts from it")
        } else if day_from_start {
            Some("the rule takes its day from it")
        } else {
            None
        };
        if let Some(why) = why {
            out.push((
                "schedule-needs-anchor",
                format!("this rule needs `starting` (its first date): {why}, so its occurrences would depend on when you look"),
            ));
        }
    }
    out
}

/// A warning when nothing gives a timing a time of day, so it fires at midnight.
pub fn no_time(timing: &JsonMap<String, Json>) -> Option<String> {
    if timing.contains_key("at") || timing.contains_key("cron") {
        return None;
    }
    if let Some(parts) = timing
        .get("rrule")
        .and_then(Json::as_str)
        .and_then(|r| rule_parts(r).ok())
    {
        let daily_or_coarser = matches!(
            part(&parts, "FREQ"),
            Some("DAILY" | "WEEKLY" | "MONTHLY" | "YEARLY")
        );
        if !daily_or_coarser || part(&parts, "BYHOUR").is_some() {
            return None;
        }
    }
    Some(
        "no time of day is given, so it fires at 00:00 in its timezone; set `at: \"HH:MM\"` to say when"
            .into(),
    )
}