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; | |
| Accept -L as a short alias for --loud | 78 | } else if arg == "--loud" || arg == "-L" { |
| Add --loud flag that ends the greeting with three exclamation marks | 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`. |
| Rename greeting() to greet() | 112 | pub fn greet(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); | |
| Accept -L as a short alias for --loud | 151 | assert!(parse_args(args(&["-L", "Ada"])).unwrap().loud); |
| Add --loud flag that ends the greeting with three exclamation marks | 152 | assert!(!parse_args(args(&["Ada"])).unwrap().loud); |
| 153 | } | |
| 154 | ||
| 155 | #[test] | |
| 156 | fn loud_uses_three_exclamation_marks() { | |
| Rename greeting() to greet() | 157 | assert_eq!(loud(&greet("Ada")), "Hi, Ada!!!"); |
| Add --loud flag that ends the greeting with three exclamation marks | 158 | } |
| 159 | ||
| 160 | #[test] | |
| Extract argument parsing into parse_args in lib.rs | 161 | fn parse_args_no_args() { |
| Add --times N flag that prints the greeting N times | 162 | assert_eq!(parse_args(args(&[])).unwrap().name, None); |
| Extract argument parsing into parse_args in lib.rs | 163 | } |
| 164 | ||
| 165 | #[test] | |
| 166 | fn parse_args_name() { | |
| Add --times N flag that prints the greeting N times | 167 | assert_eq!(parse_args(args(&["Ada"])).unwrap().name.as_deref(), Some("Ada")); |
| Extract argument parsing into parse_args in lib.rs | 168 | } |
| 169 | ||
| 170 | #[test] | |
| 171 | fn parse_args_skips_leading_flag() { | |
| 172 | assert_eq!( | |
| Add --times N flag that prints the greeting N times | 173 | parse_args(args(&["--unknown", "Ada"])).unwrap().name.as_deref(), |
| Extract argument parsing into parse_args in lib.rs | 174 | Some("Ada") |
| 175 | ); | |
| 176 | } | |
| 177 | ||
| Greet people from the command line | 178 | #[test] |
| Add --shout flag that upper-cases the greeting | 179 | fn parse_args_shout_defaults_off() { |
| Add --times N flag that prints the greeting N times | 180 | assert!(!parse_args(args(&["Ada"])).unwrap().shout); |
| Add --shout flag that upper-cases the greeting | 181 | } |
| 182 | ||
| 183 | #[test] | |
| 184 | fn parse_args_shout_before_name() { | |
| Add --times N flag that prints the greeting N times | 185 | let options = parse_args(args(&["--shout", "Ada"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 186 | assert!(options.shout); |
| 187 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 188 | } | |
| 189 | ||
| 190 | #[test] | |
| 191 | fn parse_args_shout_after_name() { | |
| Add --times N flag that prints the greeting N times | 192 | let options = parse_args(args(&["Ada", "--shout"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 193 | assert!(options.shout); |
| 194 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 195 | } | |
| 196 | ||
| 197 | #[test] | |
| 198 | fn parse_args_shout_alone() { | |
| Add --times N flag that prints the greeting N times | 199 | let options = parse_args(args(&["--shout"])).unwrap(); |
| Add --shout flag that upper-cases the greeting | 200 | assert!(options.shout); |
| 201 | assert_eq!(options.name, None); | |
| 202 | } | |
| 203 | ||
| 204 | #[test] | |
| Add --version flag that prints the crate version and exits | 205 | fn parse_args_version_defaults_off() { |
| Add --times N flag that prints the greeting N times | 206 | assert!(!parse_args(args(&["Ada"])).unwrap().version); |
| Add --version flag that prints the crate version and exits | 207 | } |
| 208 | ||
| 209 | #[test] | |
| 210 | fn parse_args_version() { | |
| Add --times N flag that prints the greeting N times | 211 | let options = parse_args(args(&["--version"])).unwrap(); |
| Add --version flag that prints the crate version and exits | 212 | assert!(options.version); |
| 213 | assert_eq!(options.name, None); | |
| 214 | } | |
| 215 | ||
| 216 | #[test] | |
| Add --times N flag that prints the greeting N times | 217 | fn parse_args_times_defaults_to_one() { |
| 218 | assert_eq!(parse_args(args(&["Ada"])).unwrap().times, 1); | |
| 219 | } | |
| 220 | ||
| 221 | #[test] | |
| 222 | fn parse_args_times_valid() { | |
| 223 | let options = parse_args(args(&["--times", "3", "Ada"])).unwrap(); | |
| 224 | assert_eq!(options.times, 3); | |
| 225 | assert_eq!(options.name.as_deref(), Some("Ada")); | |
| 226 | } | |
| 227 | ||
| 228 | #[test] | |
| 229 | fn parse_args_times_missing_value() { | |
| 230 | assert!(parse_args(args(&["--times"])).is_err()); | |
| 231 | } | |
| 232 | ||
| 233 | #[test] | |
| 234 | fn parse_args_times_non_number() { | |
| 235 | assert!(parse_args(args(&["--times", "abc", "Ada"])).is_err()); | |
| 236 | } | |
| 237 | ||
| 238 | #[test] | |
| Merge #53: Add a --lang option (en, es, fr) to the greeter | 239 | fn parse_args_lang_defaults_to_en() { |
| 240 | assert_eq!(parse_args(args(&["Ada"])).unwrap().lang, Lang::En); | |
| 241 | } | |
| 242 | ||
| 243 | #[test] | |
| 244 | fn parse_args_lang_valid() { | |
| 245 | let options = parse_args(args(&["--lang", "es", "Ana"])).unwrap(); | |
| 246 | assert_eq!(options.lang, Lang::Es); | |
| 247 | assert_eq!(options.name.as_deref(), Some("Ana")); | |
| 248 | assert_eq!(parse_args(args(&["--lang", "fr"])).unwrap().lang, Lang::Fr); | |
| 249 | } | |
| 250 | ||
| 251 | #[test] | |
| 252 | fn parse_args_lang_missing_value() { | |
| 253 | assert_eq!( | |
| 254 | parse_args(args(&["--lang"])).unwrap_err(), | |
| 255 | "--lang requires a value" | |
| 256 | ); | |
| 257 | } | |
| 258 | ||
| 259 | #[test] | |
| 260 | fn parse_args_lang_unknown() { | |
| 261 | assert_eq!( | |
| 262 | parse_args(args(&["--lang", "de"])).unwrap_err(), | |
| 263 | "--lang value must be one of en, es, fr, got \"de\"" | |
| 264 | ); | |
| 265 | } | |
| 266 | ||
| 267 | #[test] | |
| 268 | fn greeting_in_each_language() { | |
| 269 | assert_eq!(greeting_in("Ada", Lang::En), "Hi, Ada!"); | |
| 270 | assert_eq!(greeting_in("Ana", Lang::Es), "¡Hola, Ana!"); | |
| 271 | assert_eq!(greeting_in("Luc", Lang::Fr), "Bonjour, Luc !"); | |
| 272 | } | |
| 273 | ||
| 274 | #[test] | |
| 275 | fn greeting_in_trims_and_falls_back_to_world() { | |
| 276 | assert_eq!(greeting_in(" Ana ", Lang::Es), "¡Hola, Ana!"); | |
| 277 | assert_eq!(greeting_in(" ", Lang::Fr), "Bonjour, world !"); | |
| 278 | } | |
| 279 | ||
| 280 | #[test] | |
| Add --shout flag that upper-cases the greeting | 281 | fn shout_upper_cases_greeting() { |
| Rename greeting() to greet() | 282 | assert_eq!(shout(&greet("Ada")), "HI, ADA!"); |
| Add --shout flag that upper-cases the greeting | 283 | } |
| 284 | ||
| 285 | #[test] | |
| Greet people from the command line | 286 | fn greets_by_name() { |
| Rename greeting() to greet() | 287 | assert_eq!(greet("Ada"), "Hi, Ada!"); |
| Greet people from the command line | 288 | } |
| Handle empty and whitespace-only names, trim whitespace from input | 289 | |
| 290 | #[test] | |
| 291 | fn greets_world_with_empty_string() { | |
| Rename greeting() to greet() | 292 | assert_eq!(greet(""), "Hi, world!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 293 | } |
| 294 | ||
| 295 | #[test] | |
| 296 | fn greets_world_with_whitespace_only() { | |
| Rename greeting() to greet() | 297 | assert_eq!(greet(" "), "Hi, world!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 298 | } |
| 299 | ||
| 300 | #[test] | |
| 301 | fn trims_whitespace_from_name() { | |
| Rename greeting() to greet() | 302 | assert_eq!(greet(" Ada "), "Hi, Ada!"); |
| Handle empty and whitespace-only names, trim whitespace from input | 303 | } |
| Add farewell function alongside greeting | 304 | |
| 305 | #[test] | |
| 306 | fn says_goodbye_by_name() { | |
| 307 | assert_eq!(farewell("Ada"), "Goodbye, Ada!"); | |
| 308 | } | |
| 309 | ||
| 310 | #[test] | |
| 311 | fn says_goodbye_to_world_when_blank() { | |
| 312 | assert_eq!(farewell(" "), "Goodbye, world!"); | |
| 313 | } | |
| 314 | ||
| 315 | #[test] | |
| 316 | fn farewell_trims_whitespace() { | |
| 317 | assert_eq!(farewell(" Ada "), "Goodbye, Ada!"); | |
| 318 | } | |
| Greet people from the command line | 319 | } |
This file's history is long; its oldest lines are credited to the oldest commit read.