use std::collections::HashMap;
struct Command {
code: char,
description: &'static str,
}
fn translation_build() -> HashMap<&'static str, String> {
let mut map = HashMap::new();
for (key, value) in [
("START", "Start your instance"),
("STOP", "Stop your instance"),
("QUIT", "Quit"),
("STARTING", "Starting instance"),
("STOPPING", "Stopping instance"),
("QUITTING", "Quitting"),
("UNKNOWN_CMD", "Invalid option"),
] {
map.insert(key, value.to_string());
}
map.shrink_to_fit();
map
}
fn translation_lookup(key: &str) -> Option<&'static str> {
use once_cell::sync::Lazy;
static STRING_TABLE: Lazy<HashMap<&'static str, String>> = Lazy::new(translation_build);
(&*STRING_TABLE).get(key).map(String::as_str)
}
fn main() {
let commands = [
Command {
code: '1',
description: translation_lookup("START").unwrap_or("START"),
},
Command {
code: '2',
description: translation_lookup("STOP").unwrap_or("STOP"),
},
Command {
code: '3',
description: translation_lookup("QUIT").unwrap_or("QUIT"),
},
];
for c in commands {
println!(" {}: {}", c.code, c.description);
}
}