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
use crate::utils::IteratorExt;

const ZERO_CHAR_BYTE: u8 = b'0';
const EXPR_SPLIT: char = ',';

#[derive(Debug, Copy, Clone)]
///Describes potential error within expression
pub enum InvalidExpr {
    ///Wildcard is used with other values.
    InvalidWildCard,
    ///Indicates invalid integer used as step value.
    InvalidStepRange,
    ///Indicates that step is not integer.
    InvalidStepValue,
    ///Indicates that specified value is outside of allowed range.
    InvalidEntryRange,
    ///Indicates that specified value is not of valid type.
    InvalidEntryValue,
    ///Indicates that specified range is not valid.
    InvalidRange,
    ///Indicates that specified range contains reversed values.
    InvalidRangeRev,
    ///Indicates that too many values are parsed. Indicates Internal Error of library.
    ParserOverflow
}

#[cold]
#[inline(never)]
const fn parser_overflow() -> InvalidExpr {
    InvalidExpr::ParserOverflow
}

macro_rules! impl_into_inner {
    ($($ty:ident as $as:ty;)+) => {
        $(
            impl Into<$as> for $ty {
                #[inline(always)]
                fn into(self) -> $as {
                    self as $as
                }
            }
        )+
    };
    ($($ty:ident unpack $as:ty;)+) => {
        $(
            impl Into<$as> for $ty {
                #[inline(always)]
                fn into(self) -> $as {
                    self.0 as _
                }
            }
        )+
    }
}

impl_into_inner!(
    DayOfMonth unpack u8;
    DayOfMonth unpack usize;
    Minute unpack u8;
    Minute unpack usize;
    Hour unpack u8;
    Hour unpack usize;
);
impl_into_inner!(
    Month as u8;
    Month as usize;
    Day as u8;
    Day as usize;
);

