1use super::*;
2
3impl CronSchedule {
5 pub fn parse(expr: &str) -> Result<Self, CronError> {
15 let trimmed: &str = expr.trim();
16 let expanded: &str = match trimmed {
17 ALIAS_YEARLY | ALIAS_ANNUALLY => EXPR_YEARLY,
18 ALIAS_MONTHLY => EXPR_MONTHLY,
19 ALIAS_WEEKLY => EXPR_WEEKLY,
20 ALIAS_DAILY | ALIAS_MIDNIGHT => EXPR_DAILY,
21 ALIAS_HOURLY => EXPR_HOURLY,
22 other => other,
23 };
24 let fields: Vec<&str> = expanded.split_ascii_whitespace().collect();
25 let (sec_f, min_f, hr_f, dom_f, mon_f, dow_f): (&str, &str, &str, &str, &str, &str) =
26 match fields.len() {
27 5 => ("0", fields[0], fields[1], fields[2], fields[3], fields[4]),
28 6 => (
29 fields[0], fields[1], fields[2], fields[3], fields[4], fields[5],
30 ),
31 _ => return Err(CronError::InvalidFieldCount),
32 };
33 let (seconds, _): (u64, bool) = parse_cron_field(sec_f, 0, 59, false, false)?;
34 let (minutes, _): (u64, bool) = parse_cron_field(min_f, 0, 59, false, false)?;
35 let (hours, _): (u64, bool) = parse_cron_field(hr_f, 0, 23, false, false)?;
36 let (days, dom_wild): (u64, bool) = parse_cron_field(dom_f, 1, 31, false, false)?;
37 let (months, _): (u64, bool) = parse_cron_field(mon_f, 1, 12, true, false)?;
38 let (weekdays, dow_wild): (u64, bool) = parse_cron_field(dow_f, 0, 6, false, true)?;
39 Ok(Self {
40 seconds,
41 minutes,
42 hours,
43 days,
44 months,
45 weekdays,
46 day_restricted: !dom_wild,
47 weekday_restricted: !dow_wild,
48 })
49 }
50
51 #[inline(always)]
64 fn day_matches(&self, day: u8, weekday: u8) -> bool {
65 let Self {
66 days,
67 weekdays,
68 day_restricted,
69 weekday_restricted,
70 ..
71 } = self;
72 let dom_hit: bool = (*days & (1u64 << day)) != 0;
73 let dow_hit: bool = (*weekdays & (1u64 << weekday)) != 0;
74 if *day_restricted && *weekday_restricted {
75 dom_hit || dow_hit
76 } else {
77 dom_hit && dow_hit
78 }
79 }
80
81 #[inline(always)]
91 pub fn matches_datetime(&self, dt: UtcDateTime) -> bool {
92 let Self {
93 seconds,
94 minutes,
95 hours,
96 months,
97 ..
98 } = self;
99 (*seconds & (1u64 << dt.get_second())) != 0
100 && (*minutes & (1u64 << dt.get_minute())) != 0
101 && (*hours & (1u64 << dt.get_hour())) != 0
102 && (*months & (1u64 << dt.get_month())) != 0
103 && self.day_matches(dt.get_day(), dt.get_weekday())
104 }
105
106 #[inline(always)]
116 pub fn matches_unix_secs(&self, unix_secs: i64) -> bool {
117 self.matches_datetime(UtcDateTime::from_unix_secs(unix_secs))
118 }
119
120 pub fn next_after(&self, after_unix_secs: i64) -> Option<i64> {
132 let start_dt: UtcDateTime = UtcDateTime::from_unix_secs(after_unix_secs.checked_add(1)?);
133 let max_year: i32 = start_dt.get_year().saturating_add(MAX_SEARCH_YEARS);
134 let Self {
135 seconds,
136 minutes,
137 hours,
138 months,
139 ..
140 } = self;
141 let first_sec: u8 = next_set_bit(*seconds, 0)?;
142 let first_min: u8 = next_set_bit(*minutes, 0)?;
143 let first_hr: u8 = next_set_bit(*hours, 0)?;
144 let mut year: i32 = start_dt.get_year();
145 let mut month: u8 = start_dt.get_month();
146 let mut day: u8 = start_dt.get_day();
147 let mut hour: u8 = start_dt.get_hour();
148 let mut minute: u8 = start_dt.get_minute();
149 let mut second: u8 = start_dt.get_second();
150 while year <= max_year {
151 match next_set_bit(*months, month) {
152 Some(next_m) if next_m == month => {}
153 Some(next_m) => {
154 month = next_m;
155 day = 1;
156 hour = first_hr;
157 minute = first_min;
158 second = first_sec;
159 }
160 None => {
161 year += 1;
162 month = next_set_bit(*months, 1)?;
163 day = 1;
164 hour = first_hr;
165 minute = first_min;
166 second = first_sec;
167 continue;
168 }
169 }
170 let dim: u8 = days_in_month(year, month);
171 if day > dim {
172 month += 1;
173 if month > 12 {
174 year += 1;
175 month = 1;
176 }
177 day = 1;
178 hour = first_hr;
179 minute = first_min;
180 second = first_sec;
181 continue;
182 }
183 let weekday: u8 = weekday_from_days(days_from_civil(year, month, day));
184 if !self.day_matches(day, weekday) {
185 day += 1;
186 hour = first_hr;
187 minute = first_min;
188 second = first_sec;
189 continue;
190 }
191 match next_set_bit(*hours, hour) {
192 Some(next_h) if next_h == hour => {}
193 Some(next_h) => {
194 hour = next_h;
195 minute = first_min;
196 second = first_sec;
197 }
198 None => {
199 day += 1;
200 hour = first_hr;
201 minute = first_min;
202 second = first_sec;
203 continue;
204 }
205 }
206 match next_set_bit(*minutes, minute) {
207 Some(next_min) if next_min == minute => {}
208 Some(next_min) => {
209 minute = next_min;
210 second = first_sec;
211 }
212 None => {
213 hour += 1;
214 minute = first_min;
215 second = first_sec;
216 continue;
217 }
218 }
219 match next_set_bit(*seconds, second) {
220 Some(next_s) => {
221 let candidate: UtcDateTime = UtcDateTime {
222 year,
223 month,
224 day,
225 weekday,
226 hour,
227 minute,
228 second: next_s,
229 };
230 return Some(candidate.to_unix_secs());
231 }
232 None => {
233 minute += 1;
234 second = first_sec;
235 }
236 }
237 }
238 None
239 }
240}
241
242impl FromStr for CronSchedule {
244 type Err = CronError;
245
246 fn from_str(s: &str) -> Result<Self, Self::Err> {
256 Self::parse(s)
257 }
258}