routers_codec 0.1.3

Encoding and Decoding Primitives for Routers
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
use alloc::fmt;
use core::fmt::{Display, Formatter};
use either::{Left, Right};
use itertools::Itertools;
use serde::Serialize;
use strum::{Display, EnumString};

#[derive(Debug, Clone, PartialEq, Display, EnumString, Serialize)]
pub enum Weekday {
    #[strum(serialize = "Mo")]
    Monday,
    #[strum(serialize = "Tu")]
    Tuesday,
    #[strum(serialize = "We")]
    Wednesday,
    #[strum(serialize = "Th")]
    Thursday,
    #[strum(serialize = "Fr")]
    Friday,
    #[strum(serialize = "Sa")]
    Saturday,
    #[strum(serialize = "Su")]
    Sunday,
}

impl Weekday {
    fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "mo" | "monday" => Some(Weekday::Monday),
            "tu" | "tuesday" => Some(Weekday::Tuesday),
            "we" | "wednesday" => Some(Weekday::Wednesday),
            "th" | "thursday" => Some(Weekday::Thursday),
            "fr" | "friday" => Some(Weekday::Friday),
            "sa" | "saturday" => Some(Weekday::Saturday),
            "su" | "sunday" => Some(Weekday::Sunday),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct Time {
    pub hour: u8,
    pub minute: u8,
}

impl Time {
    fn new(hour: u8, minute: u8) -> Result<Self, String> {
        if hour > 24 || minute > 59 {
            Err("Invalid time".to_string())
        } else {
            Ok(Time { hour, minute })
        }
    }
}

impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:02}:{:02}", self.hour, self.minute)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct TimeRange {
    pub start: Time,
    pub end: Time,
}

impl Display for TimeRange {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}-{}", self.start, self.end)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct TimeOfWeek {
    time: Time,
    weekday: Weekday,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum WeekdayRange {
    Single(Weekday),
    Range(Weekday, Weekday),
    List(Vec<Weekday>),
}

impl Display for WeekdayRange {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            WeekdayRange::Single(weekday) => {
                write!(f, "{weekday}")
            }
            WeekdayRange::Range(start, end) => {
                write!(f, "{start}-{end}")
            }
            WeekdayRange::List(weekdays) => {
                write!(
                    f,
                    "{}",
                    weekdays.iter().map(|weekday| weekday.to_string()).join(",")
                )
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OpeningRule {
    pub weekdays: Option<WeekdayRange>,
    pub times: Vec<TimeRange>,
    pub closed: bool,
}

impl Display for OpeningRule {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(weekday) = &self.weekdays {
            write!(f, "{weekday}")?;
        }

        let times = self.times.iter().map(|s| s.to_string()).join(",");
        write!(f, "{times}")
    }
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OpeningHours {
    pub rules: Vec<OpeningRule>,
}

impl Display for OpeningHours {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            self.rules.iter().map(|rule| rule.to_string()).join(";")
        )
    }
}

pub struct OpeningHoursParser;

impl OpeningHoursParser {
    pub fn parse(input: &str) -> Result<OpeningHours, String> {
        let input = input.trim();

        // Handle special cases
        if input.eq_ignore_ascii_case("24/7") {
            return Ok(OpeningHours {
                rules: vec![OpeningRule {
                    weekdays: None,
                    times: vec![TimeRange {
                        start: Time::new(0, 0)?,
                        end: Time::new(23, 59)?,
                    }],
                    closed: false,
                }],
            });
        }

        let mut rules = Vec::new();
        let rule_parts: Vec<&str> = input.split(';').collect();

        for rule_part in rule_parts {
            let rule = OpeningHoursParser::parse_rule(rule_part.trim())?;
            rules.push(rule);
        }

        if rules.is_empty() {
            return Err("No rules".to_string());
        }

        Ok(OpeningHours { rules })
    }

    fn parse_rule(rule: &str) -> Result<OpeningRule, String> {
        let rule = rule.trim();

        // Check if it's a closed rule
        if rule.eq_ignore_ascii_case("closed") || rule.eq_ignore_ascii_case("off") {
            return Ok(OpeningRule {
                weekdays: None,
                times: Vec::new(),
                closed: true,
            });
        }

        // Split by space to separate weekdays from times
        let parts: Vec<&str> = rule.split_whitespace().collect();

        if parts.is_empty() {
            return Err("Empty rule".to_string());
        }

        let (time_parts, weekday_parts): (Vec<_>, Vec<_>) = parts
            .iter()
            .scan(false, |parsing_times, part| {
                *parsing_times |= OpeningHoursParser::looks_like_time(part);
                Some((part, *parsing_times))
            })
            .partition_map(|(part, is_time)| if is_time { Left(*part) } else { Right(*part) });

        let weekdays = weekday_parts
            .into_iter()
            .map(OpeningHoursParser::parse_weekday_range)
            .find_map(|val| val.ok());

        // If no weekdays specified, apply to all days
        let times = if time_parts.is_empty() {
            Vec::new()
        } else {
            OpeningHoursParser::parse_time_ranges(&time_parts.join(" "))?
        };

        if weekdays.is_none() && times.is_empty() {
            return Err("No applicable values parsed".to_string());
        }

        Ok(OpeningRule {
            weekdays,
            times,
            closed: false,
        })
    }

    fn looks_like_time(s: &str) -> bool {
        s.contains(':') && s.len() >= 3
    }

    fn parse_weekday_range(input: &str) -> Result<WeekdayRange, String> {
        if input.contains('-') {
            let parts: Vec<&str> = input.split('-').collect();
            if parts.len() != 2 {
                return Err("Invalid weekday range".to_string());
            }
            let start = Weekday::from_str(parts[0]).ok_or("Invalid start weekday")?;
            let end = Weekday::from_str(parts[1]).ok_or("Invalid end weekday")?;
            Ok(WeekdayRange::Range(start, end))
        } else if input.contains(',') {
            let parts: Vec<&str> = input.split(',').collect();
            let mut weekdays = Vec::new();
            for part in parts {
                let weekday = Weekday::from_str(part.trim()).ok_or("Invalid weekday in list")?;
                weekdays.push(weekday);
            }
            Ok(WeekdayRange::List(weekdays))
        } else {
            let weekday = Weekday::from_str(input).ok_or("Invalid weekday")?;
            Ok(WeekdayRange::Single(weekday))
        }
    }

    fn parse_time_ranges(input: &str) -> Result<Vec<TimeRange>, String> {
        let mut ranges = Vec::new();

        // Split by comma for multiple time ranges
        let range_parts: Vec<&str> = input.split(',').collect();

        for range_part in range_parts {
            let range_part = range_part.trim();

            if range_part.contains('-') {
                let parts: Vec<&str> = range_part.split('-').collect();
                if parts.len() != 2 {
                    return Err("Invalid time range format".to_string());
                }

                let start_time = OpeningHoursParser::parse_time(parts[0].trim())?;
                let end_time = OpeningHoursParser::parse_time(parts[1].trim())?;

                ranges.push(TimeRange {
                    start: start_time,
                    end: end_time,
                });
            } else {
                // Single time point - treat as start time with end time one hour later
                let time = OpeningHoursParser::parse_time(range_part)?;
                let end_hour = if time.hour == 23 { 0 } else { time.hour + 1 };
                ranges.push(TimeRange {
                    start: time,
                    end: Time::new(end_hour, time.minute)?,
                });
            }
        }

        Ok(ranges)
    }

    fn parse_time(input: &str) -> Result<Time, String> {
        let input = input.trim();

        if input.contains(':') {
            let parts: Vec<&str> = input.split(':').collect();
            if parts.len() != 2 {
                return Err("Invalid time format".to_string());
            }

            let hour: u8 = parts[0].parse().map_err(|_| "Invalid hour")?;
            let minute: u8 = parts[1].parse().map_err(|_| "Invalid minute")?;

            Time::new(hour, minute)
        } else {
            // Assume it's just hours
            let hour: u8 = input.parse().map_err(|_| "Invalid hour")?;
            Time::new(hour, 0)
        }
    }
}

