From 2fb00a57d828045457a4cab59ed471f0e7c4e33a Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 11:16:03 +0200 Subject: [PATCH 1/7] changed all formatting functions so they accept a type that implements Display --- src/config/toml.rs | 2 +- src/infra/err.rs | 7 ++++--- src/sync/prefetch.rs | 6 ++++-- src/sync/progress.rs | 10 +++++++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/config/toml.rs b/src/config/toml.rs index e4b36e8..a20b618 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -34,7 +34,7 @@ pub fn with_parsed_file(config_path: PathBuf, on_success: F) err::abort_with(&format!( "Error parsing configuration at path {}: {}", config_path.display(), - e.to_string() + e )); } } diff --git a/src/infra/err.rs b/src/infra/err.rs index a4ccd01..85d1f1d 100644 --- a/src/infra/err.rs +++ b/src/infra/err.rs @@ -1,7 +1,8 @@ +use std::fmt::Display; use std::process; /// Print an error message and exit with code 1 -pub fn abort_with(err_msg: &str) -> ! { +pub fn abort_with(err_msg: &Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: @@ -13,7 +14,7 @@ pub fn abort_with(err_msg: &str) -> ! { } /// Print an error message, suggesting opening an issue and exit with code 1 -pub fn abort_suggest_issue(err_msg: &str) -> ! { +pub fn abort_suggest_issue(err_msg: &Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: @@ -31,7 +32,7 @@ this issue: } /// Print just the message and exit -pub fn abort(err_msg: &str) -> ! { +pub fn abort(err_msg: &Message) -> ! { eprintln!("{}", err_msg); process::exit(1); } diff --git a/src/sync/prefetch.rs b/src/sync/prefetch.rs index dd96183..868e635 100644 --- a/src/sync/prefetch.rs +++ b/src/sync/prefetch.rs @@ -1,6 +1,8 @@ use console::{style, Emoji}; use indicatif::{HumanBytes, ProgressBar, ProgressStyle}; + use std::collections::BTreeMap; +use std::fmt::Display; use super::configure::configure_tool; use crate::config::schema::ConfigAsset; @@ -36,12 +38,12 @@ impl PrefetchProgress { } } - fn expected_err_msg(&self, tool_name: &str, msg: &str) { + fn expected_err_msg(&self, tool_name: &str, msg: &Message) { let tool = format!("{}", style(tool_name).cyan().bold()); self.pb.println(format!("{} {} {}", ERROR, tool, msg)) } - fn unexpected_err_msg(&self, tool_name: &str, msg: &str) { + fn unexpected_err_msg(&self, tool_name: &str, msg: &Message) { let tool = format!("{}", style(tool_name).cyan().bold()); let err_msg = format!( r#"{emoji} {tool} {msg} diff --git a/src/sync/progress.rs b/src/sync/progress.rs index dbdeb1d..dfe8f16 100644 --- a/src/sync/progress.rs +++ b/src/sync/progress.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use console::{style, Emoji}; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; @@ -84,7 +86,13 @@ impl SyncProgress { pb.finish(); } - pub fn failure(&self, pb: ProgressBar, tool_name: &str, tag: &str, err_msg: String) { + pub fn failure( + &self, + pb: ProgressBar, + tool_name: &str, + tag: &str, + err_msg: Message, + ) { pb.set_prefix(self.fmt_prefix(FAILURE, tool_name, tag)); let failure_msg = format!("{}", style(err_msg).red()); From 247f69d87633e778f047408f2ddb2e1a903c782b Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 11:46:09 +0200 Subject: [PATCH 2/7] removed .to_string calls --- src/config/schema.rs | 2 +- src/sync/prefetch.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config/schema.rs b/src/config/schema.rs index 2bb5b2b..97ea493 100644 --- a/src/config/schema.rs +++ b/src/config/schema.rs @@ -64,7 +64,7 @@ impl Config { let expanded_store_directory = shellexpand::full(&self.store_directory); let store_directory = match expanded_store_directory { - Err(e) => err::abort_with(&e.to_string()), + Err(e) => err::abort_with(&e), Ok(cow_path) => PathBuf::from(cow_path.into_owned()), }; diff --git a/src/sync/prefetch.rs b/src/sync/prefetch.rs index 868e635..5fe0a97 100644 --- a/src/sync/prefetch.rs +++ b/src/sync/prefetch.rs @@ -108,7 +108,7 @@ fn prefetch_tool( match configure_tool(tool_name, config_asset) { Tool::Error(e) => { - prefetch_progress.expected_err_msg(tool_name, &e.to_string()); + prefetch_progress.expected_err_msg(tool_name, &e); prefetch_progress.update_message(already_completed); None } @@ -127,7 +127,7 @@ fn prefetch_tool( } Ok(release) => match tool_info.select_asset(&release.assets) { Err(err) => { - prefetch_progress.unexpected_err_msg(tool_name, &err.to_string()); + prefetch_progress.unexpected_err_msg(tool_name, &err); prefetch_progress.update_message(already_completed); None } From ec1fe1383e82267e00db23b4d05d777682e27291 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 11:51:11 +0200 Subject: [PATCH 3/7] added a small description about the display trait in each function --- src/infra/err.rs | 3 +++ src/sync/prefetch.rs | 2 ++ src/sync/progress.rs | 1 + 3 files changed, 6 insertions(+) diff --git a/src/infra/err.rs b/src/infra/err.rs index 85d1f1d..d8029b4 100644 --- a/src/infra/err.rs +++ b/src/infra/err.rs @@ -2,6 +2,7 @@ use std::fmt::Display; use std::process; /// Print an error message and exit with code 1 +/// This function can take in any type that implements the [`Display`] trait pub fn abort_with(err_msg: &Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: @@ -14,6 +15,7 @@ pub fn abort_with(err_msg: &Message) -> ! { } /// Print an error message, suggesting opening an issue and exit with code 1 +/// This function can take in any type that implements the [`Display`] trait pub fn abort_suggest_issue(err_msg: &Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: @@ -32,6 +34,7 @@ this issue: } /// Print just the message and exit +/// This function can take in any type that implements the [`Display`] trait pub fn abort(err_msg: &Message) -> ! { eprintln!("{}", err_msg); process::exit(1); diff --git a/src/sync/prefetch.rs b/src/sync/prefetch.rs index 5fe0a97..1611338 100644 --- a/src/sync/prefetch.rs +++ b/src/sync/prefetch.rs @@ -38,11 +38,13 @@ impl PrefetchProgress { } } + /// This method can take in any type that implements the [`Display`] trait fn expected_err_msg(&self, tool_name: &str, msg: &Message) { let tool = format!("{}", style(tool_name).cyan().bold()); self.pb.println(format!("{} {} {}", ERROR, tool, msg)) } + /// This method can take in any type that implements the [`Display`] trait fn unexpected_err_msg(&self, tool_name: &str, msg: &Message) { let tool = format!("{}", style(tool_name).cyan().bold()); let err_msg = format!( diff --git a/src/sync/progress.rs b/src/sync/progress.rs index dfe8f16..49fabad 100644 --- a/src/sync/progress.rs +++ b/src/sync/progress.rs @@ -86,6 +86,7 @@ impl SyncProgress { pb.finish(); } + /// This method can take in any type that implements the [`Display`] trait pub fn failure( &self, pb: ProgressBar, From 71fcac3961d73bb770d1fa2d51cbcb6fef7c356d Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 12:11:25 +0200 Subject: [PATCH 4/7] changed abort_suggest_issue to not look at the Sized trait and instead Boxed the offending &str --- src/infra/err.rs | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/infra/err.rs b/src/infra/err.rs index d8029b4..685ce1f 100644 --- a/src/infra/err.rs +++ b/src/infra/err.rs @@ -16,7 +16,7 @@ pub fn abort_with(err_msg: &Message) -> ! { /// Print an error message, suggesting opening an issue and exit with code 1 /// This function can take in any type that implements the [`Display`] trait -pub fn abort_suggest_issue(err_msg: &Message) -> ! { +pub fn abort_suggest_issue(err_msg: &Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: diff --git a/src/lib.rs b/src/lib.rs index 4a1ee4f..e20bf04 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ fn resolve_config_path(config_path: Option) -> PathBuf { path } None => { - err::abort_suggest_issue("Unable to find $HOME directory"); + err::abort_suggest_issue(&Box::new("Unable to find $HOME directory")); } }, } From ae5f358177eb44e0cb3aab06ea3f43dc30e6d5ec Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 12:12:52 +0200 Subject: [PATCH 5/7] removed reference from type signature since the functions here always exit the program --- src/infra/err.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/infra/err.rs b/src/infra/err.rs index 685ce1f..caf00c2 100644 --- a/src/infra/err.rs +++ b/src/infra/err.rs @@ -3,7 +3,7 @@ use std::process; /// Print an error message and exit with code 1 /// This function can take in any type that implements the [`Display`] trait -pub fn abort_with(err_msg: &Message) -> ! { +pub fn abort_with(err_msg: Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: @@ -16,7 +16,7 @@ pub fn abort_with(err_msg: &Message) -> ! { /// Print an error message, suggesting opening an issue and exit with code 1 /// This function can take in any type that implements the [`Display`] trait -pub fn abort_suggest_issue(err_msg: &Message) -> ! { +pub fn abort_suggest_issue(err_msg: Message) -> ! { eprintln!( r#"Aborting 'tool-sync' with error: @@ -35,7 +35,7 @@ this issue: /// Print just the message and exit /// This function can take in any type that implements the [`Display`] trait -pub fn abort(err_msg: &Message) -> ! { +pub fn abort(err_msg: Message) -> ! { eprintln!("{}", err_msg); process::exit(1); } From 1d1d93991f5677699bf6b73acff57511f777b7f6 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 12:16:56 +0200 Subject: [PATCH 6/7] removed useless references --- src/config/schema.rs | 4 ++-- src/config/toml.rs | 2 +- src/install.rs | 2 +- src/lib.rs | 2 +- src/sync/install.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/config/schema.rs b/src/config/schema.rs index 97ea493..b0d0b88 100644 --- a/src/config/schema.rs +++ b/src/config/schema.rs @@ -64,14 +64,14 @@ impl Config { let expanded_store_directory = shellexpand::full(&self.store_directory); let store_directory = match expanded_store_directory { - Err(e) => err::abort_with(&e), + Err(e) => err::abort_with(e), Ok(cow_path) => PathBuf::from(cow_path.into_owned()), }; let has_store_directory = store_directory.as_path().is_dir(); if !has_store_directory { - err::abort_with(&format!( + err::abort_with(format!( "Specified directory for storing tools doesn't exist: {}", store_directory.display() )); diff --git a/src/config/toml.rs b/src/config/toml.rs index a20b618..807b6c2 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -31,7 +31,7 @@ pub fn with_parsed_file(config_path: PathBuf, on_success: F) on_success(config); } Err(e) => { - err::abort_with(&format!( + err::abort_with(format!( "Error parsing configuration at path {}: {}", config_path.display(), e diff --git a/src/install.rs b/src/install.rs index 98c89e7..a322325 100644 --- a/src/install.rs +++ b/src/install.rs @@ -27,6 +27,6 @@ Supported tools: {tools}"# ); - err::abort(&exit_message); + err::abort(exit_message); } } diff --git a/src/lib.rs b/src/lib.rs index e20bf04..0176b9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ fn resolve_config_path(config_path: Option) -> PathBuf { path } None => { - err::abort_suggest_issue(&Box::new("Unable to find $HOME directory")); + err::abort_suggest_issue(Box::new("Unable to find $HOME directory")); } }, } diff --git a/src/sync/install.rs b/src/sync/install.rs index 1458e07..7868e66 100644 --- a/src/sync/install.rs +++ b/src/sync/install.rs @@ -28,7 +28,7 @@ impl<'a> Installer<'a> { let tmp_dir = TempDir::new("tool-sync"); match tmp_dir { Err(e) => { - err::abort_suggest_issue(&format!("Error creating temporary directory: {}", e)); + err::abort_suggest_issue(format!("Error creating temporary directory: {}", e)); } Ok(tmp_dir) => Installer { store_directory, From 001c0d05f85af49eae4fd8b6c9467b2b4b04c253 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Mon, 19 Sep 2022 13:51:12 +0200 Subject: [PATCH 7/7] removed boxed form of str slice --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0176b9d..4a1ee4f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ fn resolve_config_path(config_path: Option) -> PathBuf { path } None => { - err::abort_suggest_issue(Box::new("Unable to find $HOME directory")); + err::abort_suggest_issue("Unable to find $HOME directory"); } }, }