hron 1.0.0

Human-readable cron — scheduling expressions that are a superset of what cron can express
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
use crate::ast::*;
use crate::error::ScheduleError;

/// Convert a Schedule to a 5-field cron expression (minute hour dom month dow).
pub fn to_cron(schedule: &Schedule) -> Result<String, ScheduleError> {
    if !schedule.except.is_empty() {
        return Err(ScheduleError::cron(
            "not expressible as cron (except clauses not supported)",
        ));
    }
    if schedule.until.is_some() {
        return Err(ScheduleError::cron(
            "not expressible as cron (until clauses not supported)",
        ));
    }
    if !schedule.during.is_empty() {
        return Err(ScheduleError::cron(
            "not expressible as cron (during clauses not supported)",
        ));
    }
    match &schedule.expr {
        ScheduleExpr::DayRepeat {
            interval,
            days,
            times,
        } => {
            if *interval > 1 {
                return Err(ScheduleError::cron(
                    "not expressible as cron (multi-day intervals not supported)",
                ));
            }
            if times.len() != 1 {
                return Err(ScheduleError::cron(
                    "not expressible as cron (multiple times not supported)",
                ));
            }
            let time = &times[0];
            let dow = day_filter_to_cron_dow(days)?;
            Ok(format!("{} {} * * {}", time.minute, time.hour, dow))
        }

        ScheduleExpr::IntervalRepeat {
            interval,
            unit,
            from,
            to,
            day_filter,
        } => {
            // Only expressible if window is full day (00:00 to 23:59)
            let full_day = from.hour == 0 && from.minute == 0 && to.hour == 23 && to.minute == 59;
            if !full_day {
                return Err(ScheduleError::cron(
                    "not expressible as cron (partial-day interval windows not supported)",
                ));
            }
            if day_filter.is_some() {
                return Err(ScheduleError::cron(
                    "not expressible as cron (interval with day filter not supported)",
                ));
            }

            match unit {
                IntervalUnit::Minutes => {
                    if 60 % interval != 0 {
                        return Err(ScheduleError::cron(format!(
                            "not expressible as cron (*/{interval} breaks at hour boundaries)"
                        )));
                    }
                    Ok(format!("*/{interval} * * * *"))
                }
                IntervalUnit::Hours => Ok(format!("0 */{interval} * * *")),
            }
        }

        ScheduleExpr::WeekRepeat { .. } => Err(ScheduleError::cron(
            "not expressible as cron (multi-week intervals not supported)",
        )),

        ScheduleExpr::MonthRepeat {
            interval,
            target,
            times,
        } => {
            if *interval > 1 {
                return Err(ScheduleError::cron(
                    "not expressible as cron (multi-month intervals not supported)",
                ));
            }
            if times.len() != 1 {
                return Err(ScheduleError::cron(
                    "not expressible as cron (multiple times not supported)",
                ));
            }
            let time = &times[0];
            match target {
                MonthTarget::Days(_) => {
                    let expanded = target.expand_days();
                    let dom = expanded
                        .iter()
                        .map(|d| d.to_string())
                        .collect::<Vec<_>>()
                        .join(",");
                    Ok(format!("{} {} {} * *", time.minute, time.hour, dom))
                }
                MonthTarget::LastDay => Err(ScheduleError::cron(
                    "not expressible as cron (last day of month not supported)",
                )),
                MonthTarget::LastWeekday => Err(ScheduleError::cron(
                    "not expressible as cron (last weekday of month not supported)",
                )),
                MonthTarget::NearestWeekday { day, direction } => {
                    if direction.is_some() {
                        return Err(ScheduleError::cron(
                            "not expressible as cron (directional nearest weekday not supported)",
                        ));
                    }
                    Ok(format!("{} {} {}W * *", time.minute, time.hour, day))
                }
                MonthTarget::OrdinalWeekday { .. } => Err(ScheduleError::cron(
                    "not expressible as cron (ordinal weekday of month not supported)",
                )),
            }
        }

        ScheduleExpr::SingleDate { .. } => Err(ScheduleError::cron(
            "not expressible as cron (single dates are not repeating)",
        )),

        ScheduleExpr::YearRepeat { .. } => Err(ScheduleError::cron(
            "not expressible as cron (yearly schedules not supported in 5-field cron)",
        )),
    }
}

fn day_filter_to_cron_dow(filter: &DayFilter) -> Result<String, ScheduleError> {
    match filter {
        DayFilter::Every => Ok("*".to_string()),
        DayFilter::Weekday => Ok("1-5".to_string()),
        DayFilter::Weekend => Ok("0,6".to_string()),
        DayFilter::Days(days) => {
            let mut nums: Vec<u8> = days.iter().map(|d| cron_dow_number(*d)).collect();
            nums.sort();
            Ok(nums
                .iter()
                .map(|n| n.to_string())
                .collect::<Vec<_>>()
                .join(","))
        }
    }
}

