summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthewsot@outlook.com>2022-01-23 20:17:27 -0800
committerMatthew Sotoudeh <matthewsot@outlook.com>2022-01-23 20:18:11 -0800
commitd4839fe311a49ac2484b12487b8da6f0ae203dca (patch)
tree5abd4820ce089c462fde750905cae6bfed70e61a
parentf12dfa1375cc9bf88f4e8defc400b462e9d895aa (diff)
Supports reading from week, weekly files
-rw-r--r--src/dates.rs37
-rw-r--r--src/parse.rs14
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<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());
}
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<Task> {
- 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<File>`
@@ -22,18 +23,19 @@ pub fn tasks_from_path(wtd_path: &WTDPath) -> Vec<Task> {
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<Task> {
+fn parse_file_str(file_str: &str, implicit_week: Option<NaiveDate>) -> Vec<Task> {
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("## ") {
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback