Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make page-range MabeTyped again #224

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 2 additions & 8 deletions src/csl/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,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 @@ -496,10 +493,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 @@ -491,7 +490,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 @@ -505,9 +504,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