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 --whisper flag | 31 | /// Whether to lower-case the greeting and end it with `...` (`--whisper`). |
| 32 | pub whisper: bool, | |
| Add --reverse flag | 33 | /// Whether to spell the name backwards (`--reverse`). |
| 34 | pub reverse: bool, | |
| Add --version flag that prints the crate version and exits | 35 | /// Whether to print the crate version and exit (`--version`). |
| 36 | pub version: bool, | |
| Add --times N flag that prints the greeting N times | 37 | /// How many times to print the greeting (`--times N`). |
| 38 | pub times: usize, | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 39 | /// The greeting language (`--lang CODE`). |
| 40 | pub lang: Lang, | |
| Add --quiet flag that prints nothing | 41 | /// Whether to print nothing (`--quiet`). |
| 42 | pub quiet: bool, | |
| Add --loud flag that ends the greeting with three exclamation marks | 43 | /// Whether to end the greeting with three exclamation marks (`--loud`). |
| 44 | pub loud: bool, | |
| Merge #64: Add a --bye flag | 45 | /// Whether to print a goodbye after the greeting (`--bye`). |
| 46 | pub bye: bool, | |
| Add --wave flag that prints o/ after the greeting | 47 | /// Whether to print a wave after the greeting (`--wave`). |
| 48 | pub wave: bool, | |
| Catch up with main | 49 | /// Whether to print the greeting and a goodbye on one line (`--both`). |
| 50 | pub both: bool, | |
| Add --times N flag that prints the greeting N times | 51 | } |
| 52 | ||
| 53 | impl Default for Options { | |
| 54 | fn default() -> Self { | |
| 55 | Options { | |
| 56 | name: None, | |
| 57 | shout: false, | |
| Add --whisper flag | 58 | whisper: false, |
| Add --reverse flag | 59 | reverse: false, |
| Add --times N flag that prints the greeting N times | 60 | version: false, |
| 61 | times: 1, | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 62 | lang: Lang::En, |
| Add --quiet flag that prints nothing | 63 | quiet: false, |
| Add --loud flag that ends the greeting with three exclamation marks | 64 | loud: false, |
| Merge #64: Add a --bye flag | 65 | bye: false, |
| Add --wave flag that prints o/ after the greeting | 66 | wave: false, |
| Catch up with main | 67 | both: false, |
| Add --times N flag that prints the greeting N times | 68 | } |
| 69 | } | |
| Extract argument parsing into parse_args in lib.rs | 70 | } |
| 71 | ||
| 72 | /// Parses command-line arguments (excluding the program name). | |
| 73 | /// | |
| Add --times N flag that prints the greeting N times | 74 | /// `--shout` sets [`Options::shout`], `--version` sets |
| 75 | /// [`Options::version`], and `--times N` sets [`Options::times`] (the | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 76 | /// next argument is consumed as the value), and `--lang CODE` sets |
| 77 | /// [`Options::lang`] (likewise consuming the next argument); other | |
| 78 | /// arguments starting with `--` are treated as flags and skipped. The | |
| 79 | /// name is the first remaining argument. | |
| Add --times N flag that prints the greeting N times | 80 | /// |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 81 | /// Returns an error message if `--times` has a missing or non-numeric |
| 82 | /// value, or if `--lang` has a missing or unsupported value. | |
| Add --times N flag that prints the greeting N times | 83 | pub fn parse_args<I: IntoIterator<Item = String>>(args: I) -> Result<Options, String> { |
| Add --shout flag that upper-cases the greeting | 84 | let mut options = Options::default(); |
| Add --times N flag that prints the greeting N times | 85 | let mut args = args.into_iter(); |
| 86 | while let Some(arg) = args.next() { | |
| Add --shout flag that upper-cases the greeting | 87 | if arg == "--shout" { |
| 88 | options.shout = true; | |
| Add --whisper flag | 89 | options.whisper = false; |
| 90 | } else if arg == "--whisper" { | |
| 91 | options.whisper = true; | |
| 92 | options.shout = false; | |
| Add --reverse flag | 93 | } else if arg == "--reverse" { |
| 94 | options.reverse = true; | |
| Add --version flag that prints the crate version and exits | 95 | } else if arg == "--version" { |
| 96 | options.version = true; | |
| Add --quiet flag that prints nothing | 97 | } else if arg == "--quiet" { |
| 98 | options.quiet = true; | |
| Accept -L as a short alias for --loud | 99 | } else if arg == "--loud" || arg == "-L" { |
| Add --loud flag that ends the greeting with three exclamation marks | 100 | options.loud = true; |
| Merge #64: Add a --bye flag | 101 | } else if arg == "--bye" { |
| 102 | options.bye = true; | |
| Add --wave flag that prints o/ after the greeting | 103 | } else if arg == "--wave" { |
| 104 | options.wave = true; | |
| Catch up with main | 105 | } else if arg == "--both" { |
| 106 | options.both = true; | |
| Add --times N flag that prints the greeting N times | 107 | } else if arg == "--times" { |
| 108 | let value = args | |
| 109 | .next() | |
| 110 | .ok_or_else(|| "--times requires a value".to_owned())?; | |
| 111 | options.times = value | |
| 112 | .parse() | |
| 113 | .map_err(|_| format!("--times value must be a number, got {value:?}"))?; | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 114 | } else if arg == "--lang" { |
| 115 | let value = args | |
| 116 | .next() | |
| 117 | .ok_or_else(|| "--lang requires a value".to_owned())?; | |
| Add CI workflow for formatting, lints and tests | 118 | options.lang = Lang::parse(&value) |
| 119 | .ok_or_else(|| format!("--lang value must be one of en, es, fr, got {value:?}"))?; | |
| Add --shout flag that upper-cases the greeting | 120 | } else if !arg.starts_with("--") && options.name.is_none() { |
| 121 | options.name = Some(arg); | |
| 122 | } | |
| 123 | } | |
| Add --times N flag that prints the greeting N times | 124 | Ok(options) |
| Extract argument parsing into parse_args in lib.rs | 125 | } |
| 126 | ||
| Add --reverse flag | 127 | /// `name` spelled backwards. |
| 128 | pub fn reverse(name: &str) -> String { | |
| 129 | name.chars().rev().collect() | |
| 130 | } | |
| 131 | ||
| Add --shout flag that upper-cases the greeting | 132 | /// `text` upper-cased. |
| 133 | pub fn shout(text: &str) -> String { | |
| 134 | text.to_uppercase() | |
| 135 | } | |
| 136 | ||
| Add --whisper flag | 137 | /// `text` lower-cased, with its final exclamation mark replaced by `...`. |
| 138 | pub fn whisper(text: &str) -> String { | |
| 139 | let body = text.to_lowercase(); | |
| 140 | format!("{}...", body.trim_end_matches('!').trim_end()) | |
| 141 | } | |
| 142 | ||
| Add --loud flag that ends the greeting with three exclamation marks | 143 | /// `text` with its final exclamation mark replaced by three. |
| 144 | pub fn loud(text: &str) -> String { | |
| 145 | format!("{}!!!", text.strip_suffix('!').unwrap_or(text)) | |
| 146 | } | |
| 147 | ||
| Greet people from the command line | 148 | /// The greeting for `name`. |
| Rename hail() and part() to greet() and farewell() | 149 | pub fn greet(name: &str) -> String { |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 150 | greeting_in(name, Lang::En) |
| 151 | } | |
| 152 | ||
| 153 | /// The greeting for `name` in `lang`. | |
| 154 | pub fn greeting_in(name: &str, lang: Lang) -> String { | |
| Handle empty and whitespace-only names, trim whitespace from input | 155 | let trimmed = name.trim(); |
| 156 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 157 | match lang { |
| 158 | Lang::En => format!("Hi, {display_name}!"), | |
| 159 | Lang::Es => format!("¡Hola, {display_name}!"), | |
| 160 | Lang::Fr => format!("Bonjour, {display_name} !"), | |
| 161 | } | |
| Greet people from the command line | 162 | } |
| 163 | ||
| Add --wave flag that prints o/ after the greeting | 164 | /// The wave printed by `--wave`. |
| 165 | pub fn wave() -> &'static str { | |
| 166 | "o/" | |
| 167 | } | |
| 168 | ||
| Add farewell function alongside greeting | 169 | /// The farewell for `name`. |
| Rename hail() and part() to greet() and farewell() | 170 | pub fn farewell(name: &str) -> String { |
| Add farewell function alongside greeting | 171 | let trimmed = name.trim(); |
| 172 | let display_name = if trimmed.is_empty() { "world" } else { trimmed }; | |
| 173 | format!("Goodbye, {display_name}!") | |
| 174 | } | |
| 175 | ||
| Greet people from the command line | 176 | #[cfg(test)] |
| 177 | mod tests { | |
| 178 | use super::*; | |
| 179 | ||
| Extract argument parsing into parse_args in lib.rs | 180 | fn args(list: &[&str]) -> Vec<String> { |
| 181 | list.iter().map(|s| s.to_string()).collect() | |
| 182 | } | |
| 183 | ||
| 184 | #[test] | |
| Add --quiet flag that prints nothing | 185 | fn parse_args_quiet() { |
| 186 | assert!(parse_args(args(&["--quiet", "Ada"])).unwrap().quiet); | |
| 187 | assert!(!parse_args(args(&["Ada"])).unwrap().quiet); | |
| 188 | } | |
| 189 | ||
| 190 | #[test] | |
| Merge #64: Add a --bye flag | 191 | fn parse_args_bye() { |
| 192 | assert!(parse_args(args(&["--bye", "Ada"])).unwrap().bye); | |
| 193 | assert!(!parse_args(args(&["Ada"])).unwrap().bye); | |
| 194 | } | |
| 195 | ||
| 196 | #[test] | |
| Add --wave flag that prints o/ after the greeting | 197 | fn parse_args_wave() { |
| 198 | assert!(parse_args(args(&["--wave", "Ada"])).unwrap().wave); | |
| 199 | assert!(!parse_args(args(&["Ada"])).unwrap().wave); | |
| 200 | } | |
| 201 | ||
| 202 | #[test] | |
| Catch up with main | 203 | fn parse_args_both() { |
| 204 | assert!(parse_args(args(&["--both", "Ada"])).unwrap().both); | |
| 205 | assert!(!parse_args(args(&["Ada"])).unwrap().both); | |
| 206 | } | |
| 207 | ||
| 208 | #[test] | |
| Add --wave flag that prints o/ after the greeting | 209 | fn wave_is_o_slash() { |
| 210 | assert_eq!(wave(), "o/"); | |
| 211 | } | |
| 212 | ||
| 213 | #[test] | |
| Add --loud flag that ends the greeting with three exclamation marks | 214 | fn parse_args_loud() { |
| 215 | assert!(parse_args(args(&["--loud", "Ada"])).unwrap().loud); | |
| Accept -L as a short alias for --loud | 216 | assert!(parse_args(args(&["-L", "Ada"])).unwrap().loud); |
| Add --loud flag that ends the greeting with three exclamation marks | 217 | assert!(!parse_args(args(&["Ada"])).unwrap().loud); |
| 218 | } | |
| 219 | ||
| 220 | #[test] | |
| 221 | fn loud_uses_three_exclamation_marks() { | |
| Rename hail() and part() to greet() and farewell() | 222 | assert_eq!(loud(&greet("Ada")), "Hi, Ada!!!"); |
| Add --loud flag that ends the greeting with three exclamation marks | 223 | } |
| 224 | ||
| 225 | #[test] | |
| Extract argument parsing into parse_args in lib.rs | 226 | fn parse_args_no_args() { |
| Add --times N flag that prints the greeting N times | 227 | assert_eq!(parse_args(args(&[])).unwrap().name, None); |
| Extract argument parsing into parse_args in lib.rs | 228 | } |
| 229 | ||
| 230 | #[test] | |
| 231 | fn parse_args_name() { | |
| Add CI workflow for formatting, lints and tests | 232 | assert_eq!( |
| 233 | parse_args(args(&["Ada"])).unwrap().name.as_deref(), | |
| 234 | Some("Ada") | |
| 235 | ); | |
| Extract argument parsing into parse_args in lib.rs | 236 | } |
| 237 | ||
| 238 | #[test] | |
| 239 | fn parse_args_skips_leading_flag() { | |
| 240 | assert_eq!( | |
| Add CI workflow for formatting, lints and tests | 241 | parse_args(args(&["--unknown", "Ada"])) |
| 242 | .unwrap() | |
| 243 | .name | |
| 244 | .as_deref(), | |
| Extract argument parsing into parse_args in lib.rs | 245 | Some("Ada") |
| 246 | ); | |
| 247 | } | |
| 248 | ||
| Greet people from the command line | 249 | #[test] |
| Add --shout flag that upper-cases the greeting | 250 | fn parse_args_shout_defaults_off() { |
| Add --times N flag that prints the greeting N times | 251 | assert!(!parse_args(args(&["Ada"])).unwrap().shout); |
| Add --shout flag that upper-cases the greeting | 252 | } |
| 253 | ||
| 254 | #[test] | |
| 255 | fn parse_args_shout_before_name() { | |
| Add --times N flag that prints the greeting N times | 256 | let options = parse_args(args(&["--shout", "Ada"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 257 | assert!(options.shout); |
| 258 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 259 | } | |
| 260 | ||
| 261 | #[test] | |
| 262 | fn parse_args_shout_after_name() { | |
| Add --times N flag that prints the greeting N times | 263 | let options = parse_args(args(&["Ada", "--shout"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 264 | assert!(options.shout); |
| 265 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 266 | } | |
| 267 | ||
| 268 | #[test] | |
| 269 | fn parse_args_shout_alone() { | |
| Add --times N flag that prints the greeting N times | 270 | let options = parse_args(args(&["--shout"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 271 | assert!(options.shout); |
| 272 | assert_eq!(options.name, None); | |
| 273 | } | |
| 274 | ||
| 275 | #[test] | |
| Add --version flag that prints the crate version and exits | 276 | fn parse_args_version_defaults_off() { |
| Add --times N flag that prints the greeting N times | 277 | assert!(!parse_args(args(&["Ada"])).unwrap().version); |
| Add --version flag that prints the crate version and exits | 278 | } |
| 279 | ||
| 280 | #[test] | |
| 281 | fn parse_args_version() { | |
| Add --times N flag that prints the greeting N times | 282 | let options = parse_args(args(&["--version"])).unwrap(); |
| Add --version flag that prints the crate version and exits | 283 | assert!(options.version); |
| 284 | assert_eq!(options.name, None); | |
| 285 | } | |
| 286 | ||
| 287 | #[test] | |
| Add --times N flag that prints the greeting N times | 288 | fn parse_args_times_defaults_to_one() { |
| 289 | assert_eq!(parse_args(args(&["Ada"])).unwrap().times, 1); | |
| 290 | } | |
| 291 | ||
| 292 | #[test] | |
| 293 | fn parse_args_times_valid() { | |
| 294 | let options = parse_args(args(&["--times", "3", "Ada"])).unwrap(); | |
| 295 | assert_eq!(options.times, 3); | |
| 296 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 297 | } | |
| 298 | ||
| 299 | #[test] | |
| 300 | fn parse_args_times_missing_value() { | |
| 301 | assert!(parse_args(args(&["--times"])).is_err()); | |
| 302 | } | |
| 303 | ||
| 304 | #[test] | |
| 305 | fn parse_args_times_non_number() { | |
| 306 | assert!(parse_args(args(&["--times", "abc", "Ada"])).is_err()); | |
| 307 | } | |
| 308 | ||
| 309 | #[test] | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 310 | fn parse_args_lang_defaults_to_en() { |
| 311 | assert_eq!(parse_args(args(&["Ada"])).unwrap().lang, Lang::En); | |
| 312 | } | |
| 313 | ||
| 314 | #[test] | |
| 315 | fn parse_args_lang_valid() { | |
| 316 | let options = parse_args(args(&["--lang", "es", "Ana"])).unwrap(); | |
| 317 | assert_eq!(options.lang, Lang::Es); | |
| 318 | assert_eq!(options.name.as_deref(), Some("Ana")); | |
| 319 | assert_eq!(parse_args(args(&["--lang", "fr"])).unwrap().lang, Lang::Fr); | |
| 320 | } | |
| 321 | ||
| 322 | #[test] | |
| 323 | fn parse_args_lang_missing_value() { | |
| 324 | assert_eq!( | |
| 325 | parse_args(args(&["--lang"])).unwrap_err(), | |
| 326 | "--lang requires a value" | |
| 327 | ); | |
| 328 | } | |
| 329 | ||
| 330 | #[test] | |
| 331 | fn parse_args_lang_unknown() { | |
| 332 | assert_eq!( | |
| 333 | parse_args(args(&["--lang", "de"])).unwrap_err(), | |
| 334 | "--lang value must be one of en, es, fr, got \"de\"" | |
| 335 | ); | |
| 336 | } | |
| 337 | ||
| 338 | #[test] | |
| 339 | fn greeting_in_each_language() { | |
| 340 | assert_eq!(greeting_in("Ada", Lang::En), "Hi, Ada!"); | |
| 341 | assert_eq!(greeting_in("Ana", Lang::Es), "¡Hola, Ana!"); | |
| 342 | assert_eq!(greeting_in("Luc", Lang::Fr), "Bonjour, Luc !"); | |
| 343 | } | |
| 344 | ||
| 345 | #[test] | |
| 346 | fn greeting_in_trims_and_falls_back_to_world() { | |
| 347 | assert_eq!(greeting_in(" Ana ", Lang::Es), "¡Hola, Ana!"); | |
| 348 | assert_eq!(greeting_in(" ", Lang::Fr), "Bonjour, world !"); | |
| 349 | } | |
| 350 | ||
| 351 | #[test] | |
| Add --shout flag that upper-cases the greeting | 352 | fn shout_upper_cases_greeting() { |
| Rename hail() and part() to greet() and farewell() | 353 | assert_eq!(shout(&greet("Ada")), "HI, ADA!"); |
| Add --shout flag that upper-cases the greeting | 354 | } |
| 355 | ||
| 356 | #[test] | |
| Add --whisper flag | 357 | fn whisper_lower_cases_and_trails_off() { |
| Rename hail() and part() to greet() and farewell() | 358 | assert_eq!(whisper(&greet("Ada")), "hi, ada..."); |
| Add --whisper flag | 359 | assert_eq!(whisper(&greeting_in("Luc", Lang::Fr)), "bonjour, luc..."); |
| 360 | } | |
| 361 | ||
| 362 | #[test] | |
| 363 | fn parse_args_whisper_and_shout_last_wins() { | |
| 364 | let o = parse_args(args(&["--whisper", "Ada"])).unwrap(); | |
| 365 | assert!(o.whisper && !o.shout); | |
| 366 | let o = parse_args(args(&["--shout", "--whisper"])).unwrap(); | |
| 367 | assert!(o.whisper && !o.shout); | |
| 368 | let o = parse_args(args(&["--whisper", "--shout"])).unwrap(); | |
| 369 | assert!(o.shout && !o.whisper); | |
| 370 | } | |
| 371 | ||
| 372 | #[test] | |
| Add --reverse flag | 373 | fn parse_args_reverse() { |
| 374 | assert!(parse_args(args(&["--reverse", "Ada"])).unwrap().reverse); | |
| 375 | assert!(!parse_args(args(&["Ada"])).unwrap().reverse); | |
| 376 | } | |
| 377 | ||
| 378 | #[test] | |
| 379 | fn reverse_spells_backwards() { | |
| Rename hail() and part() to greet() and farewell() | 380 | assert_eq!(greet(&reverse("Ada")), "Hi, adA!"); |
| Add --reverse flag | 381 | } |
| 382 | ||
| 383 | #[test] | |
| Greet people from the command line | 384 | fn greets_by_name() { |
| Rename hail() and part() to greet() and farewell() | 385 | assert_eq!(greet("Ada"), "Hi, Ada!"); |
| Greet people from the command line | 386 | } |
| Handle empty and whitespace-only names, trim whitespace from input | 387 | |
| 388 | #[test] | |
| 389 | fn greets_world_with_empty_string() { | |
| Rename hail() and part() to greet() and farewell() | 390 | assert_eq!(greet(""), "Hi, world!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 391 | } |
| 392 | ||
| 393 | #[test] | |
| 394 | fn greets_world_with_whitespace_only() { | |
| Rename hail() and part() to greet() and farewell() | 395 | assert_eq!(greet(" "), "Hi, world!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 396 | } |
| 397 | ||
| 398 | #[test] | |
| 399 | fn trims_whitespace_from_name() { | |
| Rename hail() and part() to greet() and farewell() | 400 | assert_eq!(greet(" Ada "), "Hi, Ada!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 401 | } |
| Add farewell function alongside greeting | 402 | |
| 403 | #[test] | |
| 404 | fn says_goodbye_by_name() { | |
| Rename hail() and part() to greet() and farewell() | 405 | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); |
| Add farewell function alongside greeting | 406 | } |
| 407 | ||
| 408 | #[test] | |
| 409 | fn says_goodbye_to_world_when_blank() { | |
| Rename hail() and part() to greet() and farewell() | 410 | assert_eq!(farewell(" "), "Goodbye, world!"); |
| Add farewell function alongside greeting | 411 | } |
| 412 | ||
| 413 | #[test] | |
| 414 | fn farewell_trims_whitespace() { | |
| Rename hail() and part() to greet() and farewell() | 415 | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); |
| Add farewell function alongside greeting | 416 | } |
| Greet people from the command line | 417 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.