From ff627b98dfcaf940c977da195b999d9d90ed623e Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 18 Dec 2021 23:31:27 +0100 Subject: [PATCH] Prepare eval error handling for cli arg errors --- src/cli.rs | 2 +- src/cli/error.rs | 7 ++-- src/eval/command.rs | 2 +- src/eval/command/date.rs | 26 +++++++-------- src/eval/command/formula.rs | 66 ++++++++++++++++++------------------- src/eval/delta.rs | 22 ++++++------- src/eval/error.rs | 63 ++++++++++++++++++++--------------- src/files.rs | 12 +++++-- 8 files changed, 107 insertions(+), 93 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 910cb94..8a66b96 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -139,7 +139,7 @@ pub fn run() { let range = find_range(&opt, now); if let Err(e) = run_command(&opt, &mut files, range, now) { - e.print(&files); + e.print(&files.sources()); process::exit(1); } diff --git a/src/cli/error.rs b/src/cli/error.rs index 8f15b7a..3bf054e 100644 --- a/src/cli/error.rs +++ b/src/cli/error.rs @@ -1,7 +1,6 @@ use std::result; -use crate::eval; -use crate::files::Files; +use crate::eval::{self, SourceInfo}; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -14,9 +13,9 @@ pub enum Error { } impl Error { - pub fn print(&self, files: &Files) { + pub fn print<'a>(&self, sources: &[SourceInfo<'a>]) { match self { - Error::Eval(e) => e.print(files), + Error::Eval(e) => e.print(sources), Error::NoSuchEntry(n) => eprintln!("No entry with number {}", n), Error::NotATask(ns) => { if ns.is_empty() { diff --git a/src/eval/command.rs b/src/eval/command.rs index 735fae9..6406c55 100644 --- a/src/eval/command.rs +++ b/src/eval/command.rs @@ -216,7 +216,7 @@ impl<'a> CommandState<'a> { Ok(()) } else { Err(Error::MoveWithoutSource { - file: self.command.source.file(), + index: self.command.source.file(), span, }) } diff --git a/src/eval/command/date.rs b/src/eval/command/date.rs index 1d00da9..9fe1292 100644 --- a/src/eval/command/date.rs +++ b/src/eval/command/date.rs @@ -103,13 +103,13 @@ impl DateSpec { Some((start, skip, range)) } - fn step(file: usize, from: NaiveDate, repeat: &Spanned) -> Result { - let to = repeat.value.apply_date(file, from)?; + fn step(index: usize, from: NaiveDate, repeat: &Spanned) -> Result { + let to = repeat.value.apply_date(index, from)?; if to > from { Ok(to) } else { Err(Error::RepeatDidNotMoveForwards { - file, + index, span: repeat.span, from, to, @@ -117,13 +117,13 @@ impl DateSpec { } } - fn dates(&self, file: usize, start: NaiveDate) -> Result { - let root = self.start_delta.apply_date(file, start)?; + fn dates(&self, index: usize, start: NaiveDate) -> Result { + let root = self.start_delta.apply_date(index, start)?; Ok(if let Some(root_time) = self.start_time { - let (other, other_time) = self.end_delta.apply_date_time(file, root, root_time)?; + let (other, other_time) = self.end_delta.apply_date_time(index, root, root_time)?; Dates::new_with_time(root, root_time, other, other_time) } else { - let other = self.end_delta.apply_date(file, root)?; + let other = self.end_delta.apply_date(index, root)?; Dates::new(root, other) }) } @@ -131,23 +131,23 @@ impl DateSpec { impl<'a> CommandState<'a> { pub fn eval_date_spec(&mut self, spec: DateSpec) -> Result<()> { - let file = self.command.source.file(); + let index = self.command.source.file(); if let Some(repeat) = &spec.repeat { if let Some((mut start, skip, range)) = spec.start_and_range(self) { if skip { - start = DateSpec::step(file, start, repeat)?; + start = DateSpec::step(index, start, repeat)?; } while start < range.from() { - start = DateSpec::step(file, start, repeat)?; + start = DateSpec::step(index, start, repeat)?; } while start <= range.until() { - let dates = spec.dates(file, start)?; + let dates = spec.dates(index, start)?; self.add(self.kind(), Some(dates)); - start = DateSpec::step(file, start, repeat)?; + start = DateSpec::step(index, start, repeat)?; } } } else { - let dates = spec.dates(file, spec.start)?; + let dates = spec.dates(index, spec.start)?; self.add(self.kind(), Some(dates)); } Ok(()) diff --git a/src/eval/command/formula.rs b/src/eval/command/formula.rs index 9f5b6de..3eab921 100644 --- a/src/eval/command/formula.rs +++ b/src/eval/command/formula.rs @@ -46,7 +46,7 @@ pub enum Var { } impl Var { - fn eval(self, file: usize, date: NaiveDate) -> Result { + fn eval(self, index: usize, date: NaiveDate) -> Result { Ok(match self { Var::JulianDay => date.num_days_from_ce().into(), Var::Year => date.year().into(), @@ -81,7 +81,7 @@ impl Var { } Var::Easter(span) => { let e = computus::gregorian(date.year()).map_err(|e| Error::Easter { - file, + index, span, date, msg: e, @@ -199,46 +199,46 @@ impl From for Expr { } impl Expr { - fn eval(&self, file: usize, date: NaiveDate) -> Result { + fn eval(&self, index: usize, date: NaiveDate) -> Result { Ok(match self { Expr::Lit(l) => *l, - Expr::Var(v) => v.eval(file, date)?, - Expr::Neg(e) => -e.eval(file, date)?, - Expr::Add(a, b) => a.eval(file, date)? + b.eval(file, date)?, - Expr::Sub(a, b) => a.eval(file, date)? - b.eval(file, date)?, - Expr::Mul(a, b) => a.eval(file, date)? * b.eval(file, date)?, + Expr::Var(v) => v.eval(index, date)?, + Expr::Neg(e) => -e.eval(index, date)?, + Expr::Add(a, b) => a.eval(index, date)? + b.eval(index, date)?, + Expr::Sub(a, b) => a.eval(index, date)? - b.eval(index, date)?, + Expr::Mul(a, b) => a.eval(index, date)? * b.eval(index, date)?, Expr::Div(a, b, span) => { - let b = b.eval(file, date)?; + let b = b.eval(index, date)?; if b == 0 { return Err(Error::DivByZero { - file, + index, span: *span, date, }); } - a.eval(file, date)?.div_euclid(b) + a.eval(index, date)?.div_euclid(b) } Expr::Mod(a, b, span) => { - let b = b.eval(file, date)?; + let b = b.eval(index, date)?; if b == 0 { return Err(Error::ModByZero { - file, + index, span: *span, date, }); } - a.eval(file, date)?.rem_euclid(b) + a.eval(index, date)?.rem_euclid(b) } - Expr::Eq(a, b) => b2i(a.eval(file, date)? == b.eval(file, date)?), - Expr::Neq(a, b) => b2i(a.eval(file, date)? != b.eval(file, date)?), - Expr::Lt(a, b) => b2i(a.eval(file, date)? < b.eval(file, date)?), - Expr::Lte(a, b) => b2i(a.eval(file, date)? <= b.eval(file, date)?), - Expr::Gt(a, b) => b2i(a.eval(file, date)? > b.eval(file, date)?), - Expr::Gte(a, b) => b2i(a.eval(file, date)? >= b.eval(file, date)?), - Expr::Not(e) => b2i(!i2b(e.eval(file, date)?)), - Expr::And(a, b) => b2i(i2b(a.eval(file, date)?) && i2b(b.eval(file, date)?)), - Expr::Or(a, b) => b2i(i2b(a.eval(file, date)?) || i2b(b.eval(file, date)?)), - Expr::Xor(a, b) => b2i(i2b(a.eval(file, date)?) ^ i2b(b.eval(file, date)?)), + Expr::Eq(a, b) => b2i(a.eval(index, date)? == b.eval(index, date)?), + Expr::Neq(a, b) => b2i(a.eval(index, date)? != b.eval(index, date)?), + Expr::Lt(a, b) => b2i(a.eval(index, date)? < b.eval(index, date)?), + Expr::Lte(a, b) => b2i(a.eval(index, date)? <= b.eval(index, date)?), + Expr::Gt(a, b) => b2i(a.eval(index, date)? > b.eval(index, date)?), + Expr::Gte(a, b) => b2i(a.eval(index, date)? >= b.eval(index, date)?), + Expr::Not(e) => b2i(!i2b(e.eval(index, date)?)), + Expr::And(a, b) => b2i(i2b(a.eval(index, date)?) && i2b(b.eval(index, date)?)), + Expr::Or(a, b) => b2i(i2b(a.eval(index, date)?) || i2b(b.eval(index, date)?)), + Expr::Xor(a, b) => b2i(i2b(a.eval(index, date)?) ^ i2b(b.eval(index, date)?)), }) } } @@ -341,29 +341,29 @@ impl FormulaSpec { s.limit_from_until(range) } - fn dates(&self, file: usize, start: NaiveDate) -> Result { - let root = self.start_delta.apply_date(file, start)?; + fn dates(&self, index: usize, start: NaiveDate) -> Result { + let root = self.start_delta.apply_date(index, start)?; Ok(if let Some(root_time) = self.start_time { - let (other, other_time) = self.end_delta.apply_date_time(file, root, root_time)?; + let (other, other_time) = self.end_delta.apply_date_time(index, root, root_time)?; Dates::new_with_time(root, root_time, other, other_time) } else { - let other = self.end_delta.apply_date(file, root)?; + let other = self.end_delta.apply_date(index, root)?; Dates::new(root, other) }) } - fn eval(&self, file: usize, date: NaiveDate) -> Result { - Ok(i2b(self.start.eval(file, date)?)) + fn eval(&self, index: usize, date: NaiveDate) -> Result { + Ok(i2b(self.start.eval(index, date)?)) } } impl<'a> CommandState<'a> { pub fn eval_formula_spec(&mut self, spec: FormulaSpec) -> Result<()> { if let Some(range) = spec.range(self) { - let file = self.command.source.file(); + let index = self.command.source.file(); for day in range.days() { - if spec.eval(file, day)? { - let dates = spec.dates(file, day)?; + if spec.eval(index, day)? { + let dates = spec.dates(index, day)?; self.add(self.kind(), Some(dates)); } } diff --git a/src/eval/delta.rs b/src/eval/delta.rs index 4185f2c..ecde59b 100644 --- a/src/eval/delta.rs +++ b/src/eval/delta.rs @@ -143,7 +143,7 @@ impl From<&commands::Delta> for Delta { } struct DeltaEval { - file: usize, + index: usize, start: NaiveDate, start_time: Option