From 6e0040a232b669eaf2d26bbbd90dea39cb270adc Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Sun, 29 Oct 2023 21:21:05 +0100 Subject: [PATCH] Parse empty dates --- src/types/date.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/types/date.rs b/src/types/date.rs index d10a114..6d98dc7 100644 --- a/src/types/date.rs +++ b/src/types/date.rs @@ -162,12 +162,14 @@ impl Date { let span = chunks.span(); let date = chunks.format_verbatim().to_uppercase(); let mut date_trimmed = date.trim_end(); - - let (is_uncertain, is_approximate) = match &date_trimmed[date_trimmed.len() - 1..] - { - "?" => (true, false), - "~" => (false, true), - "%" => (true, true), + let last_trimmed = (!date.is_empty() + && date.is_char_boundary(date_trimmed.len() - 1)) + .then(|| &date_trimmed[date_trimmed.len() - 1..]); + + let (is_uncertain, is_approximate) = match last_trimmed { + Some("?") => (true, false), + Some("~") => (false, true), + Some("%") => (true, true), _ => (false, false), }; @@ -849,6 +851,12 @@ mod tests { use super::*; use crate::chunk::tests::*; + #[test] + fn test_parse_empty_date() { + assert!(Date::parse(&[]).is_err()); + assert!(Date::parse(&[s(N(""), 0..0)]).is_err()); + } + #[test] fn test_parse_date() { let date = Date::parse(&[s(N("2017-10 -25?"), 0..12)]).unwrap();