From 4d9c71e541b38c00e8a241dd263fa76b6526f99a Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 19 Mar 2014 17:56:40 +1100 Subject: [PATCH] du: be dryer when extracting the --max-depth arg. There's no need to repeat the two checks for `from_str` when we can just do it preemptively. --- du/du.rs | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/du/du.rs b/du/du.rs index 8c9e71e5e..c0a9d46b8 100644 --- a/du/du.rs +++ b/du/du.rs @@ -176,38 +176,30 @@ ers of 1000)."); return } - let options = Options{ + let summarize = matches.opt_present("summarize"); + + let max_depth_str = matches.opt_str("max-depth"); + let max_depth = max_depth_str.as_ref().and_then(|s| from_str::(*s)); + match (max_depth_str, max_depth) { + (Some(ref s), _) if summarize => { + println!("{}: warning: summarizing conflicts with --max-depth={:s}", program, *s); + return + } + (Some(ref s), None) => { + println!("{}: invalid maximum depth '{:s}'", program, *s); + return + } + (Some(_), Some(_)) | (None, _) => { /* valid */ } + } + + let options = Options { all: matches.opt_present("all"), - max_depth: match (matches.opt_present("summarize"), matches.opt_str("max-depth")) { - (true, Some(s)) => match from_str::(s) { - Some(_) => { - println!("du: warning: summarizing conflicts with --max-depth={:s}", s); - return - }, - None => { - println!("du: invalid maximum depth '{:s}'", s); - return - } - }, - (true, None) => Some(0), - (false, Some(s)) => match from_str::(s) { - Some(u) => Some(u), - None => { - println!("du: invalid maximum depth '{:s}'", s); - return - } - }, - (false, None) => None - }, + max_depth: max_depth, total: matches.opt_present("total"), separate_dirs: matches.opt_present("S"), }; - let strs = matches.free.clone(); - let strs = match strs.is_empty() { - true => ~[~"./"], - false => strs - }; + let strs = if matches.free.is_empty() {~[~"./"]} else {matches.free.clone()}; let options_arc = Arc::new(options);