/// Cron uses 0=Sunday, 1=Monday, ..., 6=Saturday.
fn cron_dow_number(day: Weekday) -> u8 {
    match day {
        Weekday::Sunday => 0,
        Weekday::Monday => 1,
        Weekday::Tuesday => 2,
        Weekday::Wednesday => 3,
        Weekday::Thursday => 4,
        Weekday::Friday => 5,
        Weekday::Saturday => 6,
    }
}

// ============================================================================
// from_cron: Parse 5-field cron expressions (and @ shortcuts)
// ============================================================================

/// Parse a 5-field cron expression into a Schedule.
pub fn from_cron(cron: &str) -> Result<Schedule, ScheduleError> {
    let cron = cron.trim();

    // Handle @ shortcuts first
    if cron.starts_with('@') {
        return parse_cron_shortcut(cron);
    }

    let fields: Vec<&str> = cron.split_whitespace().collect();
    if fields.len() != 5 {
        return Err(ScheduleError::cron(format!(
            "expected 5 cron fields, got {}",
            fields.len()
        )));
    }

    let minute_field = fields[0];
    let hour_field = fields[1];
    let dom_field = fields[2];
    let month_field = fields[3];
    let dow_field = fields[4];

    // Normalize ? to * (they're semantically equivalent for our purposes)
    let dom_field = if dom_field == "?" { "*" } else { dom_field };
    let dow_field = if dow_field == "?" { "*" } else { dow_field };

    // Parse month field into during clause
    let during = parse_month_field(month_field)?;

    // Check for special DOW patterns: nth weekday (#), last weekday (5L)
    if let Some(schedule) =
        try_parse_nth_weekday(minute_field, hour_field, dom_field, dow_field, &during)?
    {
        return Ok(schedule);
    }

    // Check for L (last day) or LW (last weekday) in DOM
    if let Some(schedule) =
        try_parse_last_day(minute_field, hour_field, dom_field, dow_field, &during)?
    {
        return Ok(schedule);
    }

    // Check for W (nearest weekday): e.g., 15W
    if dom_field.ends_with('W') && dom_field != "LW" {
        if let Some(schedule) =
            try_parse_nearest_weekday(minute_field, hour_field, dom_field, dow_field, &during)?
        {
            return Ok(schedule);
        }
    }

    // Check for interval patterns: */N or range/N
    if let Some(schedule) =
        try_parse_interval(minute_field, hour_field, dom_field, dow_field, &during)?
    {
        return Ok(schedule);
    }

    // Standard time-based cron
    let minute: u8 = parse_single_value(minute_field, "minute", 0, 59)?;
    let hour: u8 = parse_single_value(hour_field, "hour", 0, 23)?;
    let time = TimeOfDay { hour, minute };

    // DOM-based (monthly) - when DOM is specified and DOW is *
    if dom_field != "*" && dow_field == "*" {
        let target = parse_dom_field(dom_field)?;
        let mut schedule = Schedule::new(ScheduleExpr::MonthRepeat {
            interval: 1,
            target,
            times: vec![time],
        });
        schedule.during = during;
        return Ok(schedule);
    }

    // DOW-based (day repeat)
    let days = parse_cron_dow(dow_field)?;
    let mut schedule = Schedule::new(ScheduleExpr::DayRepeat {
        interval: 1,
        days,
        times: vec![time],
    });
    schedule.during = during;
    Ok(schedule)
}

/// Parse @ shortcuts like @daily, @hourly, etc.
fn parse_cron_shortcut(cron: &str) -> Result<Schedule, ScheduleError> {
    match cron.to_lowercase().as_str() {
        "@yearly" | "@annually" => Ok(Schedule::new(ScheduleExpr::YearRepeat {
            interval: 1,
            target: YearTarget::Date {
                month: MonthName::January,
                day: 1,
            },
            times: vec![TimeOfDay { hour: 0, minute: 0 }],
        })),
        "@monthly" => Ok(Schedule::new(ScheduleExpr::MonthRepeat {
            interval: 1,
            target: MonthTarget::Days(vec![DayOfMonthSpec::Single(1)]),
            times: vec![TimeOfDay { hour: 0, minute: 0 }],
        })),
        "@weekly" => Ok(Schedule::new(ScheduleExpr::DayRepeat {
            interval: 1,
            days: DayFilter::Days(vec![Weekday::Sunday]),
            times: vec![TimeOfDay { hour: 0, minute: 0 }],
        })),
        "@daily" | "@midnight" => Ok(Schedule::new(ScheduleExpr::DayRepeat {
            interval: 1,
            days: DayFilter::Every,
            times: vec![TimeOfDay { hour: 0, minute: 0 }],
        })),
        "@hourly" => Ok(Schedule::new(ScheduleExpr::IntervalRepeat {
            interval: 1,
            unit: IntervalUnit::Hours,
            from: TimeOfDay { hour: 0, minute: 0 },
            to: TimeOfDay {
                hour: 23,
                minute: 59,
            },
            day_filter: None,
        })),
        _ => Err(ScheduleError::cron(format!("unknown @ shortcut: {cron}"))),
    }
}

