diff --git a/rust/Cargo.lock b/rust/Cargo.lock index b21cc6a..29a52e3 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "once_cell" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" + [[package]] name = "rust" version = "0.1.0" +dependencies = [ + "once_cell", +] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 84a8d29..6c42619 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] \ No newline at end of file +[dependencies] +once_cell = "1.10.0" diff --git a/rust/tests/9_if_you_can_reed_this.rs b/rust/tests/9_if_you_can_reed_this.rs new file mode 100644 index 0000000..73c0ce5 --- /dev/null +++ b/rust/tests/9_if_you_can_reed_this.rs @@ -0,0 +1,54 @@ +use once_cell::sync::Lazy; +use std::collections::HashMap; + +#[rustfmt::skip] +pub static NATO: Lazy> = Lazy::new(|| { + [ + ('A', "Alfa"), ('B', "Bravo"), ('C', "Charlie"), ('D', "Delta"), + ('E', "Echo"), ('F', "Foxtrot"), ('G', "Golf"), ('H', "Hotel"), + ('I', "India"), ('J', "Juliett"), ('K', "Kilo"), ('L', "Lima"), + ('M', "Mike"), ('N', "November"), ('O', "Oscar"), ('P', "Papa"), + ('Q', "Quebec"), ('R', "Romeo"), ('S', "Sierra"), ('T', "Tango"), + ('U', "Uniform"), ('V', "Victor"), ('W', "Whiskey"), ('X', "Xray"), + ('Y', "Yankee"), ('Z', "Zulu"), + ] + .iter() + .copied() + .collect() +}); + +fn to_nato(words: &str) -> String { + words + .to_uppercase() + .chars() + .filter(|c| !c.is_whitespace()) + .map(|c| match NATO.get(&c) { + Some(w) => w.to_string(), + None => c.to_string(), + }) + .collect::>() + .join(" ") +} + +#[cfg(test)] +mod tests { + use super::to_nato; + + #[test] + fn base_to_nato() { + assert_eq!( + to_nato("If you can read"), + "India Foxtrot Yankee Oscar Uniform Charlie Alfa November Romeo Echo Alfa Delta" + ); + + assert_eq!( + to_nato("Did not see that coming",), + "Delta India Delta November Oscar Tango Sierra Echo Echo Tango Hotel Alfa Tango Charlie Oscar Mike India November Golf" + ); + + assert_eq!( + to_nato("go for it!"), + "Golf Oscar Foxtrot Oscar Romeo India Tango !" + ); + } +}