//! A set of generic utilities used across the codebase. //! //! It currently includes [`ToTitleCase`], [`get_random_u64`], and [`Bye`]. use rand::{Rng, SeedableRng}; use rand_chacha::ChaCha20Rng; /// Converts a string to title case. /// /// It removes all duplicate whitespaces and capitalizes the first letter of every word. pub trait ToTitleCase { fn to_title_case(&self) -> String; } impl ToTitleCase for T where T: ToString, { /// Removes duplicated whitespaces and capitalizes the first letter of each word. /// /// Returns: /// [`String`] - The title-cased version of the string. fn to_title_case(&self) -> String { self.to_string() .split_whitespace() .map(|word| { let mut chars = word.chars(); match chars.next() { Some(first) => first.to_uppercase().collect::() + chars.as_str(), None => String::new(), } }) .collect::>() .join(" ") } } /// Returns a random `u64` generated using a cryptographically secure random number generator. /// /// Returns: /// [`u64`] - A randomly generated 64-bit unsigned integer. pub fn get_random_u64() -> u64 { let mut rng = ChaCha20Rng::from_os_rng(); rng.random() } /// Consumes any value. Used to normalize return values when the output of a function doesn't /// matter yet all functions being called return different types. pub trait Bye: Sized { /// Whatever the value is, it consumes it and returns a unit value. Used to remove warnings /// about unused results. fn bye(self) {} } impl Bye for T {}