From 7c04e36cb04af1a8fa9dbd93c24d401f87991a13 Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sun, 31 Dec 2023 09:48:41 +0700 Subject: [PATCH 1/2] Added function --- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++---- tests/lib.rs | 14 +------------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9f2bdbf..50bedff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,41 @@ //! I'm so sorry... +const WORD_REPLACE: [(&'static str, &'static str); 9] = [ + ("small", "smol"), + ("cute", "kawaii~"), + ("fluff", "floof"), + ("love", "luv"), + ("stupid", "baka"), + ("idiot", "baka"), + ("what", "nani"), + ("meow", "nya~"), + ("roar", "rawrr~"), +]; + +/// Replace certain words with uwuified equivalents, such as: +/// +/// - `small` -> `smol` +/// - `cute` -> `kawaii~` +/// - `fluff` -> `floof` +/// - ... +/// +/// # Example: +/// +/// ``` +/// use imsosorrybtw::replace_words; +/// let text = "small dogs"; +/// let uwuified = replace_words(text); +/// +/// assert_eq!(uwuified, "smol dogs"); +/// ``` +pub fn replace_words(text: &str) -> String { + WORD_REPLACE + .iter() + .fold(text.to_string(), |acc, (word, replacement)| { + acc.replace(word, replacement) + }) +} + /// Replace certain characters with their UwU equivalents: /// /// - `r` -> `w` @@ -7,8 +43,6 @@ /// - `n` -> `ny` /// - `ove` -> `uv` /// -/// This will **NOT** account for capitalization. -/// /// # Example /// /// ``` @@ -32,10 +66,14 @@ pub fn char_replace(text: &str) -> String { /// UwUify a string. This basically turns the text lowercase and applies every other function in /// this crate to a string in order: /// +/// - `replace_words` /// - `char_replace` #[allow(clippy::doc_markdown)] #[must_use] pub fn uwuify(text: &str) -> String { - let text = text.to_lowercase(); - char_replace(&text) + let mut text = text.to_lowercase(); + text = replace_words(&text); + text = char_replace(&text); + + text } diff --git a/tests/lib.rs b/tests/lib.rs index 82fded1..9ae019f 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,19 +1,7 @@ //! Tests for the `imsosorrybtw` crate. //! If you're apologizing, might as well do it properly! -use imsosorrybtw::{char_replace, uwuify}; - -/// Test the `char_replace` function. -#[test] -fn test_char_replace() { - // This test is the same as the example in the docs, but it's good to have - // it here as well. - - let text = "I'm so sorry..."; - let uwuified = char_replace(text); - - assert_eq!(uwuified, "I'm so sowwy..."); -} +use imsosorrybtw::uwuify; /// Test if `uwuify` turns the text lowercase. #[test] From 9464c962b725bc3aa8c8b09fbe140cae5b1b6a5b Mon Sep 17 00:00:00 2001 From: somehybrid Date: Sun, 31 Dec 2023 09:53:48 +0700 Subject: [PATCH 2/2] Fixed linter issues --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 50bedff..b2eb597 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ //! I'm so sorry... -const WORD_REPLACE: [(&'static str, &'static str); 9] = [ +const WORD_REPLACE: [(&str, &str); 9] = [ ("small", "smol"), ("cute", "kawaii~"), ("fluff", "floof"), @@ -28,6 +28,8 @@ const WORD_REPLACE: [(&'static str, &'static str); 9] = [ /// /// assert_eq!(uwuified, "smol dogs"); /// ``` +#[allow(clippy::doc_markdown)] +#[must_use] pub fn replace_words(text: &str) -> String { WORD_REPLACE .iter()