1
use crate::convert::ToModel;
2
use crate::model::property::RecurrenceRule;
3
use crate::parser::types::RecurRulePart;
4
use anyhow::Context;
5

            
6
impl ToModel for Vec<RecurRulePart> {
7
    type Model = RecurrenceRule;
8

            
9
204
    fn to_model(&self) -> anyhow::Result<Self::Model> {
10
204
        let mut rule = RecurrenceRule::empty_with_capacity(self.len());
11
868
        for part in self.iter() {
12
868
            match part {
13
204
                RecurRulePart::Freq(f) => {
14
204
                    rule = rule.set_freq(f.clone());
15
204
                }
16
64
                RecurRulePart::Until(date_time) => {
17
64
                    let dt = date_time.to_model()?;
18
64
                    rule = rule.set_until(dt);
19
                }
20
40
                RecurRulePart::Count(count) => {
21
40
                    rule = rule.set_count(*count);
22
40
                }
23
40
                RecurRulePart::Interval(interval) => {
24
40
                    rule = rule.set_interval(*interval);
25
40
                }
26
42
                RecurRulePart::BySecList(by_sec_list) => {
27
42
                    rule = rule.set_by_second(by_sec_list.clone());
28
42
                }
29
42
                RecurRulePart::ByMinute(by_minute) => {
30
42
                    rule = rule.set_by_minute(by_minute.clone());
31
42
                }
32
42
                RecurRulePart::ByHour(by_hour) => {
33
42
                    rule = rule.set_by_hour(by_hour.clone());
34
42
                }
35
104
                RecurRulePart::ByDay(by_day) => {
36
104
                    rule = rule.set_by_day(by_day.clone());
37
104
                }
38
42
                RecurRulePart::ByMonthDay(by_month_day) => {
39
42
                    rule = rule.set_by_month_day(by_month_day.clone());
40
42
                }
41
46
                RecurRulePart::ByYearDay(by_year_day) => {
42
46
                    rule = rule.set_by_year_day(by_year_day.clone());
43
46
                }
44
46
                RecurRulePart::ByWeek(week) => {
45
46
                    rule = rule.set_by_week_number(week.clone());
46
46
                }
47
66
                RecurRulePart::ByMonth(month) => {
48
66
                    rule = rule.set_by_month(
49
66
                        month
50
66
                            .iter()
51
149
                            .map(|m| time::Month::try_from(*m).context("Invalid month"))
52
66
                            .collect::<anyhow::Result<Vec<_>>>()?,
53
                    );
54
                }
55
44
                RecurRulePart::BySetPos(by_set_pos) => {
56
44
                    rule = rule.set_by_set_pos(by_set_pos.clone());
57
44
                }
58
46
                RecurRulePart::WeekStart(week_start) => {
59
46
                    rule = rule.set_week_start(week_start.clone());
60
46
                }
61
            }
62
        }
63

            
64
204
        Ok(rule)
65
204
    }
66
}