From d4839fe311a49ac2484b12487b8da6f0ae203dca Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Sun, 23 Jan 2022 20:17:27 -0800 Subject: Supports reading from week, weekly files --- src/dates.rs | 37 ++++++++++++++++++++----------------- src/parse.rs | 14 ++++++++------ 2 files changed, 28 insertions(+), 23 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, pub is_weekly_file: bool, } // both start, end are inclusive. -pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec> { +pub fn relevant_files<'a>(start: &'a NaiveDate, end: &'a NaiveDate) -> Vec { let mut paths: Vec = 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 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 Option<&'static Path> { +fn week_file(week_start: NaiveDate) -> Option { 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 { + 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()); } diff --git a/src/parse.rs b/src/parse.rs index a896cc2..d751579 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,3 +1,4 @@ +use std::path::Path; use std::fs::File; use std::io::prelude::*; use std::str::FromStr; @@ -9,7 +10,7 @@ use crate::html_calendar::*; // Translates a Path into a vector of tasks. Panics if the path cannot be opened or read from. pub fn tasks_from_path(wtd_path: &WTDPath) -> Vec { - let path = wtd_path.path; + let path = Path::new(&wtd_path.path); let display = path.display(); // Open the path in read-only mode, returns `io::Result` @@ -22,18 +23,19 @@ pub fn tasks_from_path(wtd_path: &WTDPath) -> Vec { let mut s = String::new(); match file.read_to_string(&mut s) { Err(why) => panic!("Couldn't read {}: {}", display, why), - Ok(_) => { - return parse_file_str(&s); - } + Ok(_) => return parse_file_str(&s, wtd_path.week_start), } } -fn parse_file_str(file_str: &str) -> Vec { +fn parse_file_str(file_str: &str, implicit_week: Option) -> Vec { let mut tasks = Vec::new(); - let mut start_date = None; + let mut start_date = implicit_week; let mut the_date = None; for l in file_str.split('\n') { if l.starts_with("# ") { + if implicit_week.is_some() { + panic!("Cannot use '# ' headers in a weekly.md or [week].md file!"); + } // '# 12/27/21', starts a new week block start_date = parse_date_line(l); } else if l.starts_with("## ") { -- cgit v1.2.3