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 | ||
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 3 | /// A supported greeting language. |
| 4 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | |
| 5 | pub enum Lang { | |
| 6 | #[default] | |
| 7 | En, | |
| 8 | Es, | |
| 9 | Fr, | |
| 10 | } | |
| 11 | ||
| 12 | impl Lang { | |
| 13 | /// Parses a language code (`en`, `es` or `fr`). | |
| 14 | pub fn parse(code: &str) -> Option<Lang> { | |
| 15 | match code { | |
| 16 | "en" => Some(Lang::En), | |
| 17 | "es" => Some(Lang::Es), | |
| 18 | "fr" => Some(Lang::Fr), | |
| 19 | _ => None, | |
| 20 | } | |
| 21 | } | |
| 22 | } | |
| 23 | ||
| Extract argument parsing into parse_args in lib.rs | 24 | /// Parsed command-line options. |
| Add --times N flag that prints the greeting N times | 25 | #[derive(Debug, PartialEq, Eq)] |
| Extract argument parsing into parse_args in lib.rs | 26 | pub struct Options { |
| 27 | /// The name to greet, if one was given. | |
| 28 | pub name: Option<String>, | |
| Add --shout flag that upper-cases the greeting | 29 | /// Whether to upper-case the greeting (`--shout`). |
| 30 | pub shout: bool, | |
| Add --version flag that prints the crate version and exits | 31 | /// Whether to print the crate version and exit (`--version`). |
| 32 | pub version: bool, | |
| Add --times N flag that prints the greeting N times | 33 | /// How many times to print the greeting (`--times N`). |
| 34 | pub times: usize, | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 35 | /// The greeting language (`--lang CODE`). |
| 36 | pub lang: Lang, | |
| Add --quiet flag that prints nothing | 37 | /// Whether to print nothing (`--quiet`). |
| 38 | pub quiet: bool, | |
| Add --loud flag that ends the greeting with three exclamation marks | 39 | /// Whether to end the greeting with three exclamation marks (`--loud`). |
| 40 | pub loud: bool, | |
| Add --times N flag that prints the greeting N times | 41 | } |
| 42 | ||
| 43 | impl Default for Options { | |
| 44 | fn default() -> Self { | |
| 45 | Options { | |
| 46 | name: None, | |
| 47 | shout: false, | |
| 48 | version: false, | |
| 49 | times: 1, | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 50 | lang: Lang::En, |
| Add --quiet flag that prints nothing | 51 | quiet: false, |
| Add --loud flag that ends the greeting with three exclamation marks | 52 | loud: false, |
| Add --times N flag that prints the greeting N times | 53 | } |
| 54 | } | |
| Extract argument parsing into parse_args in lib.rs | 55 | } |
| 56 | ||
| 57 | /// Parses command-line arguments (excluding the program name). | |
| 58 | /// | |
| Add --times N flag that prints the greeting N times | 59 | /// `--shout` sets [`Options::shout`], `--version` sets |
| 60 | /// [`Options::version`], and `--times N` sets [`Options::times`] (the | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 61 | /// next argument is consumed as the value), and `--lang CODE` sets |
| 62 | /// [`Options::lang`] (likewise consuming the next argument); other | |
| 63 | /// arguments starting with `--` are treated as flags and skipped. The | |
| 64 | /// name is the first remaining argument. | |
| Add --times N flag that prints the greeting N times | 65 | /// |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 66 | /// Returns an error message if `--times` has a missing or non-numeric |
| 67 | /// value, or if `--lang` has a missing or unsupported value. | |
| Add --times N flag that prints the greeting N times | 68 | pub fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Options, String> { |
| Add --shout flag that upper-cases the greeting | 69 | let mut options = Options::default(); |
| Add --times N flag that prints the greeting N times | 70 | let mut args = args.into_iter(); |
| 71 | while let Some(arg) = args.next() { | |
| Add --shout flag that upper-cases the greeting | 72 | if arg == "--shout" { |
| 73 | options.shout = true; | |
| Add --version flag that prints the crate version and exits | 74 | } else if arg == "--version" { |
| 75 | options.version = true; | |
| Add --quiet flag that prints nothing | 76 | } else if arg == "--quiet" { |
| 77 | options.quiet = true; | |
| Add --loud flag that ends the greeting with three exclamation marks | 78 | } else if arg == "--loud" { |
| 79 | options.loud = true; | |
| Add --times N flag that prints the greeting N times | 80 | } else if arg == "--times" { |
| 81 | let value = args | |
| 82 | .next() | |
| 83 | .ok_or_else(|| "--times requires a value".to_owned())?; | |
| 84 | options.times = value | |
| 85 | .parse() | |
| 86 | .map_err(|_| format!("--times value must be a number, got {value:?}"))?; | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 87 | } else if arg == "--lang" { |
| 88 | let value = args | |
| 89 | .next() | |
| 90 | .ok_or_else(|| "--lang requires a value".to_owned())?; | |
| 91 | options.lang = Lang::parse(&value).ok_or_else(|| { | |
| 92 | format!("--lang value must be one of en, es, fr, got {value:?}") | |
| 93 | })?; | |
| Add --shout flag that upper-cases the greeting | 94 | } else if !arg.starts_with("--") && options.name.is_none() { |
| 95 | options.name = Some(arg); | |
| 96 | } | |
| 97 | } | |
| Add --times N flag that prints the greeting N times | 98 | Ok(options) |
| Extract argument parsing into parse_args in lib.rs | 99 | } |
| 100 | ||
| Add --shout flag that upper-cases the greeting | 101 | /// `text` upper-cased. |
| 102 | pub fn shout(text: &str) -> String { | |
| 103 | text.to_uppercase() | |
| 104 | } | |
| 105 | ||
| Add --loud flag that ends the greeting with three exclamation marks | 106 | /// `text` with its final exclamation mark replaced by three. |
| 107 | pub fn loud(text: &str) -> String { | |
| 108 | format!("{}!!!", text.strip_suffix('!').unwrap_or(text)) | |
| 109 | } | |
| 110 | ||
| Greet people from the command line | 111 | /// The greeting for `name`. |
| 112 | pub fn greeting(name: &str) -> String { | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 113 | greeting_in(name, Lang::En) |
| 114 | } | |
| 115 | ||
| 116 | /// The greeting for `name` in `lang`. | |
| 117 | pub fn greeting_in(name: &str, lang: Lang) -> String { | |
| Handle empty and whitespace-only names, trim whitespace from input | 118 | let trimmed = name.trim(); |
| 119 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 120 | match lang { |
| 121 | Lang::En => format!("Hi, {display_name}!"), | |
| 122 | Lang::Es => format!("¡Hola, {display_name}!"), | |
| 123 | Lang::Fr => format!("Bonjour, {display_name} !"), | |
| 124 | } | |
| Greet people from the command line | 125 | } |
| 126 | ||
| Add farewell function alongside greeting | 127 | /// The farewell for `name`. |
| 128 | pub fn farewell(name: &str) -> String { | |
| 129 | let trimmed = name.trim(); | |
| 130 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 131 | format!("Goodbye, {display_name}!") | |
| 132 | } | |
| 133 | ||
| Greet people from the command line | 134 | #[cfg(test)] |
| 135 | mod tests { | |
| 136 | use super::*; | |
| 137 | ||
| Extract argument parsing into parse_args in lib.rs | 138 | fn args(list: &[&str]) -> Vec<String> { |
| 139 | list.iter().map(|s| s.to_string()).collect() | |
| 140 | } | |
| 141 | ||
| 142 | #[test] | |
| Add --quiet flag that prints nothing | 143 | fn parse_args_quiet() { |
| 144 | assert!(parse_args(args(&["--quiet", "Ada"])).unwrap().quiet); | |
| 145 | assert!(!parse_args(args(&["Ada"])).unwrap().quiet); | |
| 146 | } | |
| 147 | ||
| 148 | #[test] | |
| Add --loud flag that ends the greeting with three exclamation marks | 149 | fn parse_args_loud() { |
| 150 | assert!(parse_args(args(&["--loud", "Ada"])).unwrap().loud); | |
| 151 | assert!(!parse_args(args(&["Ada"])).unwrap().loud); | |
| 152 | } | |
| 153 | ||
| 154 | #[test] | |
| 155 | fn loud_uses_three_exclamation_marks() { | |
| 156 | assert_eq!(loud(&greeting("Ada")), "Hi, Ada!!!"); | |
| 157 | } | |
| 158 | ||
| 159 | #[test] | |
| Extract argument parsing into parse_args in lib.rs | 160 | fn parse_args_no_args() { |
| Add --times N flag that prints the greeting N times | 161 | assert_eq!(parse_args(args(&[])).unwrap().name, None); |
| Extract argument parsing into parse_args in lib.rs | 162 | } |
| 163 | ||
| 164 | #[test] | |
| 165 | fn parse_args_name() { | |
| Add --times N flag that prints the greeting N times | 166 | assert_eq!(parse_args(args(&["Ada"])).unwrap().name.as_deref(), Some("Ada")); |
| Extract argument parsing into parse_args in lib.rs | 167 | } |
| 168 | ||
| 169 | #[test] | |
| 170 | fn parse_args_skips_leading_flag() { | |
| 171 | assert_eq!( | |
| Add --times N flag that prints the greeting N times | 172 | parse_args(args(&["--unknown", "Ada"])).unwrap().name.as_deref(), |
| Extract argument parsing into parse_args in lib.rs | 173 | Some("Ada") |
| 174 | ); | |
| 175 | } | |
| 176 | ||
| Greet people from the command line | 177 | #[test] |
| Add --shout flag that upper-cases the greeting | 178 | fn parse_args_shout_defaults_off() { |
| Add --times N flag that prints the greeting N times | 179 | assert!(!parse_args(args(&["Ada"])).unwrap().shout); |
| Add --shout flag that upper-cases the greeting | 180 | } |
| 181 | ||
| 182 | #[test] | |
| 183 | fn parse_args_shout_before_name() { | |
| Add --times N flag that prints the greeting N times | 184 | let options = parse_args(args(&["--shout", "Ada"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 185 | assert!(options.shout); |
| 186 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 187 | } | |
| 188 | ||
| 189 | #[test] | |
| 190 | fn parse_args_shout_after_name() { | |
| Add --times N flag that prints the greeting N times | 191 | let options = parse_args(args(&["Ada", "--shout"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 192 | assert!(options.shout); |
| 193 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 194 | } | |
| 195 | ||
| 196 | #[test] | |
| 197 | fn parse_args_shout_alone() { | |
| Add --times N flag that prints the greeting N times | 198 | let options = parse_args(args(&["--shout"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 199 | assert!(options.shout); |
| 200 | assert_eq!(options.name, None); | |
| 201 | } | |
| 202 | ||
| 203 | #[test] | |
| Add --version flag that prints the crate version and exits | 204 | fn parse_args_version_defaults_off() { |
| Add --times N flag that prints the greeting N times | 205 | assert!(!parse_args(args(&["Ada"])).unwrap().version); |
| Add --version flag that prints the crate version and exits | 206 | } |
| 207 | ||
| 208 | #[test] | |
| 209 | fn parse_args_version() { | |
| Add --times N flag that prints the greeting N times | 210 | let options = parse_args(args(&["--version"])).unwrap(); |
| Add --version flag that prints the crate version and exits | 211 | assert!(options.version); |
| 212 | assert_eq!(options.name, None); | |
| 213 | } | |
| 214 | ||
| 215 | #[test] | |
| Add --times N flag that prints the greeting N times | 216 | fn parse_args_times_defaults_to_one() { |
| 217 | assert_eq!(parse_args(args(&["Ada"])).unwrap().times, 1); | |
| 218 | } | |
| 219 | ||
| 220 | #[test] | |
| 221 | fn parse_args_times_valid() { | |
| 222 | let options = parse_args(args(&["--times", "3", "Ada"])).unwrap(); | |
| 223 | assert_eq!(options.times, 3); | |
| 224 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 225 | } | |
| 226 | ||
| 227 | #[test] | |
| 228 | fn parse_args_times_missing_value() { | |
| 229 | assert!(parse_args(args(&["--times"])).is_err()); | |
| 230 | } | |
| 231 | ||
| 232 | #[test] | |
| 233 | fn parse_args_times_non_number() { | |
| 234 | assert!(parse_args(args(&["--times", "abc", "Ada"])).is_err()); | |
| 235 | } | |
| 236 | ||
| 237 | #[test] | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 238 | fn parse_args_lang_defaults_to_en() { |
| 239 | assert_eq!(parse_args(args(&["Ada"])).unwrap().lang, Lang::En); | |
| 240 | } | |
| 241 | ||
| 242 | #[test] | |
| 243 | fn parse_args_lang_valid() { | |
| 244 | let options = parse_args(args(&["--lang", "es", "Ana"])).unwrap(); | |
| 245 | assert_eq!(options.lang, Lang::Es); | |
| 246 | assert_eq!(options.name.as_deref(), Some("Ana")); | |
| 247 | assert_eq!(parse_args(args(&["--lang", "fr"])).unwrap().lang, Lang::Fr); | |
| 248 | } | |
| 249 | ||
| 250 | #[test] | |
| 251 | fn parse_args_lang_missing_value() { | |
| 252 | assert_eq!( | |
| 253 | parse_args(args(&["--lang"])).unwrap_err(), | |
| 254 | "--lang requires a value" | |
| 255 | ); | |
| 256 | } | |
| 257 | ||
| 258 | #[test] | |
| 259 | fn parse_args_lang_unknown() { | |
| 260 | assert_eq!( | |
| 261 | parse_args(args(&["--lang", "de"])).unwrap_err(), | |
| 262 | "--lang value must be one of en, es, fr, got \"de\"" | |
| 263 | ); | |
| 264 | } | |
| 265 | ||
| 266 | #[test] | |
| 267 | fn greeting_in_each_language() { | |
| 268 | assert_eq!(greeting_in("Ada", Lang::En), "Hi, Ada!"); | |
| 269 | assert_eq!(greeting_in("Ana", Lang::Es), "¡Hola, Ana!"); | |
| 270 | assert_eq!(greeting_in("Luc", Lang::Fr), "Bonjour, Luc !"); | |
| 271 | } | |
| 272 | ||
| 273 | #[test] | |
| 274 | fn greeting_in_trims_and_falls_back_to_world() { | |
| 275 | assert_eq!(greeting_in(" Ana ", Lang::Es), "¡Hola, Ana!"); | |
| 276 | assert_eq!(greeting_in(" ", Lang::Fr), "Bonjour, world !"); | |
| 277 | } | |
| 278 | ||
| 279 | #[test] | |
| Add --shout flag that upper-cases the greeting | 280 | fn shout_upper_cases_greeting() { |
| Merge #42: Greet with Hi instead of Hello | 281 | assert_eq!(shout(&greeting("Ada")), "HI, ADA!"); |
| Add --shout flag that upper-cases the greeting | 282 | } |
| 283 | ||
| 284 | #[test] | |
| Greet people from the command line | 285 | fn greets_by_name() { |
| Merge #42: Greet with Hi instead of Hello | 286 | assert_eq!(greeting("Ada"), "Hi, Ada!"); |
| Greet people from the command line | 287 | } |
| Handle empty and whitespace-only names, trim whitespace from input | 288 | |
| 289 | #[test] | |
| 290 | fn greets_world_with_empty_string() { | |
| Merge #42: Greet with Hi instead of Hello | 291 | assert_eq!(greeting(""), "Hi, world!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 292 | } |
| 293 | ||
| 294 | #[test] | |
| 295 | fn greets_world_with_whitespace_only() { | |
| Merge #42: Greet with Hi instead of Hello | 296 | assert_eq!(greeting(" "), "Hi, world!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 297 | } |
| 298 | ||
| 299 | #[test] | |
| 300 | fn trims_whitespace_from_name() { | |
| Merge #42: Greet with Hi instead of Hello | 301 | assert_eq!(greeting(" Ada "), "Hi, Ada!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 302 | } |
| Add farewell function alongside greeting | 303 | |
| 304 | #[test] | |
| 305 | fn says_goodbye_by_name() { | |
| 306 | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); | |
| 307 | } | |
| 308 | ||
| 309 | #[test] | |
| 310 | fn says_goodbye_to_world_when_blank() { | |
| 311 | assert_eq!(farewell(" "), "Goodbye, world!"); | |
| 312 | } | |
| 313 | ||
| 314 | #[test] | |
| 315 | fn farewell_trims_whitespace() { | |
| 316 | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); | |
| 317 | } | |
| Greet people from the command line | 318 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.