Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1,225 @@
pub const UNACCONTABLE_WORDS: [&'static str; 202] = ["accommodation",
"adulthood",
"advertising",
"advice",
"aggression",
"aid",
"air",
"aircraft",
"alcohol",
"anger",
"applause",
"arithmetic",
"assistance",
"athletics",
"bacon",
"baggage",
"beef",
"biology",
"blood",
"botany",
"bread",
"butter",
"carbon",
"cardboard",
"cash",
"chalk",
"chaos",
"chess",
"crossroads",
"countryside",
"dancing",
"deer",
"dignity",
"dirt",
"dust",
"economics",
"education",
"electricity",
"engineering",
"enjoyment",
"envy",
"equipment",
"ethics",
"evidence",
"evolution",
"fame",
"fiction",
"flour",
"flu",
"food",
"fuel",
"fun",
"furniture",
"gallows",
"garbage",
"garlic",
"genetics",
"gold",
"golf",
"gossip",
"grammar",
"gratitude",
"grief",
"guilt",
"gymnastics",
"happiness",
"hardware",
"harm",
"hate",
"hatred",
"health",
"heat",
"help",
"homework",
"honesty",
"honey",
"hospitality",
"housework",
"humour",
"hunger",
"hydrogen",
"ice",
"importance",
"inflation",
"information",
"innocence",
"iron",
"irony",
"jam",
"jewelry",
"judo",
"karate",
"knowledge",
"lack",
"laughter",
"lava",
"leather",
"leisure",
"lightning",
"linguine",
"linguini",
"linguistics",
"literature",
"litter",
"livestock",
"logic",
"loneliness",
"luck",
"luggage",
"macaroni",
"machinery",
"magic",
"management",
"mankind",
"marble",
"mathematics",
"mayonnaise",
"measles",
"methane",
"milk",
"money",
"mud",
"music",
"mumps",
"nature",
"news",
"nitrogen",
"nonsense",
"nurture",
"nutrition",
"obedience",
"obesity",
"oxygen",
"pasta",
"patience",
"physics",
"poetry",
"pollution",
"poverty",
"pride",
"psychology",
"publicity",
"punctuation",
"quartz",
"racism",
"relaxation",
"reliability",
"research",
"respect",
"revenge",
"rice",
"rubbish",
"rum",
"safety",
"scenery",
"seafood",
"seaside",
"series",
"shame",
"sheep",
"shopping",
"sleep",
"smoke",
"smoking",
"snow",
"soap",
"software",
"soil",
"spaghetti",
"species",
"steam",
"stuff",
"stupidity",
"sunshine",
"symmetry",
"tennis",
"thirst",
"thunder",
"timber",
"traffic",
"transportation",
"trust",
"underwear",
"unemployment",
"unity",
"validity",
"veal",
"vegetation",
"vegetarianism",
"vengeance",
"violence",
"vitality",
"warmth",
"wealth",
"weather",
"welfare",
"wheat",
"wildlife",
"wisdom",
"yoga",
"zinc",
"zoology"];

View file

@ -0,0 +1,50 @@
#[cfg(feature = "heavyweight")]
use cases::classcase::to_class_case;
#[cfg(feature = "heavyweight")]
/// Deconstantizes a `&str`
///
/// ```
/// use inflector::string::deconstantize::deconstantize;
/// let mock_string: &str = "Bar";
/// let expected_string: String = "".to_owned();
/// let asserted_string: String = deconstantize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::deconstantize::deconstantize;
/// let mock_string: &str = "::Bar";
/// let expected_string: String = "".to_owned();
/// let asserted_string: String = deconstantize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::deconstantize::deconstantize;
/// let mock_string: &str = "Foo::Bar";
/// let expected_string: String = "Foo".to_owned();
/// let asserted_string: String = deconstantize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::deconstantize::deconstantize;
/// let mock_string: &str = "Test::Foo::Bar";
/// let expected_string: String = "Foo".to_owned();
/// let asserted_string: String = deconstantize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
pub fn deconstantize(non_deconstantized_string: &str) -> String {
if non_deconstantized_string.contains("::") {
let split_string: Vec<&str> = non_deconstantized_string.split("::").collect();
if split_string.len() > 1 {
to_class_case(split_string[split_string.len() - 2])
} else {
"".to_owned()
}
} else {
"".to_owned()
}
}

