pr_01m3xsjpehe75by7vy5ma74jt2/src/lib.rs
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Greet people from the command line | 1 | //! Greetings. |
| 2 | ||
| Add --lang option to greet in Spanish and French | 3 | /// Languages that can be greeted in, as accepted by `--lang`. |
| 4 | pub const LANGUAGES: [&str; 3] = ["en", "es", "fr"]; | |
| 5 | ||
| 6 | /// The greeting for `name` in the language `lang`. | |
| 7 | /// | |
| 8 | /// Returns an error naming the supported languages if `lang` is unknown. | |
| 9 | pub fn greeting(name: &str, lang: &str) -> Result<String, String> { | |
| 10 | match lang { | |
| 11 | "en" => Ok(format!("Hello, {name}!")), | |
| 12 | "es" => Ok(format!("¡Hola, {name}!")), | |
| 13 | "fr" => Ok(format!("Bonjour, {name} !")), | |
| 14 | _ => Err(format!( | |
| 15 | "unsupported language '{lang}' (supported: {})", | |
| 16 | LANGUAGES.join(", ") | |
| 17 | )), | |
| 18 | } | |
| Greet people from the command line | 19 | } |
| 20 | ||
| 21 | #[cfg(test)] | |
| 22 | mod tests { | |
| 23 | use super::*; | |
| 24 | ||
| 25 | #[test] | |
| 26 | fn greets_by_name() { | |
| Add --lang option to greet in Spanish and French | 27 | assert_eq!(greeting("Ada", "en").unwrap(), "Hello, Ada!"); |
| 28 | } | |
| 29 | ||
| 30 | #[test] | |
| 31 | fn greets_in_spanish() { | |
| 32 | assert_eq!(greeting("Ana", "es").unwrap(), "¡Hola, Ana!"); | |
| 33 | } | |
| 34 | ||
| 35 | #[test] | |
| 36 | fn greets_in_french() { | |
| 37 | assert_eq!(greeting("Luc", "fr").unwrap(), "Bonjour, Luc !"); | |
| 38 | } | |
| 39 | ||
| 40 | #[test] | |
| 41 | fn rejects_unknown_language() { | |
| 42 | let err = greeting("Ada", "de").unwrap_err(); | |
| 43 | assert!(err.contains("en, es, fr")); | |
| Greet people from the command line | 44 | } |
| 45 | } |