| 28 | 28 | | pub name: Option<String>, |
| 29 | 29 | | /// Whether to upper-case the greeting (`--shout`). |
| 30 | 30 | | pub shout: bool, |
| 31 | + | /// Whether to lower-case the greeting and end it with `...` (`--whisper`). |
| 32 | + | pub whisper: bool, |
| 31 | 33 | | /// Whether to print the crate version and exit (`--version`). |
| 32 | 34 | | pub version: bool, |
| 33 | 35 | | /// How many times to print the greeting (`--times N`). |
| ⋯ |
| 49 | 51 | | Options { |
| 50 | 52 | | name: None, |
| 51 | 53 | | shout: false, |
| 54 | + | whisper: false, |
| 52 | 55 | | version: false, |
| 53 | 56 | | times: 1, |
| 54 | 57 | | lang: Lang::En, |
| ⋯ |
| 77 | 80 | | while let Some(arg) = args.next() { |
| 78 | 81 | | if arg == "--shout" { |
| 79 | 82 | | options.shout = true; |
| 83 | + | options.whisper = false; |
| 84 | + | } else if arg == "--whisper" { |
| 85 | + | options.whisper = true; |
| 86 | + | options.shout = false; |
| 80 | 87 | | } else if arg == "--version" { |
| 81 | 88 | | options.version = true; |
| 82 | 89 | | } else if arg == "--quiet" { |
| ⋯ |
| 112 | 119 | | text.to_uppercase() |
| 113 | 120 | | } |
| 114 | 121 | | |
| 122 | + | /// `text` lower-cased, with its final exclamation mark replaced by `...`. |
| 123 | + | pub fn whisper(text: &str) -> String { |
| 124 | + | let body = text.to_lowercase(); |
| 125 | + | format!("{}...", body.trim_end_matches('!').trim_end()) |
| 126 | + | } |
| 127 | + | |
| 115 | 128 | | /// `text` with its final exclamation mark replaced by three. |
| 116 | 129 | | pub fn loud(text: &str) -> String { |
| 117 | 130 | | format!("{}!!!", text.strip_suffix('!').unwrap_or(text)) |
| ⋯ |
| 320 | 333 | | } |
| 321 | 334 | | |
| 322 | 335 | | #[test] |
| 336 | + | fn whisper_lower_cases_and_trails_off() { |
| 337 | + | assert_eq!(whisper(&hail("Ada")), "hi, ada..."); |
| 338 | + | assert_eq!(whisper(&greeting_in("Luc", Lang::Fr)), "bonjour, luc..."); |
| 339 | + | } |
| 340 | + | |
| 341 | + | #[test] |
| 342 | + | fn parse_args_whisper_and_shout_last_wins() { |
| 343 | + | let o = parse_args(args(&["--whisper", "Ada"])).unwrap(); |
| 344 | + | assert!(o.whisper && !o.shout); |
| 345 | + | let o = parse_args(args(&["--shout", "--whisper"])).unwrap(); |
| 346 | + | assert!(o.whisper && !o.shout); |
| 347 | + | let o = parse_args(args(&["--whisper", "--shout"])).unwrap(); |
| 348 | + | assert!(o.shout && !o.whisper); |
| 349 | + | } |
| 350 | + | |
| 351 | + | #[test] |
| 323 | 352 | | fn greets_by_name() { |
| 324 | 353 | | assert_eq!(hail("Ada"), "Hi, Ada!"); |
| 325 | 354 | | } |