|
1 | 1 | use std::process; |
2 | 2 |
|
3 | | -use clap::{load_yaml, App}; |
| 3 | +use clap::{load_yaml, App, AppSettings}; |
| 4 | + |
| 5 | +#[derive(Clone,Debug)] |
| 6 | +pub struct Seq { |
| 7 | + current: f64, |
| 8 | + stop: f64, |
| 9 | + step: f64, |
| 10 | +} |
| 11 | + |
| 12 | +impl Iterator for Seq { |
| 13 | + type Item = f64; |
| 14 | + |
| 15 | + fn next(&mut self) -> Option<Self::Item> { |
| 16 | + if self.step.is_sign_positive() && self.current <= self.stop { |
| 17 | + let result = self.current.clone(); |
| 18 | + |
| 19 | + self.current += self.step; |
| 20 | + |
| 21 | + Some(result) |
| 22 | + } else if self.step.is_sign_negative() && self.current >= self.stop |
| 23 | +{ |
| 24 | + let result = self.current.clone(); |
| 25 | + |
| 26 | + self.current += self.step; |
| 27 | + |
| 28 | + Some(result) |
| 29 | + } else { |
| 30 | + None |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +fn parse_number(text: &str) -> Result<f64, ()> { |
| 37 | + text |
| 38 | + .parse::<f64>() |
| 39 | + .map_err(|_| ()) |
| 40 | +} |
| 41 | + |
| 42 | +fn check_nan(value: f64) -> Result<f64, ()> { |
| 43 | + if value.is_nan() { Err(()) } else { Ok(value)} |
| 44 | +} |
| 45 | + |
| 46 | +fn check_zero(value: f64) -> Result<f64, ()> { |
| 47 | + if value == 0.0 { Err(()) } else { Ok(value)} |
| 48 | +} |
4 | 49 |
|
5 | 50 | fn main() { |
6 | 51 | let yaml = load_yaml!("seq.yml"); |
7 | | - let matches = App::from_yaml(yaml).get_matches(); |
| 52 | + let matches = App::from_yaml(yaml) |
| 53 | + .setting(AppSettings::AllowNegativeNumbers) |
| 54 | + .get_matches(); |
| 55 | + |
| 56 | + let (raw_first, raw_increment, raw_last) = match (matches.value_of("FIRST"), matches.value_of("SECOND"), matches.value_of("THIRD")) { |
| 57 | + (Some(last), None, None) => |
| 58 | + ("1", "1", last), |
| 59 | + (Some(first), Some(last), None) => |
| 60 | + (first, "1", last), |
| 61 | + (Some(first), Some(increment), Some(last)) => |
| 62 | + (first, increment, last), |
| 63 | + _ => { |
| 64 | + eprintln!("seq: Missing operands.\nTry 'seq --help' for more information."); |
| 65 | + process::exit(1); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + let first = parse_number(raw_first) |
| 70 | + .and_then(check_nan) |
| 71 | + .unwrap_or_else(|_| { |
| 72 | + eprintln!("seq: Invalid FIRST.\nTry 'sleep --help' for more information."); |
| 73 | + process::exit(1); |
| 74 | + }); |
| 75 | + |
| 76 | + let last = parse_number(raw_last) |
| 77 | + .and_then(check_nan) |
| 78 | + .unwrap_or_else(|_| { |
| 79 | + eprintln!("seq: Invalid LAST.\nTry 'sleep --help' for more information."); |
| 80 | + process::exit(1); |
| 81 | + }); |
| 82 | + |
| 83 | + let increment = parse_number(raw_increment) |
| 84 | + .and_then(check_nan) |
| 85 | + .and_then(check_zero) |
| 86 | + .unwrap_or_else(|_| { |
| 87 | + eprintln!("seq: Invalid INCREMENT.\nTry 'sleep --help' for more information."); |
| 88 | + process::exit(1); |
| 89 | + }); |
8 | 90 |
|
| 91 | + let iter = Seq { current: first, stop: last, step: increment}; |
9 | 92 |
|
10 | | - std::dbg!(matches); |
| 93 | + for index in iter { |
| 94 | + println!("{}", index); |
| 95 | + } |
11 | 96 |
|
12 | 97 | // let numbers = match matches.values_of("NUMBER") { |
13 | 98 | // Some(values) => values, |
|
0 commit comments