summaryrefslogtreecommitdiff
path: root/src/rrule.rs
blob: 68f77adf2ee0aec9fb52422cc9ff493757c9f189 (plain)
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
use crate::iter::iter;
use crate::options::*;
use crate::rrule_iter::*;
use chrono::prelude::*;
use chrono_tz::Tz;

#[derive(Clone, Debug)]
pub struct RRule {
    pub options: ParsedOptions,
}

impl RRule {
    pub fn new(options: ParsedOptions) -> Self {
        Self { options }
    }

    /// Returns all the recurrences of the rrule
    pub fn all(&mut self) -> Vec<DateTime<Tz>> {
        let iter_args = IterArgs {
            inc: true,
            before: None,
            after: None,
            dt: None,
        };
        let mut iter_res = RRuleIterRes::new(QueryMethodTypes::All, iter_args);

        let res = iter(&mut iter_res, &mut self.options);
        res
    }
    
    /// Returns the last recurrence before the given datetime instance.
    /// The inc keyword defines what happens if dt is an recurrence.
    /// With inc == true, if dt itself is an recurrence, it will be returned.
    pub fn before(&mut self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> {
        let iter_args = IterArgs {
            inc,
            before: None,
            after: None,
            dt: Some(dt),
        };
        let mut iter_res = RRuleIterRes::new(QueryMethodTypes::Before, iter_args);

        let recurrences =  iter(&mut iter_res, &mut self.options);
        if recurrences.is_empty() {
            None
        } else {
            Some(recurrences[0])
        }
    }

    /// Returns the last recurrence after the given datetime instance.
    /// The inc keyword defines what happens if dt is an recurrence.
    /// With inc == true, if dt itself is an recurrence, it will be returned.
    pub fn after(&mut self, dt: DateTime<Tz>, inc: bool) -> Option<DateTime<Tz>> {
        let iter_args = IterArgs {
            inc,
            before: None,
            after: None,
            dt: Some(dt),
        };
        let mut iter_res = RRuleIterRes::new(QueryMethodTypes::After, iter_args);
        let recurrences =  iter(&mut iter_res, &mut self.options);
        if recurrences.is_empty() {
            None
        } else {
            Some(recurrences[0])
        }
    }

    /// Returns all the recurrences of the rrule between after and before.
    /// The inc keyword defines what happens if after and/or before are
    /// themselves recurrences. With inc == true, they will be included in the
    /// list, if they are found in the recurrence set.
    pub fn between(
        &mut self,
        after: DateTime<Tz>,
        before: DateTime<Tz>,
        inc: bool,
    ) -> Vec<DateTime<Tz>> {
        let iter_args = IterArgs {
            inc,
            before: Some(before),
            after: Some(after),
            dt: None,
        };
        let mut iter_res = RRuleIterRes::new(QueryMethodTypes::Between, iter_args);

        iter(&mut iter_res, &mut self.options)
    }
}