View file

@ -0,0 +1,46 @@
#[cfg(feature = "heavyweight")]
use cases::classcase::to_class_case;
#[cfg(feature = "heavyweight")]
/// Demodulize a `&str`
///
/// ```
/// use inflector::string::demodulize::demodulize;
/// let mock_string: &str = "Bar";
/// let expected_string: String = "Bar".to_owned();
/// let asserted_string: String = demodulize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::demodulize::demodulize;
/// let mock_string: &str = "::Bar";
/// let expected_string: String = "Bar".to_owned();
/// let asserted_string: String = demodulize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::demodulize::demodulize;
/// let mock_string: &str = "Foo::Bar";
/// let expected_string: String = "Bar".to_owned();
/// let asserted_string: String = demodulize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::demodulize::demodulize;
/// let mock_string: &str = "Test::Foo::Bar";
/// let expected_string: String = "Bar".to_owned();
/// let asserted_string: String = demodulize(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
pub fn demodulize(non_demodulize_string: &str) -> String {
if non_demodulize_string.contains("::") {
let split_string: Vec<&str> = non_demodulize_string.split("::").collect();
to_class_case(split_string[split_string.len() - 1])
} else {
non_demodulize_string.to_owned()
}
}

View file

@ -0,0 +1,23 @@
#![deny(warnings)]
/// Provides demodulize a string.
///
/// Example string `Foo::Bar` becomes `Bar`
#[cfg(feature = "heavyweight")]
pub mod demodulize;
/// Provides deconstantizea string.
///
/// Example string `Foo::Bar` becomes `Foo`
#[cfg(feature = "heavyweight")]
pub mod deconstantize;
/// Provides conversion to plural strings.
///
/// Example string `FooBar` -> `FooBars`
#[cfg(feature = "heavyweight")]
pub mod pluralize;
/// Provides conversion to singular strings.
///
/// Example string `FooBars` -> `FooBar`
#[cfg(feature = "heavyweight")]
pub mod singularize;
mod constants;

View file