// Utility functions for working with parsed opening hours
impl OpeningHours {
    pub fn is_open_at(&self, TimeOfWeek { time, weekday }: &TimeOfWeek) -> bool {
        for rule in &self.rules {
            if rule.closed {
                continue;
            }

            // Check if this rule applies to the given weekday
            let applies_to_weekday = match &rule.weekdays {
                None => true, // No weekday restriction means all days
                Some(WeekdayRange::Single(day)) => day == weekday,
                Some(WeekdayRange::List(days)) => days.contains(weekday),
                Some(WeekdayRange::Range(_start, _end)) => {
                    // This is simplified - proper range checking would need day ordering
                    true // For now, assume it matches
                }
            };

            if applies_to_weekday {
                for time_range in &rule.times {
                    if self.time_in_range(time, &time_range.start, &time_range.end) {
                        return true;
                    }
                }
            }
        }
        false
    }

    fn time_in_range(&self, time: &Time, start: &Time, end: &Time) -> bool {
        let time_minutes = time.hour as u16 * 60 + time.minute as u16;
        let start_minutes = start.hour as u16 * 60 + start.minute as u16;
        let end_minutes = end.hour as u16 * 60 + end.minute as u16;

        if start_minutes <= end_minutes {
            time_minutes >= start_minutes && time_minutes <= end_minutes
        } else {
            // Handle overnight ranges
            time_minutes >= start_minutes || time_minutes <= end_minutes
        }
    }
}

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

    #[test]
    fn test_24_7() {
        let result = OpeningHoursParser::parse("24/7").unwrap();
        assert_eq!(result.rules.len(), 1);
        assert!(!result.rules[0].closed);
    }

    #[test]
    fn test_simple_time_range() {
        let result = OpeningHoursParser::parse("09:00-17:00").unwrap();
        assert_eq!(result.rules.len(), 1);
        assert_eq!(result.rules[0].times.len(), 1);
        assert_eq!(result.rules[0].times[0].start.hour, 9);
        assert_eq!(result.rules[0].times[0].end.hour, 17);
    }

    #[test]
    fn test_multiple_hours() {
        let result = OpeningHoursParser::parse("Mo-Fr 07:00-9:00,16:00-20:00").unwrap();
        assert_eq!(result.rules.len(), 1);
        assert!(matches!(
            result.rules[0].clone().weekdays.unwrap(),
            WeekdayRange::Range(Weekday::Monday, Weekday::Friday)
        ));

        assert_eq!(result.rules[0].times[0].start.hour, 7);
        assert_eq!(result.rules[0].times[0].end.hour, 9);

        assert_eq!(result.rules[0].times[1].start.hour, 16);
        assert_eq!(result.rules[0].times[1].end.hour, 20);
    }

    #[test]
    fn test_weekday_with_time() {
        let result = OpeningHoursParser::parse("Mo-Fr 09:00-17:00").unwrap();
        assert_eq!(result.rules.len(), 1);
        assert!(result.rules[0].weekdays.is_some());
    }

    #[test]
    fn test_multiple_rules() {
        let result = OpeningHoursParser::parse("Mo-Fr 09:00-17:00; Sa 10:00-14:00").unwrap();
        assert_eq!(result.rules.len(), 2);
    }

    #[test]
    fn test_closed() {
        let result = OpeningHoursParser::parse("closed").unwrap();
        assert_eq!(result.rules.len(), 1);
        assert!(result.rules[0].closed);
    }

    #[test]
    fn test_is_open_at() {
        let hours = OpeningHoursParser::parse("Mo-Fr 09:00-17:00").unwrap();

        let monday_noon = Time::new(12, 0).unwrap();
        assert!(hours.is_open_at(&TimeOfWeek {
            weekday: Weekday::Monday,
            time: monday_noon
        }));

        let monday_early = Time::new(8, 0).unwrap();
        assert!(!hours.is_open_at(&TimeOfWeek {
            weekday: Weekday::Monday,
            time: monday_early
        }));
    }
}