diff options
Diffstat (limited to 'src/dates.rs')
-rw-r--r-- | src/dates.rs | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/dates.rs b/src/dates.rs index fc3863c..c4c9c0d 100644 --- a/src/dates.rs +++ b/src/dates.rs @@ -3,24 +3,24 @@ use std::path::Path; // use chrono::{Datelike, NaiveDate, NaiveTime, Weekday, Duration, Timelike}; use chrono::{Datelike, NaiveDate, Duration}; -pub struct WTDPath<'a> { - pub path: &'a Path, +pub struct WTDPath { + pub path: String, pub week_start: Option<NaiveDate>, pub is_weekly_file: bool, } // both start, end are inclusive. -pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec<WTDPath<'a>> { +pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec<WTDPath> { let mut paths: Vec<WTDPath> = vec![]; - if let Some(path) = maybe_path("wtd.md".to_string()) { + if let Some(path) = path_if_exists("wtd.md") { paths.push(WTDPath { - path: &path, + path: path, week_start: None, is_weekly_file: false, }); } - let weekly = maybe_path("weekly.md".to_string()); + let weekly = path_if_exists("weekly.md"); let start_week = week_start_of(start); let end_week = week_start_of(end); // inclusive @@ -28,13 +28,16 @@ pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec<WTDPa let mut curr_week = start_week; while curr_week <= end_week { let maybe_week_path = week_file(curr_week); - let path = match [maybe_week_path, weekly] { - [Some(path), _] => path, - [_, Some(path)] => path, - [None, None] => continue, + let path = match [&maybe_week_path, &weekly] { + [Some(p), _] => p, + [_, Some(p)] => p, + [None, None] => { + curr_week = curr_week + Duration::weeks(1); + continue + }, }; paths.push(WTDPath { - path: path, + path: path.to_string(), week_start: Some(curr_week), is_weekly_file: maybe_week_path.is_none(), }); @@ -44,17 +47,17 @@ pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec<WTDPa return paths; } -fn week_file(week_start: NaiveDate) -> Option<&'static Path> { +fn week_file(week_start: NaiveDate) -> Option<String> { let filename = week_start.format("%b_%d_%Y.md").to_string().to_lowercase(); - return maybe_path(filename); + return path_if_exists(&filename); } -fn maybe_path(name: String) -> Option<&'static Path> { - let path = Path::new("wtd.md"); - return if path.exists() { Some(&path) } else { None }; +fn path_if_exists(name: &str) -> Option<String> { + return if Path::new(name).exists() { Some(name.to_string()) } else { None }; } fn week_start_of(date: &NaiveDate) -> NaiveDate { - let days_from_monday = date.weekday().number_from_monday(); + // number_from_monday() is 1-indexed, for some reason... + let days_from_monday = date.weekday().number_from_monday() - 1; return *date - Duration::days(days_from_monday.into()); } |