mirror of
https://github.com/theryangeary/choose
synced 2024-11-26 21:00:17 +00:00
Handle errors in parse_range
This commit is contained in:
parent
46d79d7250
commit
1ac2906146
1 changed files with 25 additions and 2 deletions
27
src/main.rs
27
src/main.rs
|
@ -55,6 +55,29 @@ fn main() {
|
|||
|
||||
fn parse_range(src: &str) -> Result<Range, ParseIntError> {
|
||||
let re = Regex::new(r"^(\d*):(\d*)$").unwrap();
|
||||
let cap = re.captures_iter(src).next().unwrap();
|
||||
return Ok((Some(cap[1].parse()?), Some(cap[2].parse()?)));
|
||||
|
||||
let cap = match re.captures_iter(src).next() {
|
||||
Some(v) => v,
|
||||
None => panic!("failed to parse range argument: {}", src),
|
||||
};
|
||||
|
||||
let start = if cap[1].is_empty() {
|
||||
None
|
||||
} else {
|
||||
match cap[1].parse() {
|
||||
Ok(x) => Some(x),
|
||||
Err(e) => panic!("failed to get range argument: {:?}", e),
|
||||
}
|
||||
};
|
||||
|
||||
let end = if cap[2].is_empty() {
|
||||
None
|
||||
} else {
|
||||
match cap[2].parse() {
|
||||
Ok(x) => Some(x),
|
||||
Err(e) => panic!("failed to get range argument: {:?}", e),
|
||||
}
|
||||
};
|
||||
|
||||
return Ok((start, end));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue