From d2d35af2f582249030fc569854450ac12e3c08d4 Mon Sep 17 00:00:00 2001 From: apogeeoak <59737221+apogeeoak@users.noreply.github.com> Date: Thu, 15 Jul 2021 16:48:47 -0400 Subject: [PATCH 1/3] Handle unsuccessful exit status. --- src/main.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index f800ad1..3eec851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,28 +1,28 @@ -extern crate open; - -use std::{ - env, - io::{stderr, Write}, - process, -}; +use std::{env, process}; fn main() { let path_or_url = match env::args().nth(1) { Some(arg) => arg, None => { - writeln!(stderr(), "usage: open ").ok(); + eprintln!("usage: open "); process::exit(1); } }; - if let Err(err) = open::that(&path_or_url) { - writeln!( - stderr(), - "An error occourred when opening '{}': {}", - path_or_url, - err - ) - .ok(); - process::exit(3); + match open::that(&path_or_url) { + Ok(status) if status.success() => (), + Ok(status) => match status.code() { + Some(code) => open_error(code, &path_or_url, &format!("error code: {}", code)), + None => open_error(3, &path_or_url, "error unknown"), + }, + Err(err) => open_error(3, &path_or_url, &err.to_string()), } } + +fn open_error(code: i32, path: &str, error_message: &str) { + eprintln!( + "An error occurred when opening '{}': {}", + path, error_message + ); + process::exit(code); +} From 4f87a7888049b182ede9e00a057c2cc625152ef9 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 16 Jul 2021 15:09:55 +0800 Subject: [PATCH 2/3] clarify what the error handler does --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3eec851..63ab93e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,14 @@ fn main() { match open::that(&path_or_url) { Ok(status) if status.success() => (), Ok(status) => match status.code() { - Some(code) => open_error(code, &path_or_url, &format!("error code: {}", code)), - None => open_error(3, &path_or_url, "error unknown"), + Some(code) => print_error_and_exit(code, &path_or_url, &format!("error code: {}", code)), + None => print_error_and_exit(3, &path_or_url, "error unknown"), }, - Err(err) => open_error(3, &path_or_url, &err.to_string()), + Err(err) => print_error_and_exit(3, &path_or_url, &err.to_string()), } } -fn open_error(code: i32, path: &str, error_message: &str) { +fn print_error_and_exit(code: i32, path: &str, error_message: &str) -> ! { eprintln!( "An error occurred when opening '{}': {}", path, error_message From 215227a3385aa2624d32567eebb08af49e258b60 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 16 Jul 2021 15:11:43 +0800 Subject: [PATCH 3/3] cargo fmt --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 63ab93e..66ca5ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,9 @@ fn main() { match open::that(&path_or_url) { Ok(status) if status.success() => (), Ok(status) => match status.code() { - Some(code) => print_error_and_exit(code, &path_or_url, &format!("error code: {}", code)), + Some(code) => { + print_error_and_exit(code, &path_or_url, &format!("error code: {}", code)) + } None => print_error_and_exit(3, &path_or_url, "error unknown"), }, Err(err) => print_error_and_exit(3, &path_or_url, &err.to_string()),