Implement basic structure for task evaluation

This commit is contained in:
Joscha 2021-11-25 01:24:16 +01:00
parent 11399468e7
commit 21739c7314
3 changed files with 146 additions and 82 deletions

View file

@ -2,53 +2,19 @@ use std::collections::HashMap;
use chrono::NaiveDate;
use crate::files::commands::Time;
use crate::files::commands::{DoneDate, Time};
use crate::files::Source;
use super::range::DateRange;
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntryKind {
Task,
DoneTask,
TaskDone(NaiveDate),
Note,
Birthday,
}
#[derive(Debug)]
pub enum EntryDate {
None,
Date {
root: NaiveDate,
},
DateWithTime {
root: NaiveDate,
root_time: Time,
},
DateToDate {
root: NaiveDate,
other: NaiveDate,
},
DateToDateWithTime {
root: NaiveDate,
root_time: Time,
other: NaiveDate,
other_time: Time,
},
}
impl EntryDate {
pub fn root(&self) -> Option<NaiveDate> {
match self {
EntryDate::None => None,
EntryDate::Date { root, .. } => Some(*root),
EntryDate::DateWithTime { root, .. } => Some(*root),
EntryDate::DateToDate { root, .. } => Some(*root),
EntryDate::DateToDateWithTime { root, .. } => Some(*root),
}
}
}
#[derive(Debug)]
pub struct Entry {
pub kind: EntryKind,
@ -56,11 +22,11 @@ pub struct Entry {
pub desc: Vec<String>,
pub source: Source,
pub date: EntryDate,
pub date: Option<DoneDate>,
}
pub struct EntryMap {
range: DateRange,
pub range: DateRange,
map: HashMap<NaiveDate, Option<Entry>>,
undated: Vec<Entry>,
}
@ -74,18 +40,6 @@ impl EntryMap {
}
}
pub fn range(&self) -> DateRange {
self.range
}
pub fn set_from(&mut self, from: NaiveDate) {
self.range = DateRange::new(from, self.range.until());
}
pub fn set_until(&mut self, until: NaiveDate) {
self.range = DateRange::new(self.range.from(), until);
}
pub fn block(&mut self, date: NaiveDate) {
if self.range.contains(date) {
self.map.entry(date).or_insert(None);
@ -93,9 +47,14 @@ impl EntryMap {
}
pub fn insert(&mut self, entry: Entry) {
if let Some(date) = entry.date.root() {
if let Some(date) = entry.date {
let date = date.root();
if self.range.contains(date) {
self.map.entry(date).or_insert(Some(entry));
} else if let EntryKind::TaskDone(done_date) = entry.kind {
if self.range.contains(done_date) {
self.map.entry(date).or_insert(Some(entry));
}
}
} else {
self.undated.push(entry);