2023-02-15 19:16:57 -05:00
|
|
|
use thiserror::Error;
|
2023-02-11 17:04:16 -05:00
|
|
|
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],
|
|
|
|
}
|
|
|
|
|
2023-02-15 19:16:57 -05:00
|
|
|
/// 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<S>(maybe_word: S) -> Result<u16, Error>
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
|
|
|
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<S>(maybe_word: S) -> Result<&'static Word<'static>, Error>
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
}
|