Skip to content

Commit

Permalink
Improved range parsing and conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 29, 2023
1 parent 2b74362 commit 5116f86
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/csl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,6 @@ impl<'a, T: EntryLike> Context<'a, T> {
while let Some(current_form) = form {
if let Some(localization) = self.style.lookup_locale(|l| {
let term = l.term(term, current_form)?;
dbg!(&term);
Some(if plural { term.multiple() } else { term.single() })
}) {
return localization;
Expand Down
42 changes: 33 additions & 9 deletions src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,15 +468,39 @@ impl TryFrom<&tex::Entry> for Entry {
}
}

if let Some(pages) = map_res(entry.pages())?
.and_then(|pages| match pages {
PermissiveType::Typed(p) => Some(p),
PermissiveType::Chunks(_) => None,
})
.and_then(|p| p.get(0).cloned())
{
item.page_range =
Some(Numeric::from_range((pages.start as i32)..(pages.end as i32)));
if let Some(pages) = map_res(entry.pages())?.and_then(|pages| match pages {
PermissiveType::Typed(p) => Some(p),
PermissiveType::Chunks(_) => None,
}) {
item.set_page_range(
if let Some(n) =
pages.first().filter(|f| pages.len() == 1 && f.start == f.end)
{
Numeric::new(n.start as i32)
} else {
let mut items = vec![];
for (i, pair) in pages.iter().enumerate() {
let last = i + 1 == pages.len();
let last_delim = (!last).then_some(NumericDelimiter::Comma);

if pair.start == pair.end {
items.push((pair.start as i32, last_delim));
} else {
items.push((
pair.start as i32,
Some(NumericDelimiter::Hyphen),
));
items.push((pair.end as i32, last_delim));
}
}

Numeric {
value: NumericValue::Set(items),
prefix: None,
suffix: None,
}
},
);
}

if let Some(ptotal) =
Expand Down
22 changes: 21 additions & 1 deletion src/types/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ impl FromStr for Numeric {
let value = match s.peek() {
Some(c) if is_delimiter(c) => {
s.eat();
s.eat_until(|c: char| !is_delimiter(c));
let mut items = vec![(value, Some(NumericDelimiter::try_from(c)?))];
loop {
let num = number(&mut s).ok_or(NumericError::NoNumber)?;
Expand Down Expand Up @@ -454,7 +455,7 @@ impl FromStr for NumericDelimiter {

fn from_str(s: &str) -> Result<Self, Self::Err> {
let first_char = s.chars().next().ok_or(NumericError::MissingDelimiter)?;
if s.len() > first_char.len_utf8() {
if first_char != '-' && s.len() > first_char.len_utf8() {
return Err(NumericError::NotADelimiter);
}

Expand All @@ -474,3 +475,22 @@ impl TryFrom<char> for NumericDelimiter {
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_mixed_range() {
let s = "34,37--39";
let n: Numeric = s.parse().unwrap();
assert_eq!(
n.value,
NumericValue::Set(vec![
(34, Some(NumericDelimiter::Comma)),
(37, Some(NumericDelimiter::Hyphen)),
(39, None)
])
);
}
}

0 comments on commit 5116f86

Please sign in to comment.