/// Parse month field into a Vec<MonthName> for the `during` clause.
fn parse_month_field(field: &str) -> Result<Vec<MonthName>, ScheduleError> {
    if field == "*" {
        return Ok(vec![]);
    }

    let mut months = Vec::new();
    for part in field.split(',') {
        // Check for step values FIRST (e.g., 1-12/3 or */3)
        if let Some((range, step)) = part.split_once('/') {
            let (start, end) = if range == "*" {
                (1u8, 12u8)
            } else if let Some((s, e)) = range.split_once('-') {
                let start_month = parse_month_value(s)?;
                let end_month = parse_month_value(e)?;
                (start_month.number(), end_month.number())
            } else {
                return Err(ScheduleError::cron(format!(
                    "invalid month step expression: {}",
                    part
                )));
            };
            let step: u8 = step
                .parse()
                .map_err(|_| ScheduleError::cron(format!("invalid month step value: {}", step)))?;
            if step == 0 {
                return Err(ScheduleError::cron("step cannot be 0"));
            }
            let mut n = start;
            while n <= end {
                months.push(month_from_number(n)?);
                n += step;
            }
        } else if let Some((start, end)) = part.split_once('-') {
            // Range like 1-3 or JAN-MAR
            let start_month = parse_month_value(start)?;
            let end_month = parse_month_value(end)?;
            let start_num = start_month.number();
            let end_num = end_month.number();
            if start_num > end_num {
                return Err(ScheduleError::cron(format!(
                    "invalid month range: {} > {}",
                    start, end
                )));
            }
            for n in start_num..=end_num {
                months.push(month_from_number(n)?);
            }
        } else {
            // Single month
            months.push(parse_month_value(part)?);
        }
    }

    Ok(months)
}

/// Parse a single month value (number 1-12 or name JAN-DEC).
fn parse_month_value(s: &str) -> Result<MonthName, ScheduleError> {
    // Try as number first
    if let Ok(n) = s.parse::<u8>() {
        return month_from_number(n);
    }
    // Try as name
    parse_month_name(s).ok_or_else(|| ScheduleError::cron(format!("invalid month: {}", s)))
}

fn month_from_number(n: u8) -> Result<MonthName, ScheduleError> {
    match n {
        1 => Ok(MonthName::January),
        2 => Ok(MonthName::February),
        3 => Ok(MonthName::March),
        4 => Ok(MonthName::April),
        5 => Ok(MonthName::May),
        6 => Ok(MonthName::June),
        7 => Ok(MonthName::July),
        8 => Ok(MonthName::August),
        9 => Ok(MonthName::September),
        10 => Ok(MonthName::October),
        11 => Ok(MonthName::November),
        12 => Ok(MonthName::December),
        _ => Err(ScheduleError::cron(format!("invalid month number: {}", n))),
    }
}

/// Try to parse nth weekday patterns like 1#1 (first Monday) or 5L (last Friday).
fn try_parse_nth_weekday(
    minute_field: &str,
    hour_field: &str,
    dom_field: &str,
    dow_field: &str,
    during: &[MonthName],
) -> Result<Option<Schedule>, ScheduleError> {
    // Check for # pattern (nth weekday of month)
    if let Some((dow_str, nth_str)) = dow_field.split_once('#') {
        let dow_num = parse_dow_value(dow_str)?;
        let weekday = cron_dow_to_weekday(dow_num)?;
        let nth: u8 = nth_str
            .parse()
            .map_err(|_| ScheduleError::cron(format!("invalid nth value: {}", nth_str)))?;
        if nth == 0 || nth > 5 {
            return Err(ScheduleError::cron(format!("nth must be 1-5, got {}", nth)));
        }
        let ordinal = match nth {
            1 => OrdinalPosition::First,
            2 => OrdinalPosition::Second,
            3 => OrdinalPosition::Third,
            4 => OrdinalPosition::Fourth,
            5 => OrdinalPosition::Fifth,
            _ => unreachable!(),
        };

        if dom_field != "*" && dom_field != "?" {
            return Err(ScheduleError::cron(
                "DOM must be * when using # for nth weekday",
            ));
        }

        let minute: u8 = parse_single_value(minute_field, "minute", 0, 59)?;
        let hour: u8 = parse_single_value(hour_field, "hour", 0, 23)?;

        let mut schedule = Schedule::new(ScheduleExpr::MonthRepeat {
            interval: 1,
            target: MonthTarget::OrdinalWeekday { ordinal, weekday },
            times: vec![TimeOfDay { hour, minute }],
        });
        schedule.during = during.to_vec();
        return Ok(Some(schedule));
    }

    // Check for nL pattern (last weekday of month, e.g., 5L = last Friday)
    if dow_field.ends_with('L') && dow_field.len() > 1 {
        let dow_str = &dow_field[..dow_field.len() - 1];
        let dow_num = parse_dow_value(dow_str)?;
        let weekday = cron_dow_to_weekday(dow_num)?;

        if dom_field != "*" && dom_field != "?" {
            return Err(ScheduleError::cron(
                "DOM must be * when using nL for last weekday",
            ));
        }

        let minute: u8 = parse_single_value(minute_field, "minute", 0, 59)?;
        let hour: u8 = parse_single_value(hour_field, "hour", 0, 23)?;

        let mut schedule = Schedule::new(ScheduleExpr::MonthRepeat {
            interval: 1,
            target: MonthTarget::OrdinalWeekday {
                ordinal: OrdinalPosition::Last,
                weekday,
            },
            times: vec![TimeOfDay { hour, minute }],
        });
        schedule.during = during.to_vec();
        return Ok(Some(schedule));
    }

    Ok(None)
}

/// Try to parse L (last day) or LW (last weekday) patterns.
fn try_parse_last_day(
    minute_field: &str,
    hour_field: &str,
    dom_field: &str,
    dow_field: &str,
    during: &[MonthName],
) -> Result<Option<Schedule>, ScheduleError> {
    if dom_field != "L" && dom_field != "LW" {
        return Ok(None);
    }

    if dow_field != "*" && dow_field != "?" {
        return Err(ScheduleError::cron(
            "DOW must be * when using L or LW in DOM",
        ));
    }

    let minute: u8 = parse_single_value(minute_field, "minute", 0, 59)?;
    let hour: u8 = parse_single_value(hour_field, "hour", 0, 23)?;

    let target = if dom_field == "LW" {
        MonthTarget::LastWeekday
    } else {
        MonthTarget::LastDay
    };

    let mut schedule = Schedule::new(ScheduleExpr::MonthRepeat {
        interval: 1,
        target,
        times: vec![TimeOfDay { hour, minute }],
    });
    schedule.during = during.to_vec();
    Ok(Some(schedule))
}

/// Try to parse W (nearest weekday) patterns: 15W, 1W, etc.
fn try_parse_nearest_weekday(
    minute_field: &str,
    hour_field: &str,
    dom_field: &str,
    dow_field: &str,
    during: &[MonthName],
) -> Result<Option<Schedule>, ScheduleError> {
    if !dom_field.ends_with('W') || dom_field == "LW" {
        return Ok(None);
    }

    if dow_field != "*" && dow_field != "?" {
        return Err(ScheduleError::cron("DOW must be * when using W in DOM"));
    }

    let day_str = &dom_field[..dom_field.len() - 1];
    let day: u8 = day_str
        .parse()
        .map_err(|_| ScheduleError::cron(format!("invalid W day: {}", day_str)))?;

    if !(1..=31).contains(&day) {
        return Err(ScheduleError::cron(format!(
            "W day must be 1-31, got {}",
            day
        )));
    }

    let minute: u8 = parse_single_value(minute_field, "minute", 0, 59)?;
    let hour: u8 = parse_single_value(hour_field, "hour", 0, 23)?;

    let target = MonthTarget::NearestWeekday {
        day,
        direction: None,
    };

    let mut schedule = Schedule::new(ScheduleExpr::MonthRepeat {
        interval: 1,
        target,
        times: vec![TimeOfDay { hour, minute }],
    });
    schedule.during = during.to_vec();
    Ok(Some(schedule))
}

/// Try to parse interval patterns: */N, range/N in minute or hour fields.
fn try_parse_interval(
    minute_field: &str,
    hour_field: &str,
    dom_field: &str,
    dow_field: &str,
    during: &[MonthName],
) -> Result<Option<Schedule>, ScheduleError> {
    // Minute interval: */N or range/N
    if minute_field.contains('/') {
        let (range_part, step_str) = minute_field
            .split_once('/')
            .ok_or_else(|| ScheduleError::cron("invalid minute interval"))?;

        let interval: u32 = step_str
            .parse()
            .map_err(|_| ScheduleError::cron("invalid minute interval value"))?;

        if interval == 0 {
            return Err(ScheduleError::cron("step cannot be 0"));
        }

        let (from_minute, to_minute) = if range_part == "*" {
            (0u8, 59u8)
        } else if let Some((start, end)) = range_part.split_once('-') {
            let s: u8 = start
                .parse()
                .map_err(|_| ScheduleError::cron("invalid minute range"))?;
            let e: u8 = end
                .parse()
                .map_err(|_| ScheduleError::cron("invalid minute range"))?;
            if s > e {
                return Err(ScheduleError::cron(format!(
                    "range start must be <= end: {}-{}",
                    s, e
                )));
            }
            (s, e)
        } else {
            // Single value with step (e.g., 0/15) - treat as starting point
            let s: u8 = range_part
                .parse()
                .map_err(|_| ScheduleError::cron("invalid minute value"))?;
            (s, 59)
        };

        // Determine the hour window
        let (from_hour, to_hour) = if hour_field == "*" {
            (0u8, 23u8)
        } else if let Some((start, end)) = hour_field.split_once('-') {
            let s: u8 = start
                .parse()
                .map_err(|_| ScheduleError::cron("invalid hour range"))?;
            let e: u8 = end
                .parse()
                .map_err(|_| ScheduleError::cron("invalid hour range"))?;
            (s, e)
        } else if hour_field.contains('/') {
            // Hour also has step - this is complex, handle as hour interval
            return Ok(None);
        } else {
            let h: u8 = hour_field
                .parse()
                .map_err(|_| ScheduleError::cron("invalid hour"))?;
            (h, h)
        };

        // Check if this should be a day filter
        let day_filter = if dow_field == "*" {
            None
        } else {
            Some(parse_cron_dow(dow_field)?)
        };

        if dom_field == "*" || dom_field == "?" {
            // Determine the end minute based on context
            let end_minute = if from_minute == 0 && to_minute == 59 && to_hour == 23 {
                // Full day: 00:00 to 23:59
                59
            } else if from_minute == 0 && to_minute == 59 {
                // Partial day with full minutes range: use :00 for cleaner output
                0
            } else {
                to_minute
            };

            let mut schedule = Schedule::new(ScheduleExpr::IntervalRepeat {
                interval,
                unit: IntervalUnit::Minutes,
                from: TimeOfDay {
                    hour: from_hour,
                    minute: from_minute,
                },
                to: TimeOfDay {
                    hour: to_hour,
                    minute: end_minute,
                },
                day_filter,
            });
            schedule.during = during.to_vec();
            return Ok(Some(schedule));
        }
    }

    // Hour interval: 0 */N or 0 range/N
    if hour_field.contains('/') && (minute_field == "0" || minute_field == "00") {
        let (range_part, step_str) = hour_field
            .split_once('/')
            .ok_or_else(|| ScheduleError::cron("invalid hour interval"))?;

        let interval: u32 = step_str
            .parse()
            .map_err(|_| ScheduleError::cron("invalid hour interval value"))?;

        if interval == 0 {
            return Err(ScheduleError::cron("step cannot be 0"));
        }

        let (from_hour, to_hour) = if range_part == "*" {
            (0u8, 23u8)
        } else if let Some((start, end)) = range_part.split_once('-') {
            let s: u8 = start
                .parse()
                .map_err(|_| ScheduleError::cron("invalid hour range"))?;
            let e: u8 = end
                .parse()
                .map_err(|_| ScheduleError::cron("invalid hour range"))?;
            if s > e {
                return Err(ScheduleError::cron(format!(
                    "range start must be <= end: {}-{}",
                    s, e
                )));
            }
            (s, e)
        } else {
            let h: u8 = range_part
                .parse()
                .map_err(|_| ScheduleError::cron("invalid hour value"))?;
            (h, 23)
        };

        if (dom_field == "*" || dom_field == "?") && (dow_field == "*" || dow_field == "?") {
            // Use :59 only for full day (00:00 to 23:59), otherwise use :00
            let end_minute = if from_hour == 0 && to_hour == 23 {
                59
            } else {
                0
            };

            let mut schedule = Schedule::new(ScheduleExpr::IntervalRepeat {
                interval,
                unit: IntervalUnit::Hours,
                from: TimeOfDay {
                    hour: from_hour,
                    minute: 0,
                },
                to: TimeOfDay {
                    hour: to_hour,
                    minute: end_minute,
                },
                day_filter: None,
            });
            schedule.during = during.to_vec();
            return Ok(Some(schedule));
        }
    }

    Ok(None)
}

/// Parse a DOM field into a MonthTarget.
fn parse_dom_field(field: &str) -> Result<MonthTarget, ScheduleError> {
    let mut specs = Vec::new();

    for part in field.split(',') {
        if let Some((range_part, step_str)) = part.split_once('/') {
            // Step value: 1-31/2 or */5
            let (start, end) = if range_part == "*" {
                (1u8, 31u8)
            } else if let Some((s, e)) = range_part.split_once('-') {
                let start: u8 = s
                    .parse()
                    .map_err(|_| ScheduleError::cron(format!("invalid DOM range start: {}", s)))?;
                let end: u8 = e
                    .parse()
                    .map_err(|_| ScheduleError::cron(format!("invalid DOM range end: {}", e)))?;
                if start > end {
                    return Err(ScheduleError::cron(format!(
                        "range start must be <= end: {}-{}",
                        start, end
                    )));
                }
                (start, end)
            } else {
                let start: u8 = range_part.parse().map_err(|_| {
                    ScheduleError::cron(format!("invalid DOM value: {}", range_part))
                })?;
                (start, 31)
            };

            let step: u8 = step_str
                .parse()
                .map_err(|_| ScheduleError::cron(format!("invalid DOM step: {}", step_str)))?;
            if step == 0 {
                return Err(ScheduleError::cron("step cannot be 0"));
            }

            validate_dom(start)?;
            validate_dom(end)?;

            let mut d = start;
            while d <= end {
                specs.push(DayOfMonthSpec::Single(d));
                d += step;
            }
        } else if let Some((start_str, end_str)) = part.split_once('-') {
            // Range: 1-5
            let start: u8 = start_str.parse().map_err(|_| {
                ScheduleError::cron(format!("invalid DOM range start: {}", start_str))
            })?;
            let end: u8 = end_str
                .parse()
                .map_err(|_| ScheduleError::cron(format!("invalid DOM range end: {}", end_str)))?;
            if start > end {
                return Err(ScheduleError::cron(format!(
                    "range start must be <= end: {}-{}",
                    start, end
                )));
            }
            validate_dom(start)?;
            validate_dom(end)?;
            specs.push(DayOfMonthSpec::Range(start, end));
        } else {
            // Single: 15
            let day: u8 = part
                .parse()
                .map_err(|_| ScheduleError::cron(format!("invalid DOM value: {}", part)))?;
            validate_dom(day)?;
            specs.push(DayOfMonthSpec::Single(day));
        }
    }

    Ok(MonthTarget::Days(specs))
}

fn validate_dom(day: u8) -> Result<(), ScheduleError> {
    if !(1..=31).contains(&day) {
        return Err(ScheduleError::cron(format!(
            "DOM must be 1-31, got {}",
            day
        )));
    }
    Ok(())
}

/// Parse a DOW field into a DayFilter.
fn parse_cron_dow(field: &str) -> Result<DayFilter, ScheduleError> {
    if field == "*" {
        return Ok(DayFilter::Every);
    }

    let mut days = Vec::new();

    for part in field.split(',') {
        if let Some((range_part, step_str)) = part.split_once('/') {
            // Step value: 0-6/2 or */2
            let (start, end) = if range_part == "*" {
                (0u8, 6u8)
            } else if let Some((s, e)) = range_part.split_once('-') {
                let start = parse_dow_value_raw(s)?;
                let end = parse_dow_value_raw(e)?;
                if start > end {
                    return Err(ScheduleError::cron(format!(
                        "range start must be <= end: {}-{}",
                        s, e
                    )));
                }
                (start, end)
            } else {
                let start = parse_dow_value_raw(range_part)?;
                (start, 6)
            };

            let step: u8 = step_str
                .parse()
                .map_err(|_| ScheduleError::cron(format!("invalid DOW step: {}", step_str)))?;
            if step == 0 {
                return Err(ScheduleError::cron("step cannot be 0"));
            }

            let mut d = start;
            while d <= end {
                days.push(cron_dow_to_weekday(d)?);
                d += step;
            }
        } else if let Some((start_str, end_str)) = part.split_once('-') {
            // Range: 1-5 or MON-FRI
            // Parse without normalizing 7 to 0 for range purposes
            let start = parse_dow_value_raw(start_str)?;
            let end = parse_dow_value_raw(end_str)?;
            if start > end {
                return Err(ScheduleError::cron(format!(
                    "range start must be <= end: {}-{}",
                    start_str, end_str
                )));
            }
            for d in start..=end {
                // Normalize 7 to 0 (Sunday) when converting to weekday
                let normalized = if d == 7 { 0 } else { d };
                days.push(cron_dow_to_weekday(normalized)?);
            }
        } else {
            // Single: 1 or MON
            let dow = parse_dow_value(part)?;
            days.push(cron_dow_to_weekday(dow)?);
        }
    }

    // Check for special patterns
    if days.len() == 5 {
        let mut sorted = days.clone();
        sorted.sort_by_key(|d| d.number());
        if sorted == Weekday::all_weekdays() {
            return Ok(DayFilter::Weekday);
        }
    }
    if days.len() == 2 {
        let mut sorted = days.clone();
        sorted.sort_by_key(|d| d.number());
        if sorted == vec![Weekday::Saturday, Weekday::Sunday] {
            return Ok(DayFilter::Weekend);
        }
    }

    Ok(DayFilter::Days(days))
}

