Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! A performant rust implementation of recurrence rules as defined in the [iCalendar RFC](https://datatracker.ietf.org/doc/html/rfc5545).
2 //!
3 //! This crate provides [`RRuleSet`] for working with recurrence rules. It has a collection of `DTSTART`, `RRULE`s, `EXRULE`s, `RDATE`s and `EXDATE`s. Both the `RRULE` and `EXRULE`
4 //! properties are represented by the [`RRule`] type and the `DTSTART`, `RDATE` and `EXDATE` properties are represented by the [`chrono::DateTime<Tz>`].
5 //!
6 //! # Building `RRule` and `RRuleSet`
7 //! [`RRuleSet`] and [`RRule`] both implements the [`std::str::FromStr`] trait so that it can be parsed and built from a string representation.
8 //! [`RRuleSet`] can also be built by composing multiple [`RRule`]s for its `rrule` and `exrule` properties and [`chrono::DateTime<Tz>`] for its
9 //! `dt_start`, `exdate` and `rdate` properties. See the examples below.
10 //!
11 //! ```rust
12 //! use chrono::{DateTime, TimeZone};
13 //! use rrule::{RRuleSet, RRule, Tz, Unvalidated, Frequency};
14 //!
15 //! // Parse a single RRule string. Useful when you don't have a start date yet or
16 //! // just want to check if the input string is grammatically correct.
17 //! let rrule: RRule<Unvalidated> = "FREQ=DAILY;COUNT=40;INTERVAL=3".parse().unwrap();
18 //! assert_eq!(rrule.get_freq(), Frequency::Daily);
19 //! assert_eq!(rrule.get_count(), Some(40));
20 //! assert_eq!(rrule.get_interval(), 3);
21 //!
22 //! // Parse a RRuleSet string
23 //! let rrule_set: RRuleSet = "DTSTART:20120201T023000Z\n\
24 //! RRULE:FREQ=MONTHLY;COUNT=5\n\
25 //! RDATE:20120701T023000Z,20120702T023000Z\n\
26 //! EXDATE:20120601T023000Z".parse().unwrap();
27 //!
28 //! assert_eq!(*rrule_set.get_dt_start(), Tz::UTC.with_ymd_and_hms(2012, 2, 1,2, 30, 0).unwrap());
29 //! assert_eq!(rrule_set.get_rrule().len(), 1);
30 //! assert_eq!(rrule_set.get_rdate().len(), 2);
31 //! assert_eq!(rrule_set.get_exdate().len(), 1);
32 //!
33 //! // Add an rrule manually
34 //! let rrule = rrule.validate(*rrule_set.get_dt_start()).unwrap();
35 //! let rrule_set = rrule_set.rrule(rrule);
36 //! assert_eq!(rrule_set.get_rrule().len(), 2);
37 //! ```
38 //!
39 //! # Generating occurrences
40 //! You can loop over the occurrences of a [`RRuleSet`] by calling any of the following methods:
41 //! - [`RRuleSet::all`]: Generate all recurrences that match the rules (with a limit to prevent infinite loops).
42 //! - [`RRuleSet::all_unchecked`]: Generate all recurrences that match the rules (without a limit).
43 //! - ...
44 //!
45 //! If you have some additional filters or want to work with infinite recurrence rules
46 //! [`RRuleSet`] implements the `IntoIterator` trait which allows for very flexible queries.
47 //! All the methods above use the iterator trait in its implementation as shown below.
48 //! ```rust
49 //! use chrono::{DateTime, TimeZone};
50 //! use rrule::{RRuleSet, Tz};
51 //!
52 //! let rrule: RRuleSet = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3".parse().unwrap();
53 //! let result = rrule.all(100);
54 //!
55 //! // All dates
56 //! assert_eq!(
57 //! vec![
58 //! DateTime::parse_from_rfc3339("2012-02-01T09:30:00+00:00").unwrap(),
59 //! DateTime::parse_from_rfc3339("2012-02-02T09:30:00+00:00").unwrap(),
60 //! DateTime::parse_from_rfc3339("2012-02-03T09:30:00+00:00").unwrap(),
61 //! ],
62 //! result.dates
63 //! );
64 //! ```
65 //! Find all events that are within a given range.
66 //! ```rust
67 //! use chrono::{DateTime, TimeZone};
68 //! use rrule::{RRuleSet, Tz};
69 //!
70 //! let rrule: RRuleSet = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3".parse().unwrap();
71 //!
72 //! // Between two dates
73 //! let after = Tz::UTC.with_ymd_and_hms(2012, 2, 1,10, 0, 0).unwrap();
74 //! let before = Tz::UTC.with_ymd_and_hms(2012, 4, 1,9, 0, 0).unwrap();
75 //!
76 //! let rrule = rrule.after(after).before(before);
77 //! let result = rrule.all(100);
78 //!
79 //! assert_eq!(
80 //! vec![
81 //! DateTime::parse_from_rfc3339("2012-02-02T09:30:00+00:00").unwrap(),
82 //! DateTime::parse_from_rfc3339("2012-02-03T09:30:00+00:00").unwrap(),
83 //! ],
84 //! result.dates
85 //! );
86 //! ```
87 //!
88 //! Note: All the generated recurrence will be in the same time zone as the `dt_start` property.
89 //!
90
91 #![forbid(unsafe_code)]
92 #![deny(clippy::all)]
93 #![warn(missing_docs)]
94 #![deny(rustdoc::broken_intra_doc_links)]
95
96 mod core;
97 mod error;
98 mod iter;
99 mod parser;
100 mod tests;
101 mod validator;
102
103 pub use crate::core::{Frequency, NWeekday, RRule, RRuleResult, RRuleSet, Tz};
104 pub use crate::core::{Unvalidated, Validated};
105 pub use chrono::Weekday;
106 pub use error::{ParseError, RRuleError, ValidationError};
107 pub use iter::RRuleSetIter;