macro_rules! impl_from_expr {
    ($text:expr) => {
        let text = $text;
        let mut result = statiki::Array::new();

        let mut fields = text.split(EXPR_SPLIT);
        while let Some(field) = fields.next() {
            if field == "*" {
                if !result.is_empty() || fields.next().is_some() {
                    return Err(InvalidExpr::InvalidWildCard);
                }

                for num in Self::MIN..=Self::MAX {
                    if result.push(Self::from_num_asserted(num)).is_some() {
                        return Err(parser_overflow());
                    }
                }

            } else if let Some([init, step]) = field.split("/").collect_exact() {
                let init: u8 = match init {
                    "*" => Self::MIN,
                    init => Self::from_str(init, InvalidExpr::InvalidStepValue, InvalidExpr::InvalidStepRange)?.into(),
                };
                let step: usize = Self::from_str(step, InvalidExpr::InvalidStepValue, InvalidExpr::InvalidStepRange)?.into();

                if step == 0 {
                    return Err(InvalidExpr::InvalidStepRange);
                }

                for num in (init..=Self::MAX).step_by(step) {
                    let num = Self::from_num_asserted(num);
                    if !result.contains(&num) {
                        if result.push(num).is_some() {
                            return Err(parser_overflow());
                        }
                    }
                }

                result.sort_unstable();
            } else if let Some([from, to]) = field.split("-").collect_exact() {
                let from = Self::from_str(from, InvalidExpr::InvalidRange, InvalidExpr::InvalidRange)?;
                let to = Self::from_str(to, InvalidExpr::InvalidRange, InvalidExpr::InvalidRange)?;

                if from > to {
                    return Err(InvalidExpr::InvalidRangeRev);
                }

                for num in from.into()..=to.into() {
                    let num = Self::from_num_asserted(num);
                    if !result.contains(&num) {
                        if result.push(num).is_some() {
                            return Err(parser_overflow());
                        }
                    }
                }

                result.sort_unstable();
            } else {
                let num = Self::from_str(field, InvalidExpr::InvalidEntryValue, InvalidExpr::InvalidEntryRange)?;
                if !result.contains(&num) {
                    if result.push(num).is_some() {
                        return Err(parser_overflow());
                    }
                    result.sort_unstable();
                }
            }
        }

        return Ok(result);
    }
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
///Second of the minute.
///
///# Allowed values:
///
///- `1..=31`
pub struct DayOfMonth(u8);

impl DayOfMonth {
    ///Min possible value.
    pub const MIN: u8 = 1;
    ///Max possible value.
    pub const MAX: u8 = 31;
    ///Expression name.
    pub const NAME: &'static str = "Day of Month";

    ///Creates instance from numeric
    pub(crate) const fn from_num_asserted(num: u8) -> Self {
        Self(num)
    }

    ///Creates instance from numeric
    pub const fn from_num(num: u8) -> Option<Self> {
        if num <= Self::MAX {
            Some(Self(num))
        } else {
            None
        }
    }

    #[inline(always)]
    fn from_str(text: &str, invalid_val: InvalidExpr, invalid_range: InvalidExpr) -> Result<Self, InvalidExpr> {
        match text.parse() {
            Ok(num) if num <= Self::MAX && num >= Self::MIN => Ok(Self(num)),
            Ok(_) => return Err(invalid_range),
            Err(_) => return Err(invalid_val),
        }
    }

    ///Creates instance from cron expression
    pub fn from_expr(text: &str) -> Result<statiki::Array<Self, 31>, InvalidExpr> {
        impl_from_expr!(text);
    }
}

impl core::fmt::Display for DayOfMonth {
    #[inline(always)]
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.write_fmt(format_args!("{}", self.0))
    }
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
///Minute of the hour.
///
///# Allowed values:
///
///- `0..=59`
pub struct Minute(u8);

impl Minute {
    ///Min possible value.
    pub const MIN: u8 = 0;
    ///Max possible value.
    pub const MAX: u8 = 59;
    ///Expression name.
    pub const NAME: &'static str = "Minute";

    ///Creates instance from numeric
    pub(crate) const fn from_num_asserted(num: u8) -> Self {
        Self(num)
    }

    ///Creates instance from numeric
    pub const fn from_num(num: u8) -> Option<Self> {
        if num < 60 {
            Some(Self(num))
        } else {
            None
        }
    }

    #[inline(always)]
    fn from_str(text: &str, invalid_val: InvalidExpr, invalid_range: InvalidExpr) -> Result<Self, InvalidExpr> {
        match text.parse() {
            Ok(num) if num <= Self::MAX => Ok(Self(num)),
            Ok(_) => return Err(invalid_range),
            Err(_) => return Err(invalid_val),
        }
    }

    ///Creates instance from cron expression
    pub fn from_expr(text: &str) -> Result<statiki::Array<Self, 60>, InvalidExpr> {
        impl_from_expr!(text);
    }
}

impl core::fmt::Display for Minute {
    #[inline(always)]
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.write_fmt(format_args!("{}", self.0))
    }
}

#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
///Hour of the day.
///
///# Allowed values:
///
///- `0..=23`
pub struct Hour(u8);

impl Hour {
    ///Min possible value.
    pub const MIN: u8 = 0;
    ///Max possible value.
    pub const MAX: u8 = 23;
    ///Expression name.
    pub const NAME: &'static str = "Hour";

    ///Creates instance from numeric
    pub(crate) const fn from_num_asserted(num: u8) -> Self {
        Self(num)
    }

    ///Creates instance from numeric
    pub const fn from_num(num: u8) -> Option<Self> {
        if num <= Self::MAX {
            Some(Self(num))
        } else {
            None
        }
    }

    #[inline(always)]
    fn from_str(text: &str, invalid_val: InvalidExpr, invalid_range: InvalidExpr) -> Result<Self, InvalidExpr> {
        match text.parse() {
            Ok(num) if num <= Self::MAX => Ok(Self(num)),
            Ok(_) => return Err(invalid_range),
            Err(_) => return Err(invalid_val),
        }
    }

    ///Creates instance from cron expression
    pub fn from_expr(text: &str) -> Result<statiki::Array<Self, 24>, InvalidExpr> {
        impl_from_expr!(text);
    }
}

