From 9666ac727be52ce8aaa4612a1e55ad64baf3665b Mon Sep 17 00:00:00 2001 From: Daniel M Date: Fri, 2 Apr 2021 16:17:39 +0200 Subject: [PATCH] Minor refactoring --- src/main.rs | 66 ++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index d75af94..c7d54e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,46 +119,28 @@ async fn main() -> ResBE<()> { .get_matches(); - let outdir = match arguments.value_of("outdir") { - Some(it) => it, - None => "./" - }; + let outdir = arguments.value_of("outdir").unwrap_or("./").to_string(); - let into_file = match arguments.value_of("into_file") { - Some(it) => Some(it.to_string()), - None => None - }; + let into_file = arguments.value_of("into_file").map(String::from); - let file_count = match arguments.value_of("file_count") { - Some(it) => it, - None => "1" - }; + let file_count = arguments.value_of("file_count").unwrap_or("1"); - let file_count: u32 = match file_count.parse() { - Ok(it) => it, - Err(_) => { - eprintln!("Invalid value for num-files: {}", file_count); - exit(1); - } - }; + let file_count: u32 = file_count.parse().unwrap_or_else(|_| { + eprintln!("Invalid value for num-files: {}", file_count); + exit(1); + }); if file_count <= 0 { eprintln!("Invalid value for num-files: {}", file_count); exit(1); } - let conn_count = match arguments.value_of("conn_count") { - Some(it) => it, - None => "1" - }; + let conn_count = arguments.value_of("conn_count").unwrap_or("1"); - let conn_count: u32 = match conn_count.parse() { - Ok(it) => it, - Err(_) => { - eprintln!("Invalid value for connections: {}", conn_count); - exit(1); - } - }; + let conn_count: u32 = conn_count.parse().unwrap_or_else(|_| { + eprintln!("Invalid value for connections: {}", conn_count); + exit(1); + }); if conn_count <= 0 { eprintln!("Invalid value for connections: {}", conn_count); @@ -188,7 +170,7 @@ async fn main() -> ResBE<()> { let mut cli_args = CLIArguments { - outdir: outdir.to_string(), + outdir: outdir, into_file: into_file, file_count: file_count, conn_count: conn_count, @@ -225,15 +207,12 @@ async fn main() -> ResBE<()> { } CLIAction::ResolveZippyUrl(url) => { - match zippy::resolve_link(url).await { - Ok(resolved_url) => { - println!("{}", resolved_url); - }, - Err(_e) => { - println!("Zippyshare link could not be resolved"); - exit(1); - } - } + let resolved_url = zippy::resolve_link(url).await.unwrap_or_else(|_| { + println!("Zippyshare link could not be resolved"); + exit(1); + }); + + println!("{}", resolved_url); }, CLIAction::None => { @@ -302,7 +281,6 @@ async fn download_multiple(cli_args: CLIArguments) -> ResBE<()> { rep.send( DlStatus::Message(format!("Zippyshare link could not be resolved: {}", url)) ); - continue; } } @@ -310,11 +288,7 @@ async fn download_multiple(cli_args: CLIArguments) -> ResBE<()> { url.to_string() }; - let file_name = if let Some(arg_filename) = &arg_filename { - arg_filename.to_string() - } else { - download::url_to_filename(&url) - }; + let file_name = arg_filename.clone().unwrap_or_else(|| download::url_to_filename(&url)); let into_file = outdir.join(Path::new(&file_name))