use std::fmt::Display; include!(concat!(env!("OUT_DIR"), "/codegen.rs")); #[derive(Debug, Clone, Eq, PartialEq)] /// A word struct pub struct Word<'a> { /// The word itself pub word: &'a str, /// The binary representation of this number /// /// The words are responsible for 13 bits of data, so this is fine to fit in a u16 pub number: u16, } impl Display for Word<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self) } } /// Helper function that gets the mapped number from a word /// /// ```rust /// use words::get_number; /// /// assert!(get_number("ThE").is_some()); /// assert!(get_number("AsDf").is_none()); /// ``` pub fn get_number(maybe_word: S) -> Option where S: AsRef, { get_word(maybe_word).map(|w| w.number) } /// Gets a word from the word map /// /// ```rust /// use words::get_word; /// /// assert!(get_word("THE").is_some()); /// assert!(get_word("the").is_some()); /// assert!(get_word("tHe").is_some()); /// assert!(get_word("ASDFASDF").is_none()); /// ``` pub fn get_word(maybe_word: S) -> Option<&'static Word<'static>> where S: AsRef, { WORD_MAP .get(&maybe_word.as_ref().trim().to_ascii_uppercase()) .copied() } #[cfg(test)] mod tests { use super::*; #[test] fn test_equivalence() { // Test equivalence macro_rules! te { ($word1:ident, $word2: ident) => { eprintln!("Checking if {:?} is a word", stringify!($word1)); assert!(get_word(stringify!($word1)).is_some()); eprintln!("Checking if {:?} is a word", stringify!($word2)); assert!(get_word(stringify!($word2)).is_some()); eprintln!("Checking equivalence"); assert_eq!( get_word(stringify!($word1)).unwrap().number, get_word(stringify!($word1)).unwrap().number ); }; } // Homonyms te!(blue, blew); te!(yellow, hello); // Plurals te!(sent, sense); te!(sent, scents); te!(sent, cents); } }