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
use chrono::{DateTime, Datelike, Duration, Local, Weekday};
use serde::{Deserialize, Serialize};

pub static REMINDER_EXAMPLES: &str = r###"
# This reminder will disappear once executed.

- name: Test reminder once on 10 July 2020, at 8 am IST
  when: "2020-07-10T08:00:00+05:30"
  repeat: Never

# The following reminders will reschedule themselves.

- name: "Test reminder everyday at 10:30 pm IST"
  when: "2020-07-10T22:30:00+05:30"
  repeat: EveryDay

- name: "Test reminder every other day at 10:30 pm IST"
  when: "2020-07-10T22:30:00+05:30"
  repeat:
    EveryNthDay: 2

- name: Test reminder every week at 11 am IST
  when: "2020-07-10T11:00:00+05:30"
  repeat: EveryWeek

- name: Test reminder every 3rd week at 11 am IST
  when: "2020-07-10T11:00:00+05:30"
  repeat:
    EveryNthWeek: 3

- name: "Test reminder every saturday and sunday at 9:15 am IST"
  when: "2020-07-10T09:15:00+05:30"
  repeat:
    Weekdays:
      - Sat
      - Sun

- name: "Test reminder every 2nd saturday at 9:15 am IST"
  when: "2020-07-10T09:15:00+05:30"
  repeat:
    EveryNthWeekday:
      n: 2
      weekday: Sat
"###;

#[derive(Serialize, Deserialize, Copy, Clone)]
pub struct NthWeekday {
    n: u32,
    weekday: Weekday,
}

impl NthWeekday {
    pub fn from(n: u32, weekday: Weekday) -> Self {
        Self { n, weekday }
    }
    pub fn n(&self) -> u32 {
        self.n
    }
    pub fn weekday(&self) -> Weekday {
        self.weekday
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub enum Repeat {
    Never,
    EveryDay,
    EveryNthDay(u32),
    EveryWeek,
    EveryNthWeek(u32),
    Weekly(Vec<Weekday>),
    Weekdays(Vec<Weekday>),
    EveryNthWeekday(NthWeekday),
}
impl Repeat {
    pub fn when_next(&self, when_last: DateTime<Local>) -> Option<DateTime<Local>> {
        match self {
            Self::Never => None,
            Self::EveryDay => Some(when_last + Duration::days(1)),
            Self::EveryNthDay(days) => Some(when_last + Duration::days(*days as i64)),
            Self::EveryWeek => Some(when_last + Duration::days(7)),
            Self::EveryNthWeek(weeks) => Some(when_last + Duration::days((weeks * 7).into())),
            Self::Weekdays(weekdays) | Self::Weekly(weekdays) => {
                let mut weekday = when_last.weekday().succ();
                let mut days = 1;

                while !weekdays.contains(&weekday) {
                    weekday = weekday.succ();
                    days += 1;
                }

                Some(when_last + Duration::days(days))
            }
            Self::EveryNthWeekday(nthweekday) => {
                let mut weekday = when_last.weekday().succ();
                let mut days = 1;

                while weekday != nthweekday.weekday() {
                    weekday = weekday.succ();
                    days += 1;
                }

                Some(when_last + Duration::days((days + 7 * nthweekday.n()).into()))
            }
        }
    }
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Reminder {
    name: String,
    when: DateTime<Local>,
    repeat: Repeat,
}

impl Reminder {
    pub fn new(name: String, when: DateTime<Local>, repeat: Repeat) -> Self {
        Self { name, when, repeat }
    }
    pub fn name(&self) -> &String {
        &self.name
    }
    pub fn when(&self) -> &DateTime<Local> {
        &self.when
    }
    pub fn repeat(&self) -> &Repeat {
        &self.repeat
    }

    pub fn examples() -> Vec<Reminder> {
        let reminders: Vec<Reminder> =
            serde_yaml::from_str(REMINDER_EXAMPLES).expect("invalid reminders template");
        reminders
    }

    pub fn next(&self) -> Option<Self> {
        self.repeat
            .when_next(self.when.clone())
            .map(|when| Self::new(self.name.clone(), when, self.repeat.clone()))
    }
}