Evaluate birthdays
This commit is contained in:
parent
48faf910fd
commit
f1ff3f7aaa
3 changed files with 242 additions and 1 deletions
130
src/eval.rs
Normal file
130
src/eval.rs
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
use std::collections::HashSet;
|
||||||
|
use std::result;
|
||||||
|
|
||||||
|
use chrono::{Datelike, NaiveDate};
|
||||||
|
|
||||||
|
use crate::commands::{Birthday, Command, File, Note, Spec, Task};
|
||||||
|
|
||||||
|
use self::entries::{DateRange, Entry, EntryKind, EntryMap};
|
||||||
|
|
||||||
|
pub mod entries;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("TODO")]
|
||||||
|
Todo,
|
||||||
|
}
|
||||||
|
|
||||||
|
type Result<T> = result::Result<T, Error>;
|
||||||
|
|
||||||
|
fn eval_spec(spec: &Spec, index: usize, map: &mut EntryMap) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_task(task: &Task, index: usize, range: DateRange) -> Result<Vec<Entry>> {
|
||||||
|
let mut map = EntryMap::new(range);
|
||||||
|
|
||||||
|
map.from = task.from;
|
||||||
|
map.until = task.until;
|
||||||
|
|
||||||
|
for date in &task.except {
|
||||||
|
map.block(*date);
|
||||||
|
}
|
||||||
|
|
||||||
|
for spec in &task.when {
|
||||||
|
eval_spec(spec, index, &mut map)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let done: HashSet<NaiveDate> = task
|
||||||
|
.done
|
||||||
|
.iter()
|
||||||
|
.filter_map(|done| done.refering_to)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
for (date, entry) in map.map.iter_mut() {
|
||||||
|
if let Some(entry) = entry {
|
||||||
|
if done.contains(date) {
|
||||||
|
entry.kind.done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(map.drain())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_note(task: &Note, index: usize, range: DateRange) -> Result<Vec<Entry>> {
|
||||||
|
let mut map = EntryMap::new(range);
|
||||||
|
|
||||||
|
map.from = task.from;
|
||||||
|
map.until = task.until;
|
||||||
|
|
||||||
|
for date in &task.except {
|
||||||
|
map.block(*date);
|
||||||
|
}
|
||||||
|
|
||||||
|
for spec in &task.when {
|
||||||
|
eval_spec(spec, index, &mut map)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(map.drain())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_birthday(bd: &Birthday, index: usize, range: DateRange) -> Result<Vec<Entry>> {
|
||||||
|
let mut map = EntryMap::new(range);
|
||||||
|
|
||||||
|
for year in range.years() {
|
||||||
|
if bd.when.year_known && year < bd.when.date.year() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let title = if bd.when.year_known {
|
||||||
|
let age = year - bd.when.date.year();
|
||||||
|
format!("{} ({})", bd.title, age)
|
||||||
|
} else {
|
||||||
|
bd.title.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
match bd.when.date.with_year(year) {
|
||||||
|
Some(date) => {
|
||||||
|
let mut entry = Entry::new(index, EntryKind::Birthday, title);
|
||||||
|
entry.start = Some(date);
|
||||||
|
map.insert(date, entry);
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// We must've hit a non-leapyear
|
||||||
|
assert_eq!(bd.when.date.month(), 2);
|
||||||
|
assert_eq!(bd.when.date.day(), 29);
|
||||||
|
|
||||||
|
let first_date = NaiveDate::from_ymd(year, 2, 28);
|
||||||
|
let first_title = format!("{} (first half)", title);
|
||||||
|
let mut first_entry = Entry::new(index, EntryKind::Birthday, first_title);
|
||||||
|
first_entry.start = Some(first_date);
|
||||||
|
map.insert(first_date, first_entry);
|
||||||
|
|
||||||
|
let second_date = NaiveDate::from_ymd(year, 3, 1);
|
||||||
|
let second_title = format!("{} (second half)", title);
|
||||||
|
let mut second_entry = Entry::new(index, EntryKind::Birthday, second_title);
|
||||||
|
second_entry.start = Some(second_date);
|
||||||
|
map.insert(second_date, second_entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(map.drain())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_command(command: &Command, index: usize, range: DateRange) -> Result<Vec<Entry>> {
|
||||||
|
match command {
|
||||||
|
Command::Task(task) => eval_task(task, index, range),
|
||||||
|
Command::Note(note) => eval_note(note, index, range),
|
||||||
|
Command::Birthday(birthday) => eval_birthday(birthday, index, range),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn eval(file: &File, range: DateRange) -> Result<Vec<Entry>> {
|
||||||
|
let mut result = vec![];
|
||||||
|
for (index, command) in file.commands.iter().enumerate() {
|
||||||
|
result.append(&mut eval_command(command, index, range)?);
|
||||||
|
}
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
100
src/eval/entries.rs
Normal file
100
src/eval/entries.rs
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::ops::RangeInclusive;
|
||||||
|
use std::str::from_boxed_utf8_unchecked;
|
||||||
|
|
||||||
|
use chrono::{Datelike, NaiveDate};
|
||||||
|
|
||||||
|
use crate::commands::Time;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum EntryKind {
|
||||||
|
Task,
|
||||||
|
DoneTask,
|
||||||
|
Note,
|
||||||
|
Birthday,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EntryKind {
|
||||||
|
pub fn done(&mut self) {
|
||||||
|
if matches!(self, Self::Task) {
|
||||||
|
*self = Self::DoneTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Entry {
|
||||||
|
pub kind: EntryKind,
|
||||||
|
pub title: String,
|
||||||
|
pub desc: Vec<String>,
|
||||||
|
|
||||||
|
/// Index in the source file
|
||||||
|
pub source: usize,
|
||||||
|
|
||||||
|
pub start: Option<NaiveDate>,
|
||||||
|
pub start_time: Option<Time>,
|
||||||
|
pub end: Option<NaiveDate>,
|
||||||
|
pub end_time: Option<Time>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Entry {
|
||||||
|
pub fn new(source: usize, kind: EntryKind, title: String) -> Self {
|
||||||
|
Self {
|
||||||
|
kind,
|
||||||
|
title,
|
||||||
|
desc: vec![],
|
||||||
|
source,
|
||||||
|
start: None,
|
||||||
|
start_time: None,
|
||||||
|
end: None,
|
||||||
|
end_time: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct DateRange {
|
||||||
|
pub from: NaiveDate,
|
||||||
|
pub until: NaiveDate,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DateRange {
|
||||||
|
pub fn new(from: NaiveDate, until: NaiveDate) -> Self {
|
||||||
|
assert!(from <= until);
|
||||||
|
Self { from, until }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn years(&self) -> RangeInclusive<i32> {
|
||||||
|
self.from.year()..=self.until.year()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EntryMap {
|
||||||
|
pub range: DateRange,
|
||||||
|
pub from: Option<NaiveDate>,
|
||||||
|
pub until: Option<NaiveDate>,
|
||||||
|
pub map: HashMap<NaiveDate, Option<Entry>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EntryMap {
|
||||||
|
pub fn new(range: DateRange) -> Self {
|
||||||
|
Self {
|
||||||
|
range,
|
||||||
|
from: None,
|
||||||
|
until: None,
|
||||||
|
map: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn block(&mut self, date: NaiveDate) {
|
||||||
|
self.map.entry(date).or_insert(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, date: NaiveDate, entry: Entry) {
|
||||||
|
self.map.entry(date).or_insert(Some(entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn drain(&mut self) -> Vec<Entry> {
|
||||||
|
self.map.drain().filter_map(|(_, entry)| entry).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/main.rs
13
src/main.rs
|
|
@ -1,9 +1,13 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use chrono::NaiveDate;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
use crate::eval::entries::DateRange;
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
|
mod eval;
|
||||||
mod format;
|
mod format;
|
||||||
mod parse;
|
mod parse;
|
||||||
|
|
||||||
|
|
@ -17,6 +21,13 @@ fn main() -> anyhow::Result<()> {
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
let content = fs::read_to_string(&opt.file)?;
|
let content = fs::read_to_string(&opt.file)?;
|
||||||
let file = parse::parse(&opt.file, &content)?;
|
let file = parse::parse(&opt.file, &content)?;
|
||||||
print!("{}", file);
|
let entries = eval::eval(
|
||||||
|
&file,
|
||||||
|
DateRange::new(
|
||||||
|
NaiveDate::from_ymd(2021, 1, 1),
|
||||||
|
NaiveDate::from_ymd(2021, 12, 31),
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
println!("{:#?}", entries);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue