git_anger_library/
core.rs

1use once_cell::sync::OnceCell;
2use std::collections::HashSet;
3
4/// Statically include the word list for curse words
5pub static CURSES_FILE: &str = include_str!("words.txt");
6
7fn curses() -> &'static HashSet<&'static str> {
8    static INSTANCE: OnceCell<HashSet<&'static str>> = OnceCell::new();
9    INSTANCE.get_or_init(|| CURSES_FILE.lines().map(|l| l.trim()).collect())
10}
11
12/// Cleans a string and returns a list containing the cleaned up words.
13///
14/// Of note here is that the implementation splits on any character that is not
15/// a letter, even if it is in the middle of a "word". This should not be a
16/// problem.
17pub fn split_into_clean_words(input: &str) -> impl Iterator<Item = &str> {
18    input
19        .split(|c: char| !char::is_alphabetic(c))
20        .filter(|w| !w.is_empty())
21}
22
23/// Checks if a word is naughty.
24pub fn naughty_word(word: &str) -> bool {
25    curses().contains(&word)
26}
27
28#[cfg(test)]
29mod test {
30    use super::*;
31
32    #[test]
33    fn test_naughty_words() {
34        assert!(naughty_word("fuck"));
35        assert!(naughty_word("cyberfuckers"));
36        assert!(naughty_word("shitty"));
37        assert!(!naughty_word("pretty"));
38    }
39
40    #[test]
41    fn test_clean_word() {
42        let words = split_into_clean_words("This! is a string: with, some. words in? it;");
43        assert_eq!(
44            vec!["This", "is", "a", "string", "with", "some", "words", "in", "it"],
45            words.collect::<Vec<_>>()
46        );
47    }
48}