impl core::fmt::Display for Hour {
    #[inline(always)]
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.write_fmt(format_args!("{}", self.0))
    }
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
///Day of the week.
///
///# Allowed values:
///
///- `0..=6`
///- `SUN..=SAT` - case is ignored
pub enum Day {
    ///Sunday
    Sunday = 0,
    ///Monday
    Monday = 1,
    ///Tuesday
    Tuesday = 2,
    ///Wednesday
    Wednesday = 3,
    ///Thursday
    Thursday = 4,
    ///Friday
    Friday = 5,
    ///Friday
    Saturday = 6,
}

impl Day {
    ///Min possible value.
    pub const MIN: u8 = 0;
    ///Max possible value.
    pub const MAX: u8 = 6;
    ///Expression name.
    pub const NAME: &'static str = "Day of Week";

    ///Creates instance from numeric
    pub(crate) fn from_num_asserted(num: u8) -> Self {
        unsafe {
            core::mem::transmute(num)
        }
    }

    ///Creates instance from numeric
    pub const fn from_num(num: u8) -> Option<Self> {
        //transmute once it is const fn
        match num {
            0 => Some(Self::Sunday),
            1 => Some(Self::Monday),
            2 => Some(Self::Tuesday),
            3 => Some(Self::Wednesday),
            4 => Some(Self::Thursday),
            5 => Some(Self::Friday),
            6 => Some(Self::Saturday),
            _ => None
        }
    }

    ///Returns textual representation of cron expression
    #[inline(always)]
    pub const fn to_textual_repr(self) -> &'static str {
        match self {
            Self::Sunday => "SUN",
            Self::Monday => "MON",
            Self::Tuesday => "TUE",
            Self::Wednesday => "WED",
            Self::Thursday => "THU",
            Self::Friday => "FRI",
            Self::Saturday => "SAT",
        }
    }

    const fn from_textual_repr(text: &[u8]) -> Option<Self> {
        let text = [
            text[0].to_ascii_uppercase(),
            text[1].to_ascii_uppercase(),
            text[2].to_ascii_uppercase(),
        ];

        return match &text {
            b"SUN" => Some(Self::Sunday),
            b"MON" => Some(Self::Monday),
            b"TUE" => Some(Self::Tuesday),
            b"WED" => Some(Self::Wednesday),
            b"THU" => Some(Self::Thursday),
            b"FRI" => Some(Self::Friday),
            b"SAT" => Some(Self::Saturday),
            _ => None
        };
    }

    ///Parses day from the string accordingly to allowed values.
    pub const fn from_bytes(text: &[u8]) -> Option<Self> {
        if text.len() == 1 {
            let num = text[0];
            if num >= ZERO_CHAR_BYTE {
                return Self::from_num(num - ZERO_CHAR_BYTE);
            } else {
                return None
            }
        } else if text.len() == 3 {
            return Self::from_textual_repr(text)
        }

        None
    }

    #[inline(always)]
    fn from_str(text: &str, invalid_val: InvalidExpr, invalid_range: InvalidExpr) -> Result<Self, InvalidExpr> {
        match text.parse() {
            Ok(num) if num <= Self::MAX => Ok(Self::from_num_asserted(num)),
            Ok(_) => Err(invalid_range),
            Err(_) if text.len() == 3 => match Self::from_textual_repr(text.as_bytes()) {
                Some(num) => Ok(num),
                None => Err(invalid_val)
            },
            Err(_) => Err(invalid_val),
        }
    }

    ///Creates instance from cron expression
    pub fn from_expr(text: &str) -> Result<statiki::Array<Self, 7>, InvalidExpr> {
        impl_from_expr!(text);
    }
}

impl core::fmt::Display for Day {
    #[inline(always)]
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.write_str(self.to_textual_repr())
    }
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
///Month of the year.
///
///# Allowed values:
///
///- `1..=12`
///- `JAN..=DEC` - case is ignored
pub enum Month {
    ///January
    January = 1,
    ///February
    February = 2,
    ///March
    March = 3,
    ///April
    April = 4,
    ///May
    May = 5,
    ///June
    June = 6,
    ///July
    July = 7,
    ///August
    August = 8,
    ///September
    September = 9,
    ///October
    October = 10,
    ///November
    November = 11,
    ///December
    December = 12,
}

