Created
April 26, 2017 20:19
-
-
Save boxdot/775724401a64b8a9a3c94f01f31b29d2 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
// before | |
fn parse_args<Iter>(mut args: Iter) -> Result<Args, &'static str> | |
where Iter: Iterator<Item = String> | |
{ | |
let hostname = match args.next() { | |
Some(s) => s, | |
None => return Err("argument 'hostname' missing"), | |
}; | |
let port: u16 = match args.next() { | |
Some(s) => s.parse().map_err(|_| "cannot parse port")?, | |
None => return Err("argument 'port' missing"), | |
}; | |
let username = match args.next() { | |
Some(s) => s, | |
None => return Err("argument 'username' missing"), | |
}; | |
let priv_key_path = match args.next() { | |
Some(s) => s, | |
None => return Err("path to private key is missing"), | |
}; | |
Ok(Args { | |
hostname: hostname, | |
port: port, | |
username: username, | |
priv_key_path: PathBuf::from(priv_key_path), | |
}) | |
} | |
// after | |
fn parse_args<Iter>(mut args: Iter) -> Result<Args, &'static str> | |
where Iter: Iterator<Item = String> | |
{ | |
let hostname = args.next().ok_or("argument 'hostname' missing")?; | |
let port = args.next().ok_or("argument 'port' missing")?; | |
let port: u16 = port.parse().map_err(|_| "cannot parse port")?; | |
let username = args.next().ok_or("argument 'username' missing")?; | |
let priv_key_path = args.next().ok_or("path to private key is missing")?; | |
Ok(Args { | |
hostname: hostname, | |
port: port, | |
username: username, | |
priv_key_path: PathBuf::from(priv_key_path), | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment