diff options
Diffstat (limited to 'src/tftpc.rs')
| -rw-r--r-- | src/tftpc.rs | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/src/tftpc.rs b/src/tftpc.rs index 240ef2c..a134293 100644 --- a/src/tftpc.rs +++ b/src/tftpc.rs @@ -131,8 +131,8 @@ impl Tftpc { let mut file = File::open(self.conf.filename.as_path())?; let err_invalidpath = || io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename"); - let filename = self.conf.filename.file_name().ok_or(err_invalidpath())? - .to_str().ok_or(err_invalidpath())?; + let filename = self.conf.filename.file_name().ok_or_else(err_invalidpath)? + .to_str().ok_or_else(err_invalidpath)?; let metadata = file.metadata().map_err(|_| err_invalidpath())?; if !metadata.is_file() { return Err(err_invalidpath()); @@ -171,10 +171,10 @@ impl Tftpc { fn handle_rrq(&mut self, sock: &UdpSocket) -> Result<String, io::Error> { let err_invalidpath = || io::Error::new(io::ErrorKind::InvalidInput, "Invalid path/filename"); - let filename = self.conf.filename.file_name().ok_or(err_invalidpath())?; + let filename = self.conf.filename.file_name().ok_or_else(err_invalidpath)?; let outpath = env::current_dir().expect("Can't get current directory").join(filename); let mut file = File::create(outpath)?; - let filename = self.conf.filename.to_str().ok_or(err_invalidpath())?; + let filename = self.conf.filename.to_str().ok_or_else(err_invalidpath)?; let buf = self.init_req(rtftp::Opcode::RRQ, filename, 0); @@ -226,7 +226,7 @@ impl Tftpc { } } -fn usage(opts: Options, program: String, error: Option<String>) { +fn usage(opts: &Options, program: &str, error: Option<String>) { if let Some(err) = error { println!("{}\n", err); } @@ -234,7 +234,7 @@ fn usage(opts: Options, program: String, error: Option<String>) { println!("{}", opts.usage(format!("RusTFTP {}\n\n{} [options] <remote>[:port]", version, program).as_str())); } -fn parse_commandline(args: &[String]) -> Result<Configuration, &str> { +fn parse_commandline(args: &[String]) -> Option<Configuration> { let program = args[0].clone(); let mut operation = None; let mut mode = rtftp::Mode::OCTET; @@ -247,16 +247,14 @@ fn parse_commandline(args: &[String]) -> Result<Configuration, &str> { opts.optopt("p", "put", "upload file to remote server", "FILE"); opts.optopt("b", "blksize", format!("negotiate a different block size (default: {})", blksize).as_ref(), "SIZE"); opts.optflag("n", "netascii","use netascii mode (instead of octet)"); - let matches = match opts.parse(&args[1..]) { - Ok(m) => m, - Err(err) => { - usage(opts, program, Some(err.to_string())); - return Err("Parsing error"); - } - }; + + let getopts_fail = |err: getopts::Fail| { usage(&opts, &program, Some(err.to_string())) }; + let conv_error = |err: std::num::ParseIntError| { usage(&opts, &program, Some(err.to_string())) }; + + let matches = opts.parse(&args[1..]).map_err(getopts_fail).ok()?; if matches.opt_present("h") || matches.free.len() != 1 { - usage(opts, program, None); - return Err("usage"); + usage(&opts, &program, None); + return None; } if let Some(f) = matches.opt_str("g") { @@ -269,8 +267,8 @@ fn parse_commandline(args: &[String]) -> Result<Configuration, &str> { } if operation.is_none() || (matches.opt_present("g") && matches.opt_present("p")) { - usage(opts, program, Some("Exactly one of g (get) and p (put) required".to_string())); - return Err("get put"); + usage(&opts, &program, Some("Exactly one of g (get) and p (put) required".to_string())); + return None; } if matches.opt_present("n") { @@ -279,25 +277,19 @@ fn parse_commandline(args: &[String]) -> Result<Configuration, &str> { let remote_in = matches.free[0].as_str(); let remote = match remote_in.to_socket_addrs() { - Ok(mut i) => i.next(), + Ok(i) => i, Err(_) => match (remote_in, 69).to_socket_addrs() { - Ok(mut j) => j.next(), + Ok(j) => j, Err(_) => { - usage(opts, program, Some("Failed to parse and lookup specified remote".to_string())); - return Err("lookup"); + usage(&opts, &program, Some("Failed to parse and lookup specified remote".to_string())); + return None; } }, - }; + }.next(); - blksize = match matches.opt_get_default::<usize>("b", blksize) { - Ok(b) => b, - Err(err) => { - usage(opts, program, Some(err.to_string())); - return Err("blksize"); - } - }; + blksize = matches.opt_get_default::<usize>("b", blksize).map_err(conv_error).ok()?; - Ok(Configuration { + Some(Configuration { operation: operation.unwrap(), mode, filename: filename.unwrap(), @@ -309,8 +301,8 @@ fn parse_commandline(args: &[String]) -> Result<Configuration, &str> { fn main() { let args: Vec<String> = env::args().collect(); let conf = match parse_commandline(&args) { - Ok(c) => c, - Err(_) => return, + Some(c) => c, + None => return, }; Tftpc::new(conf).start(); |
