Extract back link function, add two and three years ago links

This commit is contained in:
Thomas Gideon 2024-04-09 15:25:02 -04:00
parent a5016082fc
commit 1096dd1db8

View file

@ -138,30 +138,23 @@ async fn main() -> Result<()> {
.open(&output)
.with_context(|| format!("Failed to create {}", output))?;
file.write(format!("# {}\n\n", day.end.format("%Y-%m-%d")).as_bytes())?;
// TODO extract into a more general function
let week_ago = day.end - Duration::days(7);
// TODO check if the file exists
// TODO move to separate function
file.write(create_back_link(&day.end, "[One week ago](diary:{})\n", 7).as_bytes())?;
file.write(
format!("[One week ago](diary:{})\n", week_ago.format("%Y-%m-%d")).as_bytes(),
create_back_link(&day.end, "[One month ago](diary:{})\n", 30).as_bytes(),
)?;
let month_ago = day.end - Duration::days(30);
// TODO check if the file exists
file.write(
format!("[One month ago](diary:{})\n", month_ago.format("%Y-%m-%d")).as_bytes(),
create_back_link(&day.end, "[Six months ago](diary:{})\n", 6 * 30).as_bytes(),
)?;
let half_year_ago = day.end - Duration::days(6 * 30);
// TODO check if the file exists
file.write(
format!(
"[Six months ago](diary:{})\n",
half_year_ago.format("%Y-%m-%d")
)
.as_bytes(),
create_back_link(&day.end, "[One year ago](diary:{})\n", 365).as_bytes(),
)?;
let year_ago = day.end - Duration::days(365);
// TODO check if the file exists
file.write(
format!("[One year ago](diary:{})\n", year_ago.format("%Y-%m-%d")).as_bytes(),
create_back_link(&day.end, "[Two years ago](diary:{})\n", 365 * 2).as_bytes(),
)?;
file.write(
create_back_link(&day.end, "[Three years ago](diary:{})\n", 365 * 3).as_bytes(),
)?;
file
}
@ -175,6 +168,16 @@ async fn main() -> Result<()> {
Ok(())
}
fn create_back_link(day_end: &DateTime<Local>, anchor_text: &str, ago: i64) -> String {
let prior_date = *day_end - Duration::days(ago);
// TODO check if the file exists
format!(
"[{}](diary:{})\n",
anchor_text,
prior_date.format("%Y-%m-%d")
)
}
enum NextIter {
Skip,
Stop,