Last active
November 7, 2016 08:45
-
-
Save glaznaj/2595eebf096bbe6f774f21a166ff8fad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate regex; | |
| use regex::Regex; | |
| #[derive(Debug)] | |
| enum Errors { | |
| BadFormat, | |
| } | |
| use self::Errors::*; | |
| impl From<std::num::ParseIntError> for Errors { | |
| fn from(_err: std::num::ParseIntError) -> Errors { | |
| BadFormat | |
| } | |
| } | |
| fn main() { | |
| let args = parse_args("100 200 300 400").unwrap(); | |
| let (priority, delay, ttr, bytes) = args; | |
| println!("priority: {}, delay: {}, ttr: {}, bytes: {}", priority, delay, ttr, bytes); | |
| } | |
| fn parse_args(args: &str) -> Result<(u32, u64, u64, u16), Errors> { | |
| let re: Regex = Regex::new(r"^(\d+)\s(\d+)\s(\d+)\s(\d+)$").unwrap(); | |
| let cap = match re.captures(args) { | |
| Some(cap) => cap, | |
| _ => return Err(BadFormat), | |
| }; | |
| let priority = match cap.at(1) { | |
| Some(priority) => try!(priority.parse()), | |
| _ => return Err(BadFormat), | |
| }; | |
| let delay = match cap.at(2) { | |
| Some(delay) => try!(delay.parse()), | |
| _ => return Err(BadFormat), | |
| }; | |
| let ttr = match cap.at(3) { | |
| Some(ttr) => try!(ttr.parse()), | |
| _ => return Err(BadFormat), | |
| }; | |
| let bytes = match cap.at(4) { | |
| Some(bytes) => try!(bytes.parse()), | |
| _ => return Err(BadFormat), | |
| }; | |
| Ok((priority, delay, ttr, bytes)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment