feat(rust): add can you read kata

This commit is contained in:
Dmitriy Pleshevskiy 2022-04-20 23:38:58 +03:00
parent 8fa41c4e4c
commit 2fb615473c
3 changed files with 65 additions and 1 deletions

9
rust/Cargo.lock generated
View File

@ -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",
]

View File

@ -5,4 +5,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[dependencies]
once_cell = "1.10.0"

View File

@ -0,0 +1,54 @@
use once_cell::sync::Lazy;
use std::collections::HashMap;
#[rustfmt::skip]
pub static NATO: Lazy<HashMap<char, &'static str>> = 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::<Vec<_>>()
.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 !"
);
}
}