207 lines
5.8 KiB
Rust
207 lines
5.8 KiB
Rust
#![deny(unused_variables, missing_docs, unsafe_code, unused_extern_crates)]
|
|
|
|
//! Adds String based inflections for Rust. Snake, kebab, train, camel,
|
|
//! sentence, class, and title cases as well as ordinalize,
|
|
//! deordinalize, demodulize, deconstantize, and foreign key are supported as
|
|
//! both traits and pure functions acting on String types.
|
|
//! ```rust
|
|
//! use cruet::Inflector;
|
|
//! let camel_case_string: String = "some_string".to_camel_case();
|
|
//! let is_camel_cased: bool = camel_case_string.is_camel_case();
|
|
//! assert!(is_camel_cased == true);
|
|
//! ```
|
|
|
|
/// Provides case inflections
|
|
/// - Camel case
|
|
/// - Class case
|
|
/// - Kebab case
|
|
/// - Train case
|
|
/// - Screaming snake case
|
|
/// - Table case
|
|
/// - Sentence case
|
|
/// - Snake case
|
|
/// - Pascal case
|
|
pub mod case;
|
|
/// Provides number inflections
|
|
/// - Ordinalize
|
|
/// - Deordinalize
|
|
pub mod number;
|
|
/// Provides string inflections
|
|
/// - Deconstantize
|
|
/// - Demodulize
|
|
/// - Pluralize
|
|
/// - Singularize
|
|
pub mod string;
|
|
/// Provides suffix inflections
|
|
/// - Foreign key
|
|
pub mod suffix;
|
|
|
|
pub use case::camel::{is_camel_case, to_camel_case};
|
|
pub use case::class::{is_class_case, to_class_case};
|
|
pub use case::kebab::{is_kebab_case, to_kebab_case};
|
|
pub use case::pascal::{is_pascal_case, to_pascal_case};
|
|
pub use case::screaming_snake::{is_screaming_snake_case, to_screaming_snake_case};
|
|
pub use case::sentence::{is_sentence_case, to_sentence_case};
|
|
pub use case::snake::{is_snake_case, to_snake_case};
|
|
pub use case::table::{is_table_case, to_table_case};
|
|
pub use case::title::{is_title_case, to_title_case};
|
|
pub use case::train::{is_train_case, to_train_case};
|
|
pub use number::deordinalize::deordinalize;
|
|
pub use number::ordinalize::ordinalize;
|
|
pub use string::deconstantize::deconstantize;
|
|
pub use string::demodulize::demodulize;
|
|
pub use string::pluralize::to_plural;
|
|
pub use string::singularize::to_singular;
|
|
pub use suffix::foreign_key::{is_foreign_key, to_foreign_key};
|
|
|
|
#[allow(missing_docs)]
|
|
pub trait Inflector {
|
|
fn to_camel_case(&self) -> String;
|
|
fn is_camel_case(&self) -> bool;
|
|
|
|
fn to_pascal_case(&self) -> String;
|
|
fn is_pascal_case(&self) -> bool;
|
|
|
|
fn to_snake_case(&self) -> String;
|
|
fn is_snake_case(&self) -> bool;
|
|
|
|
fn to_screaming_snake_case(&self) -> String;
|
|
fn is_screaming_snake_case(&self) -> bool;
|
|
|
|
fn to_kebab_case(&self) -> String;
|
|
fn is_kebab_case(&self) -> bool;
|
|
|
|
fn to_train_case(&self) -> String;
|
|
fn is_train_case(&self) -> bool;
|
|
|
|
fn to_sentence_case(&self) -> String;
|
|
fn is_sentence_case(&self) -> bool;
|
|
|
|
fn to_title_case(&self) -> String;
|
|
fn is_title_case(&self) -> bool;
|
|
|
|
fn ordinalize(&self) -> String;
|
|
fn deordinalize(&self) -> String;
|
|
|
|
fn to_foreign_key(&self) -> String;
|
|
fn is_foreign_key(&self) -> bool;
|
|
|
|
fn demodulize(&self) -> String;
|
|
|
|
fn deconstantize(&self) -> String;
|
|
|
|
fn to_class_case(&self) -> String;
|
|
|
|
fn is_class_case(&self) -> bool;
|
|
|
|
fn to_table_case(&self) -> String;
|
|
|
|
fn is_table_case(&self) -> bool;
|
|
|
|
fn to_plural(&self) -> String;
|
|
|
|
fn to_singular(&self) -> String;
|
|
}
|
|
|
|
#[allow(missing_docs)]
|
|
pub trait InflectorNumbers {
|
|
fn ordinalize(&self) -> String;
|
|
}
|
|
|
|
macro_rules! define_implementations {
|
|
( $slf:ident; $($imp_trait:ident => $typ:ident), *) => {
|
|
$(
|
|
#[inline]
|
|
fn $imp_trait(&$slf) -> $typ {
|
|
$imp_trait($slf)
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
|
|
macro_rules! define_number_implementations {
|
|
( $slf:ident; $($imp_trait:ident => $typ:ident), *) => {
|
|
$(
|
|
#[inline]
|
|
fn $imp_trait(&$slf) -> $typ {
|
|
$imp_trait(&$slf.to_string())
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
|
|
macro_rules! define_gated_implementations {
|
|
( $slf:ident; $($imp_trait:ident => $typ:ident), *) => {
|
|
$(
|
|
#[inline]
|
|
|
|
fn $imp_trait(&$slf) -> $typ {
|
|
$imp_trait($slf)
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
|
|
macro_rules! implement_string_for {
|
|
( $trt:ident; $($typ:ident), *) => {
|
|
$(
|
|
impl $trt for $typ {
|
|
define_implementations![self;
|
|
to_camel_case => String,
|
|
is_camel_case => bool,
|
|
to_pascal_case => String,
|
|
is_pascal_case => bool,
|
|
to_screaming_snake_case => String,
|
|
is_screaming_snake_case => bool,
|
|
to_snake_case => String,
|
|
is_snake_case => bool,
|
|
to_kebab_case => String,
|
|
is_kebab_case => bool,
|
|
to_train_case => String,
|
|
is_train_case => bool,
|
|
to_sentence_case => String,
|
|
is_sentence_case => bool,
|
|
to_title_case => String,
|
|
is_title_case => bool,
|
|
to_foreign_key => String,
|
|
is_foreign_key => bool,
|
|
ordinalize => String,
|
|
deordinalize => String
|
|
];
|
|
define_gated_implementations![self;
|
|
to_class_case => String,
|
|
is_class_case => bool,
|
|
to_table_case => String,
|
|
is_table_case => bool,
|
|
to_plural => String,
|
|
to_singular => String,
|
|
demodulize => String,
|
|
deconstantize => String
|
|
];
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
|
|
macro_rules! implement_number_for {
|
|
( $trt:ident; $($typ:ident), *) => {
|
|
$(
|
|
impl $trt for $typ {
|
|
define_number_implementations![self;
|
|
ordinalize => String
|
|
];
|
|
}
|
|
)*
|
|
}
|
|
}
|
|
|
|
implement_string_for![
|
|
Inflector;
|
|
String, str
|
|
];
|
|
|
|
implement_number_for![
|
|
InflectorNumbers;
|
|
i8, i16, i32, i64, u8, u16, u32, u64, isize, usize, f32, f64
|
|
];
|