#[cfg(feature = "time")]
impl Into<time::Month> for Month {
    #[inline]
    fn into(self) -> time::Month {
        unsafe {
            core::mem::transmute(self)
        }
    }
}

impl Month {
    ///Min possible value.
    pub const MIN: u8 = 1;
    ///Max possible value.
    pub const MAX: u8 = 12;
    ///Expression name.
    pub const NAME: &'static str = "Month";

    ///Creates instance from numeric
    pub(crate) fn from_num_asserted(num: u8) -> Self {
        unsafe {
            core::mem::transmute(num)
        }
    }

    ///Creates instance from numeric
    pub const fn from_num(num: u8) -> Option<Self> {
        //transmute once it is const fn
        match num {
            1 => Some(Self::January),
            2 => Some(Self::February),
            3 => Some(Self::March),
            4 => Some(Self::April),
            5 => Some(Self::May),
            6 => Some(Self::June),
            7 => Some(Self::July),
            8 => Some(Self::August),
            9 => Some(Self::September),
            10 => Some(Self::October),
            11 => Some(Self::November),
            12 => Some(Self::December),
            _ => None
        }

    }

    ///Returns textual representation of cron expression
    #[inline(always)]
    pub const fn to_textual_repr(self) -> &'static str {
        match self {
            Self::January => "JAN",
            Self::February => "FEB",
            Self::March => "MAR",
            Self::April => "APR",
            Self::May => "MAY",
            Self::June => "JUN",
            Self::July => "JUL",
            Self::August => "AUG",
            Self::September => "SEP",
            Self::October => "OCT",
            Self::November => "NOV",
            Self::December => "DEC",
        }
    }

    const fn from_textual_repr(text: &[u8]) -> Option<Self> {
        let text = [
            text[0].to_ascii_uppercase(),
            text[1].to_ascii_uppercase(),
            text[2].to_ascii_uppercase(),
        ];

        return match &text {
            b"JAN" => Some(Self::January),
            b"FEB" => Some(Self::February),
            b"MAR" => Some(Self::March),
            b"APR" => Some(Self::April),
            b"MAY" => Some(Self::May),
            b"JUN" => Some(Self::June),
            b"JUL" => Some(Self::July),
            b"AUG" => Some(Self::August),
            b"SEP" => Some(Self::September),
            b"OCT" => Some(Self::October),
            b"NOV" => Some(Self::November),
            b"DEC" => Some(Self::December),
            _ => None
        };
    }

    ///Parses day from the string accordingly to allowed values.
    pub const fn from_bytes(text: &[u8]) -> Option<Self> {
        if text.len() < 3 {
            return match text {
                b"1" => Some(Self::January),
                b"2" => Some(Self::February),
                b"3" => Some(Self::March),
                b"4" => Some(Self::April),
                b"5" => Some(Self::May),
                b"6" => Some(Self::June),
                b"7" => Some(Self::July),
                b"8" => Some(Self::August),
                b"9" => Some(Self::September),
                b"10" => Some(Self::October),
                b"11" => Some(Self::November),
                b"12" => Some(Self::December),
                _ => None,
            };
        } else if text.len() == 3 {
            return Self::from_textual_repr(text);
        }

        None
    }

    #[inline(always)]
    fn from_str(text: &str, invalid_val: InvalidExpr, invalid_range: InvalidExpr) -> Result<Self, InvalidExpr> {
        match text.parse() {
            Ok(num) if num <= Self::MAX && num >= Self::MIN => Ok(Self::from_num_asserted(num)),
            Ok(_) => Err(invalid_range),
            Err(_) if text.len() == 3 => match Self::from_textual_repr(text.as_bytes()) {
                Some(num) => Ok(num),
                None => Err(invalid_val)
            },
            Err(_) => Err(invalid_val),
        }
    }

    ///Creates instance from cron expression
    pub fn from_expr(text: &str) -> Result<statiki::Array<Self, 12>, InvalidExpr> {
        impl_from_expr!(text);
    }
}

impl core::fmt::Display for Month {
    #[inline(always)]
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        fmt.write_str(self.to_textual_repr())
    }
}