Restructure eval module

This commit is contained in:
Joscha 2021-11-24 01:13:45 +01:00
parent 817732abf6
commit 35184e21e9
5 changed files with 208 additions and 199 deletions

View file

@ -1,99 +0,0 @@
use std::collections::HashMap;
use std::ops::RangeInclusive;
use chrono::{Datelike, NaiveDate};
use crate::files::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()
}
}

94
src/eval/entry.rs Normal file
View file

@ -0,0 +1,94 @@
use std::collections::HashMap;
use chrono::NaiveDate;
use crate::files::commands::Time;
use crate::files::Source;
use super::range::DateRange;
#[derive(Debug, PartialEq, Eq)]
pub enum EntryKind {
Task,
DoneTask,
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,
pub title: String,
pub desc: Vec<String>,
pub source: Source,
pub date: EntryDate,
}
pub struct EntryMap {
range: DateRange,
map: HashMap<NaiveDate, Option<Entry>>,
}
impl EntryMap {
pub fn new(range: DateRange) -> Self {
Self {
range,
map: HashMap::new(),
}
}
pub fn range(&self) -> DateRange {
self.range
}
pub fn block(&mut self, date: NaiveDate) {
if self.range.contains(date) {
self.map.entry(date).or_insert(None);
}
}
pub fn insert(&mut self, date: NaiveDate, entry: Entry) {
if self.range.contains(date) {
self.map.entry(date).or_insert(Some(entry));
}
}
pub fn drain(&mut self) -> Vec<Entry> {
self.map.drain().filter_map(|(_, entry)| entry).collect()
}
}

32
src/eval/range.rs Normal file
View file

@ -0,0 +1,32 @@
use std::ops::RangeInclusive;
use chrono::{Datelike, NaiveDate};
#[derive(Debug, Clone, Copy)]
pub struct DateRange {
from: NaiveDate,
until: NaiveDate,
}
impl DateRange {
pub fn new(from: NaiveDate, until: NaiveDate) -> Self {
assert!(from <= until);
Self { from, until }
}
pub fn contains(&self, date: NaiveDate) -> bool {
self.from <= date && date <= self.until
}
pub fn from(&self) -> NaiveDate {
self.from
}
pub fn until(&self) -> NaiveDate {
self.until
}
pub fn years(&self) -> RangeInclusive<i32> {
self.from.year()..=self.until.year()
}
}