Prepare eval error handling for cli arg errors
This commit is contained in:
parent
279bf4a4d1
commit
ff627b98df
8 changed files with 107 additions and 93 deletions
|
|
@ -216,7 +216,7 @@ impl<'a> CommandState<'a> {
|
|||
Ok(())
|
||||
} else {
|
||||
Err(Error::MoveWithoutSource {
|
||||
file: self.command.source.file(),
|
||||
index: self.command.source.file(),
|
||||
span,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -103,13 +103,13 @@ impl DateSpec {
|
|||
Some((start, skip, range))
|
||||
}
|
||||
|
||||
fn step(file: usize, from: NaiveDate, repeat: &Spanned<Delta>) -> Result<NaiveDate> {
|
||||
let to = repeat.value.apply_date(file, from)?;
|
||||
fn step(index: usize, from: NaiveDate, repeat: &Spanned<Delta>) -> Result<NaiveDate> {
|
||||
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<Dates> {
|
||||
let root = self.start_delta.apply_date(file, start)?;
|
||||
fn dates(&self, index: usize, start: NaiveDate) -> Result<Dates> {
|
||||
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(())
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ pub enum Var {
|
|||
}
|
||||
|
||||
impl Var {
|
||||
fn eval(self, file: usize, date: NaiveDate) -> Result<i64> {
|
||||
fn eval(self, index: usize, date: NaiveDate) -> Result<i64> {
|
||||
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<Weekday> for Expr {
|
|||
}
|
||||
|
||||
impl Expr {
|
||||
fn eval(&self, file: usize, date: NaiveDate) -> Result<i64> {
|
||||
fn eval(&self, index: usize, date: NaiveDate) -> Result<i64> {
|
||||
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<Dates> {
|
||||
let root = self.start_delta.apply_date(file, start)?;
|
||||
fn dates(&self, index: usize, start: NaiveDate) -> Result<Dates> {
|
||||
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<bool> {
|
||||
Ok(i2b(self.start.eval(file, date)?))
|
||||
fn eval(&self, index: usize, date: NaiveDate) -> Result<bool> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ impl From<&commands::Delta> for Delta {
|
|||
}
|
||||
|
||||
struct DeltaEval {
|
||||
file: usize,
|
||||
index: usize,
|
||||
start: NaiveDate,
|
||||
start_time: Option<Time>,
|
||||
curr: NaiveDate,
|
||||
|
|
@ -151,9 +151,9 @@ struct DeltaEval {
|
|||
}
|
||||
|
||||
impl DeltaEval {
|
||||
fn new(file: usize, start: NaiveDate, start_time: Option<Time>) -> Self {
|
||||
fn new(index: usize, start: NaiveDate, start_time: Option<Time>) -> Self {
|
||||
Self {
|
||||
file,
|
||||
index,
|
||||
start,
|
||||
start_time,
|
||||
curr: start,
|
||||
|
|
@ -163,7 +163,7 @@ impl DeltaEval {
|
|||
|
||||
fn err_step(&self, span: Span) -> Error {
|
||||
Error::DeltaInvalidStep {
|
||||
file: self.file,
|
||||
index: self.index,
|
||||
span,
|
||||
start: self.start,
|
||||
start_time: self.start_time,
|
||||
|
|
@ -174,7 +174,7 @@ impl DeltaEval {
|
|||
|
||||
fn err_time(&self, span: Span) -> Error {
|
||||
Error::DeltaNoTime {
|
||||
file: self.file,
|
||||
index: self.index,
|
||||
span,
|
||||
start: self.start,
|
||||
prev: self.curr,
|
||||
|
|
@ -315,27 +315,27 @@ impl Delta {
|
|||
|
||||
fn apply(
|
||||
&self,
|
||||
file: usize,
|
||||
index: usize,
|
||||
start: (NaiveDate, Option<Time>),
|
||||
) -> Result<(NaiveDate, Option<Time>)> {
|
||||
let mut eval = DeltaEval::new(file, start.0, start.1);
|
||||
let mut eval = DeltaEval::new(index, start.0, start.1);
|
||||
for step in &self.steps {
|
||||
eval.apply(step)?;
|
||||
}
|
||||
Ok((eval.curr, eval.curr_time))
|
||||
}
|
||||
|
||||
pub fn apply_date(&self, file: usize, date: NaiveDate) -> Result<NaiveDate> {
|
||||
Ok(self.apply(file, (date, None))?.0)
|
||||
pub fn apply_date(&self, index: usize, date: NaiveDate) -> Result<NaiveDate> {
|
||||
Ok(self.apply(index, (date, None))?.0)
|
||||
}
|
||||
|
||||
pub fn apply_date_time(
|
||||
&self,
|
||||
file: usize,
|
||||
index: usize,
|
||||
date: NaiveDate,
|
||||
time: Time,
|
||||
) -> Result<(NaiveDate, Time)> {
|
||||
let (date, time) = self.apply(file, (date, Some(time)))?;
|
||||
let (date, time) = self.apply(index, (date, Some(time)))?;
|
||||
Ok((date, time.expect("time was not preserved")))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,14 +3,13 @@ use std::result;
|
|||
use chrono::NaiveDate;
|
||||
|
||||
use crate::files::primitives::{Span, Time};
|
||||
use crate::files::Files;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
/// A delta step resulted in an invalid date.
|
||||
#[error("delta step resulted in invalid date")]
|
||||
DeltaInvalidStep {
|
||||
file: usize,
|
||||
index: usize,
|
||||
span: Span,
|
||||
start: NaiveDate,
|
||||
start_time: Option<Time>,
|
||||
|
|
@ -20,7 +19,7 @@ pub enum Error {
|
|||
/// A time-based delta step was applied to a date without time.
|
||||
#[error("time-based delta step applied to date without time")]
|
||||
DeltaNoTime {
|
||||
file: usize,
|
||||
index: usize,
|
||||
span: Span,
|
||||
start: NaiveDate,
|
||||
prev: NaiveDate,
|
||||
|
|
@ -30,7 +29,7 @@ pub enum Error {
|
|||
/// in time (`to < from`).
|
||||
#[error("repeat delta did not move forwards")]
|
||||
RepeatDidNotMoveForwards {
|
||||
file: usize,
|
||||
index: usize,
|
||||
span: Span,
|
||||
from: NaiveDate,
|
||||
to: NaiveDate,
|
||||
|
|
@ -38,38 +37,48 @@ pub enum Error {
|
|||
/// A `MOVE a TO b` statement was executed, but there was no entry at the
|
||||
/// date `a`.
|
||||
#[error("tried to move nonexisting entry")]
|
||||
MoveWithoutSource { file: usize, span: Span },
|
||||
MoveWithoutSource { index: usize, span: Span },
|
||||
/// A division by zero has occurred.
|
||||
#[error("tried to divide by zero")]
|
||||
DivByZero {
|
||||
file: usize,
|
||||
index: usize,
|
||||
span: Span,
|
||||
date: NaiveDate,
|
||||
},
|
||||
/// A modulo operation by zero has occurred.
|
||||
#[error("tried to modulo by zero")]
|
||||
ModByZero {
|
||||
file: usize,
|
||||
index: usize,
|
||||
span: Span,
|
||||
date: NaiveDate,
|
||||
},
|
||||
/// Easter calculation failed.
|
||||
#[error("easter calculation failed")]
|
||||
Easter {
|
||||
file: usize,
|
||||
index: usize,
|
||||
span: Span,
|
||||
date: NaiveDate,
|
||||
msg: &'static str,
|
||||
},
|
||||
}
|
||||
|
||||
pub struct SourceInfo<'a> {
|
||||
pub name: Option<String>,
|
||||
pub content: &'a str,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
fn print_at(files: &Files, file: &usize, span: &Span, message: String) {
|
||||
fn print_at<'a>(sources: &[SourceInfo<'a>], index: &usize, span: &Span, message: String) {
|
||||
use pest::error as pe;
|
||||
let (name, content) = files.file(*file).expect("file index is valid");
|
||||
let span = pest::Span::new(content, span.start, span.end).expect("span is valid");
|
||||
|
||||
let source = sources.get(*index).expect("index is valid");
|
||||
let span = pest::Span::new(source.content, span.start, span.end).expect("span is valid");
|
||||
let variant = pe::ErrorVariant::<()>::CustomError { message };
|
||||
let error = pe::Error::new_from_span(variant, span).with_path(&name.to_string_lossy());
|
||||
let mut error = pe::Error::new_from_span(variant, span);
|
||||
if let Some(name) = &source.name {
|
||||
error = error.with_path(name);
|
||||
}
|
||||
|
||||
eprintln!("{}", error);
|
||||
}
|
||||
|
||||
|
|
@ -80,10 +89,10 @@ impl Error {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn print(&self, files: &Files) {
|
||||
pub fn print<'a>(&self, sources: &[SourceInfo<'a>]) {
|
||||
match self {
|
||||
Error::DeltaInvalidStep {
|
||||
file,
|
||||
index,
|
||||
span,
|
||||
start,
|
||||
start_time,
|
||||
|
|
@ -97,10 +106,10 @@ impl Error {
|
|||
Self::fmt_date_time(*start, *start_time),
|
||||
Self::fmt_date_time(*prev, *prev_time),
|
||||
);
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
Error::DeltaNoTime {
|
||||
file,
|
||||
index,
|
||||
span,
|
||||
start,
|
||||
prev,
|
||||
|
|
@ -111,10 +120,10 @@ impl Error {
|
|||
\nPrevious step: {}",
|
||||
start, prev
|
||||
);
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
Error::RepeatDidNotMoveForwards {
|
||||
file,
|
||||
index,
|
||||
span,
|
||||
from,
|
||||
to,
|
||||
|
|
@ -124,30 +133,30 @@ impl Error {
|
|||
\nMoved from {} to {}",
|
||||
from, to
|
||||
);
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
Error::MoveWithoutSource { file, span } => {
|
||||
Error::MoveWithoutSource { index, span } => {
|
||||
let msg = "Tried to move nonexisting entry".to_string();
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
Error::DivByZero { file, span, date } => {
|
||||
Error::DivByZero { index, span, date } => {
|
||||
let msg = format!(
|
||||
"Tried to divide by zero\
|
||||
\nAt date: {}",
|
||||
date
|
||||
);
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
Error::ModByZero { file, span, date } => {
|
||||
Error::ModByZero { index, span, date } => {
|
||||
let msg = format!(
|
||||
"Tried to modulo by zero\
|
||||
\nAt date: {}",
|
||||
date
|
||||
);
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
Error::Easter {
|
||||
file,
|
||||
index,
|
||||
span,
|
||||
date,
|
||||
msg,
|
||||
|
|
@ -158,7 +167,7 @@ impl Error {
|
|||
\nReason: {}",
|
||||
date, msg
|
||||
);
|
||||
Self::print_at(files, file, span, msg);
|
||||
Self::print_at(sources, index, span, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue