From 252be3e00159eefab3f71cae2e054962ac79608b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 15 Dec 2023 11:38:52 -0500 Subject: [PATCH] Fix post blank for timestamp date ranges. --- src/parser/clock.rs | 2 +- src/parser/timestamp.rs | 50 +++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/parser/clock.rs b/src/parser/clock.rs index 0ad17b0..e702640 100644 --- a/src/parser/clock.rs +++ b/src/parser/clock.rs @@ -82,7 +82,7 @@ fn clock_timestamp<'b, 'g, 'r, 's>( |(timestamp, duration)| (timestamp, duration.map(Into::<&str>::into)), ), map( - parser_with_context!(inactive_timestamp)(context), + parser_with_context!(inactive_timestamp(true))(context), |timestamp| (timestamp, None), ), ))(input) diff --git a/src/parser/timestamp.rs b/src/parser/timestamp.rs index 6b41fe6..1342c25 100644 --- a/src/parser/timestamp.rs +++ b/src/parser/timestamp.rs @@ -53,8 +53,8 @@ pub(crate) fn timestamp<'b, 'g, 'r, 's>( parser_with_context!(inactive_time_range_timestamp)(context), parser_with_context!(active_date_range_timestamp)(context), parser_with_context!(inactive_date_range_timestamp)(context), - parser_with_context!(active_timestamp)(context), - parser_with_context!(inactive_timestamp)(context), + parser_with_context!(active_timestamp(true))(context), + parser_with_context!(inactive_timestamp(true))(context), ))(input) } @@ -126,13 +126,23 @@ fn sexp_end<'b, 'g, 'r, 's>( alt((tag(")>"), recognize(one_of(">\n"))))(input) } +const fn active_timestamp( + allow_post_blank: bool, +) -> impl for<'b, 'g, 'r, 's> Fn( + RefContext<'b, 'g, 'r, 's>, + OrgSource<'s>, +) -> Res, Timestamp<'s>> { + move |context, input| impl_active_timestamp(context, input, allow_post_blank) +} + #[cfg_attr( feature = "tracing", tracing::instrument(ret, level = "debug", skip(context)) )] -fn active_timestamp<'b, 'g, 'r, 's>( +fn impl_active_timestamp<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, + allow_post_blank: bool, ) -> Res, Timestamp<'s>> { let (remaining, _) = tag("<")(input)?; let (remaining, start) = date(context, remaining)?; @@ -160,8 +170,11 @@ fn active_timestamp<'b, 'g, 'r, 's>( )))(remaining)?; let (remaining, _) = tag(">")(remaining)?; - let (remaining, post_blank) = - maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?; + let (remaining, post_blank) = if allow_post_blank { + maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)? + } else { + (remaining, None) + }; let source = get_consumed(input, remaining); Ok(( @@ -181,13 +194,23 @@ fn active_timestamp<'b, 'g, 'r, 's>( )) } +pub(crate) const fn inactive_timestamp( + allow_post_blank: bool, +) -> impl for<'b, 'g, 'r, 's> Fn( + RefContext<'b, 'g, 'r, 's>, + OrgSource<'s>, +) -> Res, Timestamp<'s>> { + move |context, input| impl_inactive_timestamp(context, input, allow_post_blank) +} + #[cfg_attr( feature = "tracing", tracing::instrument(ret, level = "debug", skip(context)) )] -pub(crate) fn inactive_timestamp<'b, 'g, 'r, 's>( +fn impl_inactive_timestamp<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, + allow_post_blank: bool, ) -> Res, Timestamp<'s>> { let (remaining, _) = tag("[")(input)?; let (remaining, start) = date(context, remaining)?; @@ -215,8 +238,11 @@ pub(crate) fn inactive_timestamp<'b, 'g, 'r, 's>( )))(remaining)?; let (remaining, _) = tag("]")(remaining)?; - let (remaining, post_blank) = - maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?; + let (remaining, post_blank) = if allow_post_blank { + maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)? + } else { + (remaining, None) + }; let source = get_consumed(input, remaining); Ok(( @@ -244,10 +270,10 @@ fn active_date_range_timestamp<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Timestamp<'s>> { - let (remaining, first_timestamp) = active_timestamp(context, input)?; + let (remaining, first_timestamp) = impl_active_timestamp(context, input, false)?; // TODO: Does the space0 at the end of the active/inactive timestamp parsers cause this to be incorrect? I could use a look-behind to make sure the preceding character is not whitespace let (remaining, _separator) = tag("--")(remaining)?; - let (remaining, second_timestamp) = active_timestamp(context, remaining)?; + let (remaining, second_timestamp) = impl_active_timestamp(context, remaining, false)?; let (remaining, post_blank) = maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?; @@ -335,10 +361,10 @@ pub(crate) fn inactive_date_range_timestamp<'b, 'g, 'r, 's>( context: RefContext<'b, 'g, 'r, 's>, input: OrgSource<'s>, ) -> Res, Timestamp<'s>> { - let (remaining, first_timestamp) = inactive_timestamp(context, input)?; + let (remaining, first_timestamp) = impl_inactive_timestamp(context, input, false)?; // TODO: Does the space0 at the end of the active/inactive timestamp parsers cause this to be incorrect? I could use a look-behind to make sure the preceding character is not whitespace let (remaining, _separator) = tag("--")(remaining)?; - let (remaining, second_timestamp) = inactive_timestamp(context, remaining)?; + let (remaining, second_timestamp) = impl_inactive_timestamp(context, remaining, false)?; let (remaining, post_blank) = maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;