use std::sync::Arc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum PluralRule {
#[default]
OneOther,
ZeroOneOther,
EastSlavic,
Polish,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PluralForm {
One,
Few,
Many,
Other,
}
impl PluralRule {
pub fn for_language(language: &str) -> Self {
match language {
"fr" => Self::ZeroOneOther,
"uk" | "ru" | "be" => Self::EastSlavic,
"pl" => Self::Polish,
_ => Self::OneOther,
}
}
pub fn form(self, count: u64) -> PluralForm {
let (tens, hundreds) = (count % 10, count % 100);
let few = (2..=4).contains(&tens) && !(12..=14).contains(&hundreds);
match self {
Self::OneOther if count == 1 => PluralForm::One,
Self::OneOther => PluralForm::Other,
Self::ZeroOneOther if count <= 1 => PluralForm::One,
Self::ZeroOneOther => PluralForm::Other,
Self::EastSlavic if tens == 1 && hundreds != 11 => PluralForm::One,
Self::Polish if count == 1 => PluralForm::One,
Self::EastSlavic | Self::Polish if few => PluralForm::Few,
Self::EastSlavic | Self::Polish => PluralForm::Many,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Plural {
pub one: Arc<str>,
pub few: Arc<str>,
pub many: Arc<str>,
pub other: Arc<str>,
}
impl Plural {
pub fn two(one: &str, other: &str) -> Self {
Self {
one: Arc::from(one),
few: Arc::from(other),
many: Arc::from(other),
other: Arc::from(other),
}
}
pub fn three(one: &str, few: &str, many: &str) -> Self {
Self {
one: Arc::from(one),
few: Arc::from(few),
many: Arc::from(many),
other: Arc::from(many),
}
}
pub fn same(word: &str) -> Self {
Self::two(word, word)
}
pub fn form(&self, rule: PluralRule, count: u64) -> &Arc<str> {
match rule.form(count) {
PluralForm::One => &self.one,
PluralForm::Few => &self.few,
PluralForm::Many => &self.many,
PluralForm::Other => &self.other,
}
}
pub fn say(&self, rule: PluralRule, count: u64) -> String {
fill(self.form(rule, count), &[("count", &count.to_string())])
}
pub fn forms(&self) -> [&Arc<str>; 4] {
[&self.one, &self.few, &self.many, &self.other]
}
}
impl From<&str> for Plural {
fn from(word: &str) -> Self {
Self::same(word)
}
}
impl From<String> for Plural {
fn from(word: String) -> Self {
Self::same(&word)
}
}
pub fn fill(word: &str, places: &[(&str, &str)]) -> String {
let mut said = String::with_capacity(word.len() + 16);
let mut rest = word;
while let Some(open) = rest.find('{') {
said.push_str(&rest[..open]);
let after = &rest[open + 1..];
match after.find('}').map(|close| (&after[..close], close)) {
Some((name, close)) if places.iter().any(|(place, _)| *place == name) => {
let (_, value) = places.iter().find(|(place, _)| *place == name).expect("found above");
said.push_str(value);
rest = &after[close + 1..];
}
_ => {
said.push('{');
rest = after;
}
}
}
said.push_str(rest);
said
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_language_picks_its_form_after_a_number() {
let uk = PluralRule::EastSlavic;
let forms: Vec<_> = [1, 2, 5, 11, 12, 21, 22, 25, 101, 111, 0].map(|n| uk.form(n)).into();
use PluralForm::{Few, Many, One};
assert_eq!(forms, [One, Few, Many, Many, Many, One, Few, Many, One, Many, Many]);
let pl = PluralRule::Polish;
let forms: Vec<_> = [1, 2, 5, 12, 21, 22, 0].map(|n| pl.form(n)).into();
assert_eq!(forms, [One, Few, Many, Many, Many, Few, Many]);
assert_eq!(PluralRule::OneOther.form(0), PluralForm::Other);
assert_eq!(PluralRule::ZeroOneOther.form(2), PluralForm::Other);
assert_eq!(PluralRule::for_language("xx"), PluralRule::OneOther);
}
#[test]
fn a_word_has_its_places_filled_and_keeps_the_braces_it_has_no_value_for() {
assert_eq!(fill("{shown} of {count}", &[("shown", "3"), ("count", "9")]), "3 of 9");
assert_eq!(fill("{a} {b}", &[("a", "x")]), "x {b}");
assert_eq!(fill("{ open", &[]), "{ open");
assert_eq!(
Plural::two("{count} row", "{count} rows").say(PluralRule::OneOther, 4),
"4 rows"
);
}
}