@ -0,0 +1,194 @@
#![deny(warnings)]
use regex::Regex;
use string::constants::UNACCONTABLE_WORDS;
macro_rules! add_rule{
($r:ident, $rule:expr => $replace:expr) => {
$r.push((Regex::new($rule).unwrap(), $replace));
}
}
macro_rules! rules{
($r:ident; $($rule:expr => $replace:expr), *) => {
$(
add_rule!{$r, $rule => $replace}
)*
}
}
lazy_static!{
static ref RULES: Vec<(Regex, &'static str)> = {
let mut r = Vec::with_capacity(24);
rules![r;
r"(\w*)s$" => "s",
r"(\w*([^aeiou]ese))$" => "",
r"(\w*(ax|test))is$" => "es",
r"(\w*(alias|[^aou]us|tlas|gas|ris))$" => "es",
r"(\w*(e[mn]u))s?$" => "s",
r"(\w*([^l]ias|[aeiou]las|[emjzr]as|[iu]am))$" => "",
r"(\w*(alumn|syllab|octop|vir|radi|nucle|fung|cact|stimul|termin|bacill|foc|uter|loc|strat))(?:us|i)$" => "i",
r"(\w*(alumn|alg|vertebr))(?:a|ae)$" => "ae",
r"(\w*(seraph|cherub))(?:im)?$" => "im",
r"(\w*(her|at|gr))o$" => "oes",
r"(\w*(agend|addend|millenni|dat|extrem|bacteri|desiderat|strat|candelabr|errat|ov|symposi|curricul|automat|quor))(?:a|um)$" => "a",
r"(\w*(apheli|hyperbat|periheli|asyndet|noumen|phenomen|criteri|organ|prolegomen|hedr|automat))(?:a|on)$" => "a",
r"(\w*)sis$" => "ses",
r"(\w*(kni|wi|li))fe$" => "ves",
r"(\w*(ar|l|ea|eo|oa|hoo))f$" => "ves",
r"(\w*([^aeiouy]|qu))y$" => "ies",
r"(\w*([^ch][ieo][ln]))ey$" => "ies",
r"(\w*(x|ch|ss|sh|zz)es)$" => "",
r"(\w*(x|ch|ss|sh|zz))$" => "es",
r"(\w*(matr|cod|mur|sil|vert|ind|append))(?:ix|ex)$" => "ices",
r"(\w*(m|l)(?:ice|ouse))$" => "ice",
r"(\w*(pe)(?:rson|ople))$" => "ople",
r"(\w*(child))(?:ren)?$" => "ren",
r"(\w*eaux)$" => ""
];
r
};
}
macro_rules! special_cases{
($s:ident, $($singular: expr => $plural:expr), *) => {
match &$s[..] {
$(
$singular => {
return $plural.to_owned();
},
)*
_ => ()
}
}
}
/// Converts a `&str` to pluralized `String`
///
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "foo_bar";
/// let expected_string: String = "foo_bars".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "ox";
/// let expected_string: String = "oxen".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "crate";
/// let expected_string: String = "crates".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "boxes";
/// let expected_string: String = "boxes".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "vengeance";
/// let expected_string: String = "vengeance".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "yoga";
/// let expected_string: String = "yoga".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
/// ```
/// use inflector::string::pluralize::to_plural;
/// let mock_string: &str = "geometry";
/// let expected_string: String = "geometries".to_owned();
/// let asserted_string: String = to_plural(mock_string);
/// assert_eq!(asserted_string, expected_string);
///
/// ```
///
pub fn to_plural(non_plural_string: &str) -> String {
if UNACCONTABLE_WORDS.contains(&non_plural_string.as_ref()) {
non_plural_string.to_owned()
} else {
special_cases![non_plural_string,
"ox" => "oxen",
"man" => "men",
"woman" => "women",
"die" => "dice",
"yes" => "yeses",
"foot" => "feet",
"eave" => "eaves",
"goose" => "geese",
"tooth" => "teeth",
"quiz" => "quizzes"
];
for &(ref rule, replace) in RULES.iter().rev() {
if let Some(c) = rule.captures(&non_plural_string) {
if let Some(c) = c.get(1) {
return format!("{}{}", c.as_str(), replace);
}
}
}
format!("{}s", non_plural_string)
}
}
#[cfg(test)]
mod tests {
macro_rules! as_item {
($i:item) => { $i };
}
macro_rules! make_tests{
($($singular:ident => $plural:ident); *) =>{
$(
as_item! {
#[test]
fn $singular(){
assert_eq!(
stringify!($plural),
super::to_plural(stringify!($singular))
);
}
}
)*
}
}
#[test]
fn boxes() {
assert_eq!("boxes", super::to_plural("box"));
}
make_tests!{
geometry => geometries;
ox => oxen;
woman => women;
test => tests;
axis => axes;
knife => knives;
agendum => agenda;
elf => elves;
zoology => zoology
}
}

View file

