Skip to content

Commit

Permalink
Make page-range MabeTyped again (typst#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih authored and danilasar committed Dec 31, 2024
1 parent e8cb257 commit 52bdedb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
10 changes: 2 additions & 8 deletions src/csl/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,7 @@ impl RenderCsl for citationberg::Label {
};

let depth = ctx.push_elem(citationberg::Formatting::default());
let plural = match p {
MaybeTyped::Typed(p) => p.is_plural(),
_ => false,
};
let plural = p.as_typed().map_or(false, |p| p.is_plural());

let content =
ctx.term(Term::from(pv), self.label.form, plural).unwrap_or_default();
Expand Down Expand Up @@ -497,10 +494,7 @@ impl RenderCsl for citationberg::Label {
}
NumberOrPageVariable::Page(pv) => {
if let Some(p) = ctx.resolve_page_variable(pv) {
let plural = match p {
MaybeTyped::Typed(p) => p.is_plural(),
_ => false,
};
let plural = p.as_typed().map_or(false, |p| p.is_plural());
(
ctx.term(Term::from(pv), self.label.form, plural).is_some(),
UsageInfo::default(),
Expand Down
3 changes: 2 additions & 1 deletion src/csl/taxonomy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ impl EntryLike for Entry {
}
NumberVariable::PageFirst => self
.page_range()
.and_then(MaybeTyped::as_typed)
.and_then(PageRanges::first)
.map(|r| MaybeTyped::Typed(Cow::Owned(r.clone()))),
NumberVariable::PartNumber => self
Expand Down Expand Up @@ -216,7 +217,7 @@ impl EntryLike for Entry {
variable: PageVariable,
) -> Option<MaybeTyped<PageRanges>> {
match variable {
PageVariable::Page => self.page_range().map(|r| MaybeTyped::Typed(r.clone())),
PageVariable::Page => self.page_range().cloned(),
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/interop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Provides conversion methods for BibLaTeX.
use std::convert::TryFrom;
use std::str::FromStr;

use biblatex as tex;
use tex::{
Expand Down Expand Up @@ -497,7 +496,7 @@ impl TryFrom<&tex::Entry> for Entry {

if let Some(pages) = map_res(entry.pages())? {
item.set_page_range(match pages {
PermissiveType::Typed(pages) => PageRanges::new(
PermissiveType::Typed(pages) => MaybeTyped::Typed(PageRanges::new(
pages
.into_iter()
.map(|p| {
Expand All @@ -511,9 +510,9 @@ impl TryFrom<&tex::Entry> for Entry {
}
})
.collect(),
),
)),
PermissiveType::Chunks(chunks) => {
PageRanges::from_str(&chunks.format_verbatim()).unwrap()
MaybeTyped::infallible_from_str(&chunks.format_verbatim())
}
});
}
Expand Down
21 changes: 20 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ entry! {
/// Published version of an item.
"edition" => edition: MaybeTyped<Numeric>,
/// The range of pages within the parent this item occupies
"page-range" => page_range: PageRanges,
"page-range" => page_range: MaybeTyped<PageRanges>,
/// The total number of pages the item has.
"page-total" => page_total: Numeric,
/// The time range within the parent this item starts and ends at.
Expand Down Expand Up @@ -982,4 +982,23 @@ mod tests {
["a", "b", "c"]
);
}

#[test]
#[cfg(feature = "biblatex")]
fn test_troublesome_page_ranges() {
use io::from_biblatex_str;

let bibtex = r#"
@article{b,
title={My page ranges},
pages={150--es}
}
"#;

let library = from_biblatex_str(bibtex).unwrap();

for entry in library.iter() {
assert!(entry.page_range.is_some())
}
}
}
10 changes: 10 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,16 @@ pub enum MaybeTyped<T> {
String(String),
}

impl<T> MaybeTyped<T> {
/// Get the typed value, if it is present.
pub fn as_typed(&self) -> Option<&T> {
match self {
MaybeTyped::Typed(t) => Some(t),
MaybeTyped::String(_) => None,
}
}
}

impl<T: ToOwned> MaybeTyped<T> {
/// Wrap the typed value in a [`Cow`]'s borrowed variant.
pub fn to_cow(&self) -> MaybeTyped<Cow<T>> {
Expand Down

0 comments on commit 52bdedb

Please sign in to comment.