aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Pollan2021-09-09 13:26:56 +0200
committerRuben Pollan2021-09-09 13:26:56 +0200
commita66cfcac58ae8c9662776741d9727dfb6585fbcc (patch)
tree0d44e237464ddc63ddc224b635358c91b10373e1
parentd5e3f5dd9902dfd57f19e634964b481681e10b28 (diff)
WIP: byday ocurrences
Diffstat (limited to '')
-rw-r--r--src/periodic.rs27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/periodic.rs b/src/periodic.rs
index b557ef7..19874e4 100644
--- a/src/periodic.rs
+++ b/src/periodic.rs
@@ -1,6 +1,6 @@
use std::fmt;
use std::str::FromStr;
-use std::collections::HashSet;
+use std::collections::HashMap;
use chrono::{Duration, Weekday};
@@ -8,6 +8,8 @@ use date::Date;
use event::{Event, End};
use errors::EventError;
+pub type Byday = HashMap<Weekday, Vec<i32>>;
+
#[derive(Debug)]
pub struct Periodic {
pub event: Event,
@@ -15,7 +17,7 @@ pub struct Periodic {
pub interval: i64,
pub count: Option<i64>,
pub until: Option<Date>,
- pub byday: Option<HashSet<Weekday>>,
+ pub byday: Option<Byday>,
pub wkst: Weekday,
}
@@ -115,7 +117,7 @@ impl<'a> Iter<'a> {
if weekday == p.wkst {
days += 7 * (p.interval - 1);
}
- while !byday.contains(&weekday) {
+ while !byday.contains_key(&weekday) {
weekday = weekday.succ();
days += 1;
if weekday == p.wkst {
@@ -127,6 +129,7 @@ impl<'a> Iter<'a> {
}
}
Freq::Monthly => {
+ // TODO: byday...
let new_date = if date.month() == 12 {
date.with_month(1)
.unwrap()
@@ -137,6 +140,7 @@ impl<'a> Iter<'a> {
};
new_date - date
}
+ // TODO: byday...
Freq::Yearly => date.with_year(date.year() + 1).unwrap() - date,
}
}
@@ -170,10 +174,21 @@ impl FromStr for Freq {
}
}
-fn parse_byday(s: &str) -> Result<HashSet<Weekday>, EventError> {
- let mut byday = HashSet::new();
+fn parse_byday(s: &str) -> Result<Byday, EventError> {
+ let mut byday = Byday::new();
for v in s.split(",") {
- byday.insert(parse_weekday(v)?);
+ let weekday = parse_weekday(&v[v.len() - 2..])?;
+ let occurrence = if v.len() > 2 {
+ v[..v.len() - 2].parse()?
+ } else {
+ 0
+ };
+ match byday.get_mut(&weekday) {
+ Some(occurrences) => occurrences.push(occurrence),
+ None => {
+ byday.insert(weekday, vec![occurrence]);
+ }
+ };
}
Ok(byday)
}