Skip to content

Commit

Permalink
Parse empty dates
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 29, 2023
1 parent 0e5d55b commit 6e0040a
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/types/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6e0040a

Please sign in to comment.