Error when moving untimed entry to new time

This commit is contained in:
Joscha 2021-12-21 19:33:55 +01:00
parent 1ac39c69f2
commit 73a44a697a
4 changed files with 18 additions and 4 deletions

View file

@ -244,6 +244,7 @@ impl<'a> CommandState<'a> {
} }
fn eval_except(&mut self, date: NaiveDate) { fn eval_except(&mut self, date: NaiveDate) {
// TODO Error if nothing is removed?
self.dated.remove(&date); self.dated.remove(&date);
} }
@ -252,7 +253,7 @@ impl<'a> CommandState<'a> {
span: Span, span: Span,
from: NaiveDate, from: NaiveDate,
to: Option<NaiveDate>, to: Option<NaiveDate>,
to_time: Option<Time>, to_time: Option<Spanned<Time>>,
) -> Result<()> { ) -> Result<()> {
if let Some(mut entry) = self.dated.remove(&from) { if let Some(mut entry) = self.dated.remove(&from) {
let mut dates = entry.dates.expect("comes from self.dated"); let mut dates = entry.dates.expect("comes from self.dated");
@ -264,7 +265,12 @@ impl<'a> CommandState<'a> {
} }
if let Some(to_time) = to_time { if let Some(to_time) = to_time {
if let Some((root, _)) = dates.times() { if let Some((root, _)) = dates.times() {
delta = delta + Duration::minutes(root.minutes_to(to_time)); delta = delta + Duration::minutes(root.minutes_to(to_time.value));
} else {
return Err(Error::TimedMoveWithoutTime {
index: self.command.source.file(),
span: to_time.span,
});
} }
} }

View file

@ -48,6 +48,10 @@ pub enum Error {
/// date `a`. /// date `a`.
#[error("tried to move nonexisting entry")] #[error("tried to move nonexisting entry")]
MoveWithoutSource { index: usize, span: Span }, MoveWithoutSource { index: usize, span: Span },
/// A `MOVE a TO b` statement was executed where `b` contains a time but `a`
/// doesn't was executed.
#[error("tried to move un-timed entry to new time")]
TimedMoveWithoutTime { index: usize, span: Span },
/// A division by zero has occurred. /// A division by zero has occurred.
#[error("tried to divide by zero")] #[error("tried to divide by zero")]
DivByZero { DivByZero {
@ -162,6 +166,10 @@ impl Error {
let msg = "Tried to move nonexisting entry".to_string(); let msg = "Tried to move nonexisting entry".to_string();
Self::print_at(sources, index, span, msg); Self::print_at(sources, index, span, msg);
} }
Error::TimedMoveWithoutTime { index, span } => {
let msg = "Tried to move un-timed entry to new time".to_string();
Self::print_at(sources, index, span, msg);
}
Error::DivByZero { index, span, date } => { Error::DivByZero { index, span, date } => {
let msg = format!( let msg = format!(
"Tried to divide by zero\ "Tried to divide by zero\

View file

@ -266,7 +266,7 @@ pub enum Statement {
span: Span, span: Span,
from: NaiveDate, from: NaiveDate,
to: Option<NaiveDate>, to: Option<NaiveDate>,
to_time: Option<Time>, to_time: Option<Spanned<Time>>,
}, },
Remind(Option<Spanned<Delta>>), Remind(Option<Spanned<Delta>>),
} }

View file

@ -624,7 +624,7 @@ fn parse_stmt_move(p: Pair<'_, Rule>) -> Result<Statement> {
for p in p { for p in p {
match p.as_rule() { match p.as_rule() {
Rule::datum => to = Some(parse_datum(p)?.value), Rule::datum => to = Some(parse_datum(p)?.value),
Rule::time => to_time = Some(parse_time(p)?.value), Rule::time => to_time = Some(parse_time(p)?),
_ => unreachable!(), _ => unreachable!(),
} }
} }