use thiserror::Error; include!(concat!(env!("OUT_DIR"), "/codegen.rs")); #[derive(Debug, Clone, 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, } #[derive(Debug, Clone, PartialEq)] pub struct Address<'a> { number: u32, words: [Word<'a>; 3], } /// Helper function that gets the mapped number from a word /// /// ```rust /// use types::get_number; /// /// assert!(get_number("ThE").is_ok()); /// assert!(get_number("AsDf").is_err()); /// ``` pub fn get_number(maybe_word: S) -> Result where S: AsRef, { get_word(maybe_word).map(|w| w.number) } /// Gets a word from the word map /// /// ```rust /// use types::get_word; /// /// assert!(get_word("THE").is_ok()); /// assert!(get_word("the").is_ok()); /// assert!(get_word("tHe").is_ok()); /// assert!(get_word("ASDFASDF").is_err()); /// ``` pub fn get_word(maybe_word: S) -> Result<&'static Word<'static>, Error> where S: AsRef, { WORD_MAP .get(&maybe_word.as_ref().trim().to_ascii_uppercase()) .copied() .ok_or(Error::WordNotFound) } #[derive(Error, Debug)] pub enum Error { #[error("Word not found")] WordNotFound, #[error("The requested number is out of bounds")] NumberOutOfBounds, }