@ -0,0 +1,189 @@
use regex::Regex;
use string::constants::UNACCONTABLE_WORDS;
macro_rules! special_cases{
($s:ident, $($singular: expr => $plural:expr), *) => {
match &$s[..] {
$(
$singular => {
return $plural.to_owned();
},
)*
_ => ()
}
}
}
/// Converts a `&str` to singularized `String`
///
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "foo_bars";
/// let expected_string: String = "foo_bar".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "oxen";
/// let expected_string: String = "ox".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "crates";
/// let expected_string: String = "crate".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "oxen";
/// let expected_string: String = "ox".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "boxes";
/// let expected_string: String = "box".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "vengeance";
/// let expected_string: String = "vengeance".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
/// ```
/// use inflector::string::singularize::to_singular;
/// let mock_string: &str = "yoga";
/// let expected_string: String = "yoga".to_owned();
/// let asserted_string: String = to_singular(mock_string);
/// assert!(asserted_string == expected_string);
///
/// ```
///
pub fn to_singular(non_singular_string: &str) -> String {
if UNACCONTABLE_WORDS.contains(&non_singular_string.as_ref()) {
non_singular_string.to_owned()
} else {
special_cases![non_singular_string,
"oxen" => "ox",
"boxes" => "box",
"men" => "man",
"women" => "woman",
"dice" => "die",
"yeses" => "yes",
"feet" => "foot",
"eaves" => "eave",
"geese" => "goose",
"teeth" => "tooth",
"quizzes" => "quiz"
];
for &(ref rule, replace) in RULES.iter().rev() {
if let Some(captures) = rule.captures(&non_singular_string) {
if let Some(c) = captures.get(1) {
let mut buf = String::new();
captures.expand(&format!("{}{}", c.as_str(), replace), &mut buf);
return buf;
}
}
}
format!("{}", non_singular_string)
}
}
macro_rules! add_rule{
($r:ident, $rule:expr => $replace:expr) => {
$r.push((Regex::new($rule).unwrap(), $replace));
}
}
macro_rules! rules{
($r:ident; $($rule:expr => $replace:expr), *) => {
$(
add_rule!{$r, $rule => $replace}
)*
}
}
lazy_static!{
static ref RULES: Vec<(Regex, &'static str)> = {
let mut r = Vec::with_capacity(27);
rules![r;
r"(\w*)s$" => "",
r"(\w*)(ss)$" => "$2",
r"(n)ews$" => "ews",
r"(\w*)(o)es$" => "",
r"(\w*)([ti])a$" => "um",
r"((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)(sis|ses)$" => "sis",
r"(^analy)(sis|ses)$" => "sis",
r"(\w*)([^f])ves$" => "fe",
r"(\w*)(hive)s$" => "",
r"(\w*)(tive)s$" => "",
r"(\w*)([lr])ves$" => "f",
r"(\w*([^aeiouy]|qu))ies$" => "y",
r"(s)eries$" => "eries",
r"(m)ovies$" => "ovie",
r"(\w*)(x|ch|ss|sh)es$" => "$2",
r"(m|l)ice$" => "ouse",
r"(bus)(es)?$" => "",
r"(shoe)s$" => "",
r"(cris|test)(is|es)$" => "is",
r"^(a)x[ie]s$" => "xis",
r"(octop|vir)(us|i)$" => "us",
r"(alias|status)(es)?$" => "",
r"^(ox)en" => "",
r"(vert|ind)ices$" => "ex",
r"(matr)ices$" => "ix",
r"(quiz)zes$" => "",
r"(database)s$" => ""
];
r
};
}
#[test]
fn singularize_ies_suffix() {
assert_eq!("reply", to_singular("replies"));
assert_eq!("lady", to_singular("ladies"));
assert_eq!("soliloquy", to_singular("soliloquies"));
}
#[test]
fn singularize_ss_suffix() {
assert_eq!("glass", to_singular("glass"));
assert_eq!("access", to_singular("access"));
assert_eq!("glass", to_singular("glasses"));
assert_eq!("witch", to_singular("witches"));
assert_eq!("dish", to_singular("dishes"));
}
#[test]
fn singularize_string_if_a_regex_will_match() {
let expected_string: String = "ox".to_owned();
let asserted_string: String = to_singular("oxen");
assert!(expected_string == asserted_string);
}
#[test]
fn singularize_string_returns_none_option_if_no_match() {
let expected_string: String = "bacon".to_owned();
let asserted_string: String = to_singular("bacon");
assert!(expected_string == asserted_string);
}