/// Parse a DOW value (number 0-7 or name SUN-SAT), normalizing 7 to 0.
fn parse_dow_value(s: &str) -> Result<u8, ScheduleError> {
    let raw = parse_dow_value_raw(s)?;
    // Normalize 7 to 0 (both mean Sunday)
    Ok(if raw == 7 { 0 } else { raw })
}

/// Parse a DOW value without normalizing 7 to 0 (for range checking).
fn parse_dow_value_raw(s: &str) -> Result<u8, ScheduleError> {
    // Try as number first
    if let Ok(n) = s.parse::<u8>() {
        if n > 7 {
            return Err(ScheduleError::cron(format!("DOW must be 0-7, got {}", n)));
        }
        return Ok(n);
    }
    // Try as name
    match s.to_uppercase().as_str() {
        "SUN" => Ok(0),
        "MON" => Ok(1),
        "TUE" => Ok(2),
        "WED" => Ok(3),
        "THU" => Ok(4),
        "FRI" => Ok(5),
        "SAT" => Ok(6),
        _ => Err(ScheduleError::cron(format!("invalid DOW: {}", s))),
    }
}

fn cron_dow_to_weekday(n: u8) -> Result<Weekday, ScheduleError> {
    match n {
        0 | 7 => Ok(Weekday::Sunday),
        1 => Ok(Weekday::Monday),
        2 => Ok(Weekday::Tuesday),
        3 => Ok(Weekday::Wednesday),
        4 => Ok(Weekday::Thursday),
        5 => Ok(Weekday::Friday),
        6 => Ok(Weekday::Saturday),
        _ => Err(ScheduleError::cron(format!("invalid DOW number: {n}"))),
    }
}

/// Parse a single numeric value with validation.
fn parse_single_value(field: &str, name: &str, min: u8, max: u8) -> Result<u8, ScheduleError> {
    let value: u8 = field
        .parse()
        .map_err(|_| ScheduleError::cron(format!("invalid {} field: {}", name, field)))?;
    if value < min || value > max {
        return Err(ScheduleError::cron(format!(
            "{} must be {}-{}, got {}",
            name, min, max, value
        )));
    }
    Ok(value)
}

/// Explain a cron expression in human-readable form (best effort).
pub fn explain_cron(cron: &str) -> Result<String, ScheduleError> {
    let schedule = from_cron(cron)?;
    let mut explanation = schedule.to_string();

    // Add warnings for cron quirks
    let fields: Vec<&str> = cron.split_whitespace().collect();
    if fields.len() == 5 {
        if let Some(minute_field) = fields.first() {
            if let Some(interval_str) = minute_field.strip_prefix("*/") {
                if let Ok(interval) = interval_str.parse::<u32>() {
                    if 60 % interval != 0 {
                        explanation.push_str(&format!(
                            "\nnote: cron */{interval} actually fires at {} each hour, not true {interval}-min intervals",
                            generate_cron_minute_fires(interval)
                        ));
                    }
                }
            }
        }
    }

    Ok(explanation)
}

