Skip to content

Commit 4fc24b8

Browse files
author
Francis Murillo
committed
Working seq
1 parent 2ac4e17 commit 4fc24b8

4 files changed

Lines changed: 95 additions & 7 deletions

File tree

seq/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "seq"
33
version = "0.1.0"
4-
authors = ["GrayJack <gr41.j4ck@gmail.com>", "FrancisMurillo <francisavmurillo@gmail.com>"]
4+
authors = ["GrayJack <gr41.j4ck@gmail.com>" , "FrancisMurillo <francisavmurillo@gmail.com>"]
55
build = "build.rs"
66
edition = "2018"
77

seq/src/main.rs

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,98 @@
11
use std::process;
22

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+
}
449

550
fn main() {
651
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+
});
890

91+
let iter = Seq { current: first, stop: last, step: increment};
992

10-
std::dbg!(matches);
93+
for index in iter {
94+
println!("{}", index);
95+
}
1196

1297
// let numbers = match matches.values_of("NUMBER") {
1398
// Some(values) => values,

seq/src/seq.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ args:
2020
2121
2222
This must not be NaN. If omitted, defaults to 1.
23-
- INCREMENT:
23+
required: true
24+
- SECOND:
2425
help: >
2526
The step/increment added after each preceeding number in the
2627
sequence.
@@ -33,7 +34,8 @@ args:
3334
number; if less than, this should be a negative number. This
3435
must not be 0 or NaN. If omitted, defaults to 1 even if FIRST
3536
is greater than LAST.
36-
- LAST:
37+
required: false
38+
- THIRD:
3739
help: >
3840
The limit of the sequence.
3941
long_help: >
@@ -43,3 +45,4 @@ args:
4345
If the next number in a sequence exceeds above or falls below
4446
LAST whether STEP is positive or negative respectively, the
4547
sequence ends. This must not be NaN.
48+
required: false

sleep/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn main() {
1111
Some(values) => values,
1212
None => {
1313
eprintln!("sleep: Missing operand.\nTry 'sleep --help' for more information.");
14-
process::exit(1);
14+
process::exit(1);2
1515
}
1616
};
1717

0 commit comments

Comments
 (0)