| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#}; |
| 2 | |
| 3 | use claprust-analyzer cargo clap 4.5.37 crate/::Parserrust-analyzer cargo clap_builder 4.5.37 derive/Parser#; |
| 4 | use rrulerust-analyzer cargo rrule 0.14.0 crate/::RRuleSetrust-analyzer cargo rrule 0.14.0 core/rruleset/RRuleSet#; |
| 5 | |
| 6 | /// Recurrence Rule parser and iterator |
| 7 | /// |
| 8 | /// This program expects a Recurrence Rule (RRULE) as defined by the |
| 9 | /// [iCalendar (RFC-5545) specification](https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/). |
| 10 | /// |
| 11 | /// Some `RRuleSet` examples of a valid iCalendar string: |
| 12 | /// |
| 13 | /// - `DTSTART:20120201T093000Z\nRRULE:FREQ=YEARLY` |
| 14 | /// - `DTSTART:20120201T093000Z\nRRULE:FREQ=WEEKLY;INTERVAL=5;BYDAY=MO,FR` |
| 15 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Parserrust-analyzer cargo clap_derive 4.5.32 Parser:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 16 | #[clap( |
| 17 | name = "rrule", |
| 18 | aboutrust-analyzer cargo clap_builder 4.5.37 builder/command/impl#[Command]about(). = "A parser and iterator for recurrence rules as defined in the iCalendar RFC." |
| 19 | )] |
| 20 | struct Optsrust-analyzer cargo rrule 0.14.0 Opts# { |
| 21 | /// Limits the amount of iteration |
| 22 | /// If no limit is set, it will default to `100`. |
| 23 | /// The maximum limit is `65535`. |
| 24 | #[claprust-analyzer cargo clap_derive 4.5.32 clap:(shortrust-analyzer cargo clap_builder 4.5.37 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.5.37 builder/arg/impl#[Arg]long().)] |
| 25 | limitrust-analyzer cargo rrule 0.14.0 Opts#limit.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16>, |
| 26 | |
| 27 | /// The `RRULE` string you want to iterator over. |
| 28 | inputrust-analyzer cargo rrule 0.14.0 Opts#input.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 29 | } |
| 30 | |
| 31 | fn mainrust-analyzer cargo rrule 0.14.0 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 32 | let opts: Optsrust-analyzer cargo rrule 0.14.0 Opts# = Parserrust-analyzer cargo clap_builder 4.5.37 derive/Parser#::parserust-analyzer cargo clap_builder 4.5.37 derive/Parser#parse().(); |
| 33 | |
| 34 | let limit = opts.limitrust-analyzer cargo rrule 0.14.0 Opts#limit..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(100); |
| 35 | let rrule_str = opts.inputrust-analyzer cargo rrule 0.14.0 Opts#input..replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().("\\n", "\n"); |
| 36 | let rrule: RRuleSetrust-analyzer cargo rrule 0.14.0 core/rruleset/RRuleSet# = parse_rulerust-analyzer cargo rrule 0.14.0 parse_rule().(&rrule_str)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 37 | let rrule = rrule.limitrust-analyzer cargo rrule 0.14.0 core/rruleset/impl#[RRuleSet]limit().(); |
| 38 | let iter = rrule.into_iterrust-analyzer cargo rrule 0.14.0 iter/rruleset_iter/impl#[`&RRuleSet`][IntoIterator]into_iter().(); |
| 39 | iterator_datesrust-analyzer cargo rrule 0.14.0 iterator_dates().(iter, limit); |
| 40 | |
| 41 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 42 | } |
| 43 | |
| 44 | fn parse_rulerust-analyzer cargo rrule 0.14.0 parse_rule().<R: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>(rrule_str: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<R, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> |
| 45 | where |
| 46 | <R as FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#>::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#: Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 47 | { |
| 48 | match rrule_str.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() { |
| 49 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(rrule) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(rrule), |
| 50 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => { |
| 51 | eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Error: {}", err); |
| 52 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#("Input string needs to be a valid rrule string.".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().()) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | fn iterator_datesrust-analyzer cargo rrule 0.14.0 iterator_dates().<T>(mut rule_iter: T, limit: u16) |
| 58 | where |
| 59 | T: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 60 | <T as Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#, |
| 61 | { |
| 62 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#limit { |
| 63 | let next = rule_iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().(); |
| 64 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(value) = next { |
| 65 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", value); |
| 66 | } |
| 67 | } |
| 68 | } |