fn generate_cron_minute_fires(interval: u32) -> String {
    let mut minutes = Vec::new();
    let mut m = 0;
    while m < 60 {
        minutes.push(format!(":{:02}", m));
        m += interval;
    }
    minutes.join(" and ")
}

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

    #[test]
    fn test_to_cron_every_day() {
        let s = parse("every day at 9:00").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 9 * * *");
    }

    #[test]
    fn test_to_cron_weekday() {
        let s = parse("every weekday at 9:00").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 9 * * 1-5");
    }

    #[test]
    fn test_to_cron_weekend() {
        let s = parse("every weekend at 10:00").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 10 * * 0,6");
    }

    #[test]
    fn test_to_cron_specific_days() {
        let s = parse("every mon, wed, fri at 9:00").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 9 * * 1,3,5");
    }

    #[test]
    fn test_to_cron_interval_minutes() {
        let s = parse("every 30 min from 00:00 to 23:59").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "*/30 * * * *");
    }

    #[test]
    fn test_to_cron_interval_hours() {
        let s = parse("every 2 hours from 00:00 to 23:59").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 */2 * * *");
    }

    #[test]
    fn test_to_cron_month_single_day() {
        let s = parse("every month on the 1st at 9:00").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 9 1 * *");
    }

    #[test]
    fn test_to_cron_month_multiple_days() {
        let s = parse("every month on the 1st, 15th at 9:00").unwrap();
        assert_eq!(to_cron(&s).unwrap(), "0 9 1,15 * *");
    }

    #[test]
    fn test_to_cron_not_expressible_45min() {
        let s = parse("every 45 min from 09:00 to 17:00").unwrap();
        assert!(to_cron(&s).is_err());
    }

    #[test]
    fn test_to_cron_not_expressible_week() {
        let s = parse("every 2 weeks on monday at 9:00").unwrap();
        assert!(to_cron(&s).is_err());
    }

    #[test]
    fn test_to_cron_not_expressible_last_day() {
        let s = parse("every month on the last day at 17:00").unwrap();
        assert!(to_cron(&s).is_err());
    }

    #[test]
    fn test_to_cron_not_expressible_ordinal() {
        let s = parse("every month on the first monday at 10:00").unwrap();
        assert!(to_cron(&s).is_err());
    }

    #[test]
    fn test_to_cron_not_expressible_yearly() {
        let s = parse("every year on dec 25 at 00:00").unwrap();
        assert!(to_cron(&s).is_err());
    }

    #[test]
    fn test_from_cron_every_day() {
        let s = from_cron("0 9 * * *").unwrap();
        assert_eq!(s.to_string(), "every day at 09:00");
    }

    #[test]
    fn test_from_cron_weekday() {
        let s = from_cron("0 9 * * 1-5").unwrap();
        assert_eq!(s.to_string(), "every weekday at 09:00");
    }

    #[test]
    fn test_from_cron_monthly() {
        let s = from_cron("0 9 1 * *").unwrap();
        assert_eq!(s.to_string(), "every month on the 1st at 09:00");
    }

    #[test]
    fn test_from_cron_interval_minutes() {
        let s = from_cron("*/30 * * * *").unwrap();
        assert_eq!(s.to_string(), "every 30 min from 00:00 to 23:59");
    }

    #[test]
    fn test_from_cron_dom_range() {
        let s = from_cron("0 9 1-5 * *").unwrap();
        assert_eq!(s.to_string(), "every month on the 1st to 5th at 09:00");
    }

    #[test]
    fn test_from_cron_dow_range() {
        let s = from_cron("0 9 * * 2-4").unwrap();
        assert_eq!(s.to_string(), "every tuesday, wednesday, thursday at 09:00");
    }

    #[test]
    fn test_from_cron_month_field() {
        let s = from_cron("0 9 1 1,7 *").unwrap();
        assert_eq!(
            s.to_string(),
            "every month on the 1st at 09:00 during jan, jul"
        );
    }

    #[test]
    fn test_from_cron_at_daily() {
        let s = from_cron("@daily").unwrap();
        assert_eq!(s.to_string(), "every day at 00:00");
    }

    #[test]
    fn test_from_cron_last_day() {
        let s = from_cron("0 9 L * *").unwrap();
        assert_eq!(s.to_string(), "every month on the last day at 09:00");
    }

    #[test]
    fn test_from_cron_nth_weekday() {
        let s = from_cron("0 9 * * 1#1").unwrap();
        assert_eq!(s.to_string(), "every month on the first monday at 09:00");
    }

    #[test]
    fn test_from_cron_question_mark() {
        let s = from_cron("0 9 ? * 1").unwrap();
        assert_eq!(s.to_string(), "every monday at 09:00");
    }

    #[test]
    fn test_from_cron_named_dow() {
        let s = from_cron("0 9 * * MON,WED,FRI").unwrap();
        assert_eq!(s.to_string(), "every monday, wednesday, friday at 09:00");
    }

    #[test]
    fn test_from_cron_named_month() {
        let s = from_cron("0 9 1 JAN,JUL *").unwrap();
        assert_eq!(
            s.to_string(),
            "every month on the 1st at 09:00 during jan, jul"
        );
    }
}