Skip to content

Commit

Permalink
Change long ordinal resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Sep 22, 2023
1 parent b948657 commit c345ba7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,11 @@ impl<'a> OrdinalLookup<'a> {
Self { terms, legacy_behavior }
}

/// Create an empty lookup that will never return matches.
pub const fn empty() -> Self {
Self { terms: Vec::new(), legacy_behavior: false }
}

/// Look up a short ordinal for a number.
pub fn lookup(&self, n: i32) -> Option<&'a str> {
let mut best_match: Option<&'a LocalizedTerm> = None;
Expand Down Expand Up @@ -1884,17 +1889,24 @@ impl<'a> OrdinalLookup<'a> {
best_match.and_then(|t| t.single().or_else(|| t.multiple()))
}

/// Look up a long ordinal for a number. Includes fallback to short
/// ordinals.
/// Look up a long ordinal for a number. Does not include fallback to
/// regular ordinals.
pub fn lookup_long(&self, n: i32) -> Option<&'a str> {
self.terms
.iter()
.find(|t| {
let Term::Other(OtherTerm::LongOrdinal(o)) = t.name else { return false };
n == o as i32
if n > 0 && n <= 10 {
n == o as i32
} else {
match t.match_ {
Some(OrdinalMatch::LastTwoDigits) | None => n % 100 == o as i32,
Some(OrdinalMatch::WholeNumber) => n == o as i32,
_ => false,
}
}
})
.and_then(|t| t.single().or_else(|| t.multiple()))
.or_else(|| self.lookup(n))
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/taxonomy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ pub enum Variable {
Name(NameVariable),
}

impl Variable {
/// Check if the variable starts with `number-of-` to control contextual
/// label behavior.
pub const fn is_number_of_variable(self) -> bool {
if let Self::Number(v) = self {
v.is_number_of_variable()
} else {
false
}
}
}

/// The set of variables with no other attributes.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down Expand Up @@ -234,6 +246,14 @@ pub enum NumberVariable {
Volume,
}

impl NumberVariable {
/// Check if the variable starts with `number-of-` to control contextual
/// label behavior.
pub const fn is_number_of_variable(self) -> bool {
matches!(self, Self::NumberOfPages | Self::NumberOfVolumes)
}
}

impl From<NumberVariable> for Variable {
fn from(value: NumberVariable) -> Self {
Self::Number(value)
Expand Down

0 comments on commit c345ba7

Please sign in to comment.