Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,32 +223,32 @@ impl Type for Vec<Range<u32>> {
})
};

let component = |s: &mut Scanner, offset: usize| -> Result<u32, TypeError> {
loop {
let num = number(s, offset)?;
s.eat_whitespace();
if !s.eat_if(':') {
return Ok(num);
}
}
};

for (range_candidate, span) in
range_vecs.iter().map(|f| (f.format_verbatim(), f.span()))
{
let mut s = Scanner::new(&range_candidate);
let start = component(&mut s, span.start)?;
let start = number(&mut s, span.start)?;
s.eat_whitespace();

// The double and triple hyphen is converted into en dashes and em
// dashes earlier.
if !s.eat_if(['-', '–', '—']) {
res.push(start..start);
if !s.done() {
return Err(TypeError::new(span, TypeErrorKind::InvalidNumber));
}
Comment on lines +237 to +239
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder when these types of failures can happen 🤔

continue;
}
s.eat_while('-');
s.eat_whitespace();
let end = component(&mut s, span.start)?;
let offset = s.cursor();
let end = number(&mut s, span.start)?;
if !s.done() {
return Err(TypeError::new(
offset..span.end,
TypeErrorKind::InvalidNumber,
));
}
res.push(start..end);
}

Expand Down Expand Up @@ -473,11 +473,11 @@ impl Type for Gender {
#[cfg(test)]
mod tests {
use super::*;
use crate::chunk::tests::*;
use crate::{chunk::tests::*, Bibliography};

#[test]
fn test_ranges() {
let ranges = &[Spanned::zero(N("31--43,21:4-21:6, 194 --- 245"))];
let ranges = &[Spanned::zero(N("31--43,4-6, 194 --- 245"))];
let res = ranges.parse::<Vec<Range<u32>>>().unwrap();
assert_eq!(res[0], 31..43);
assert_eq!(res[1], 4..6);
Expand All @@ -491,4 +491,22 @@ mod tests {
assert_eq!(res[0], 34..34);
assert_eq!(res[1], 37..39);
}

#[test]
fn test_hayagriva_issue_340() {
let bib = Bibliography::parse(
r#"@inproceedings{test,
author = {John Doe},
title = {Interesting Findings},
journal = {Example Journal},
year = {2024},
pages = {1A1}
}"#,
)
.unwrap();
let t = bib.get("test").unwrap();
let pages = t.get("pages").unwrap();
let parsed: PermissiveType<std::ops::Range<u32>> = pages.parse().unwrap();
assert!(matches!(parsed, PermissiveType::Chunks(_)))
}
}