#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
mod exceptions;
use grapheme_picker::{drop_last, drop_lasts, take_last, take_lasts};
#[must_use]
pub fn pluralize_word(word: &str) -> String {
if word == "" {
return String::new();
}
if word == "ail" {
return String::from("aulx");
}
if word == "oeil" {
return String::from("yeux");
}
if word == "viel" {
return String::from("vieux");
}
if word == "topos" {
return String::from("topo\u{ef}");
}
if word == "d\u{fb}" {
return String::from("dus");
}
let result = String::from(word);
let last = take_last(word);
if exceptions::Y.contains(&word) {
return drop_last(word) + "ies";
}
if last == "s" || last == "x" || last == "z" {
return result;
}
let last_2_graphemes = take_lasts(word, 2);
let last2 = last_2_graphemes.as_str();
if last2 == "au" || last2 == "eu" || last2 == "ou" || last2 == "al" || last2 == "\u{153}u" {
return match last2 {
"ou" => {
if exceptions::OU.contains(&word) {
result + "x"
} else {
result + "s"
}
}
"eu" | "au" | "\u{153}u" => {
if exceptions::AU.contains(&word) || exceptions::EU.contains(&word) {
result + "s"
} else {
result + "x"
}
}
"al" => {
if exceptions::AL.contains(&word) {
result + "s"
} else {
drop_last(word) + "ux"
}
}
_ => result,
};
}
let last_3_graphemes = take_lasts(word, 3);
let last3 = last_3_graphemes.as_str();
if last3 == "ail" && exceptions::AIL.contains(&word) {
return drop_lasts(word, 2) + "ux";
}
result + "s"
}
#[cfg(test)]
#[allow(clippy::non_ascii_literal)]
mod tests {
use super::pluralize_word;
#[test]
fn pluralize_works() {
assert_eq!(pluralize_word(""), "");
assert_eq!(pluralize_word("a"), "as");
assert_eq!(pluralize_word("oeil"), "yeux");
assert_eq!(pluralize_word("tests"), "tests");
assert_eq!(pluralize_word("houx"), "houx");
assert_eq!(pluralize_word("nez"), "nez");
assert_eq!(pluralize_word("bleu"), "bleus");
assert_eq!(pluralize_word("vieu"), "vieux");
assert_eq!(pluralize_word("vœu"), "vœux");
assert_eq!(pluralize_word("bateau"), "bateaux");
assert_eq!(pluralize_word("landau"), "landaus");
assert_eq!(pluralize_word("bijou"), "bijoux");
assert_eq!(pluralize_word("matou"), "matous");
assert_eq!(pluralize_word("animal"), "animaux");
assert_eq!(pluralize_word("festival"), "festivals");
assert_eq!(pluralize_word("corail"), "coraux");
assert_eq!(pluralize_word("émail"), "émaux");
assert_eq!(pluralize_word("chandail"), "chandails");
assert_eq!(pluralize_word("voiture"), "voitures");
assert_eq!(pluralize_word("vélo"), "vélos");
}
}