Add eval representation of DateSpec

This commit is contained in:
Joscha 2021-11-26 00:53:27 +01:00
parent e827556c84
commit 1d80ab681e
3 changed files with 77 additions and 21 deletions

View file

@ -3,15 +3,14 @@ use std::result;
use chrono::{Datelike, NaiveDate}; use chrono::{Datelike, NaiveDate};
use crate::files::commands::DateSpec;
use crate::files::commands::{Birthday, Command, DoneDate, Note, Spec, Task}; use crate::files::commands::{Birthday, Command, DoneDate, Note, Spec, Task};
use crate::files::{Files, Source, SourcedCommand}; use crate::files::{Files, Source, SourcedCommand};
use self::entry::EntryMap; use self::entry::EntryMap;
pub use self::entry::{Entry, EntryKind}; pub use self::entry::{Entry, EntryKind};
use self::formula_spec::FormulaSpec;
pub use self::range::DateRange; pub use self::range::DateRange;
mod date_spec;
mod delta; mod delta;
mod entry; mod entry;
mod formula_spec; mod formula_spec;
@ -31,23 +30,6 @@ struct Eval {
} }
impl Eval { impl Eval {
fn eval_date_spec(
&mut self,
spec: &DateSpec,
last_done: Option<NaiveDate>,
new_entry: impl Fn(Source, Option<DoneDate>) -> Entry,
) -> Result<()> {
todo!()
}
fn eval_formula_spec(
&mut self,
spec: FormulaSpec,
new_entry: impl Fn(Source, Option<DoneDate>) -> Entry,
) -> Result<()> {
todo!()
}
fn eval_spec( fn eval_spec(
&mut self, &mut self,
spec: &Spec, spec: &Spec,
@ -55,7 +37,7 @@ impl Eval {
new_entry: impl Fn(Source, Option<DoneDate>) -> Entry, new_entry: impl Fn(Source, Option<DoneDate>) -> Entry,
) -> Result<()> { ) -> Result<()> {
match spec { match spec {
Spec::Date(spec) => self.eval_date_spec(spec, last_done, new_entry), Spec::Date(spec) => self.eval_date_spec(spec.into(), last_done, new_entry),
Spec::Weekday(spec) => self.eval_formula_spec(spec.into(), new_entry), Spec::Weekday(spec) => self.eval_formula_spec(spec.into(), new_entry),
Spec::Formula(spec) => self.eval_formula_spec(spec.into(), new_entry), Spec::Formula(spec) => self.eval_formula_spec(spec.into(), new_entry),
} }

62
src/eval/date_spec.rs Normal file
View file

@ -0,0 +1,62 @@
use chrono::NaiveDate;
use crate::files::commands::{self, DoneDate, Time};
use crate::files::Source;
use super::delta::{Delta, DeltaStep};
use super::{Entry, Eval, Result};
pub struct DateSpec {
pub start: NaiveDate,
pub start_delta: Delta,
pub start_time: Option<Time>,
pub end_delta: Delta,
pub repeat: Option<Delta>,
pub start_at_done: bool,
}
impl From<&commands::DateSpec> for DateSpec {
fn from(spec: &commands::DateSpec) -> Self {
let start_delta: Delta = spec
.start_delta
.as_ref()
.map(|delta| delta.into())
.unwrap_or_default();
let mut end_delta: Delta = spec
.end_delta
.as_ref()
.map(|delta| delta.into())
.unwrap_or_default();
if let Some(time) = spec.end_time {
end_delta.steps.push(DeltaStep::Time(time));
}
let repeat: Option<Delta> = spec.repeat.as_ref().map(|repeat| (&repeat.delta).into());
let start_at_done = spec
.repeat
.as_ref()
.map(|repeat| repeat.start_at_done)
.unwrap_or(false);
Self {
start: spec.start,
start_delta,
start_time: spec.start_time,
end_delta,
repeat,
start_at_done,
}
}
}
impl Eval {
pub fn eval_date_spec(
&mut self,
spec: DateSpec,
last_done: Option<NaiveDate>,
new_entry: impl Fn(Source, Option<DoneDate>) -> Entry,
) -> Result<()> {
todo!()
}
}

View file

@ -1,6 +1,8 @@
use crate::files::commands::{self, Expr, Time, Var}; use crate::files::commands::{self, DoneDate, Expr, Time, Var};
use crate::files::Source;
use super::delta::{Delta, DeltaStep}; use super::delta::{Delta, DeltaStep};
use super::{Entry, Eval, Result};
pub struct FormulaSpec { pub struct FormulaSpec {
// TODO Implement more efficient exprs and expr evaluation // TODO Implement more efficient exprs and expr evaluation
@ -66,3 +68,13 @@ impl From<&commands::WeekdaySpec> for FormulaSpec {
} }
} }
} }
impl Eval {
pub fn eval_formula_spec(
&mut self,
spec: FormulaSpec,
new_entry: impl Fn(Source, Option<DoneDate>) -> Entry,
) -> Result<()> {
todo!()
}
}