-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#98] changed all formatting functions so they accept a type that implements Display #100
Changes from all commits
2fb00a5
247f69d
ec1fe13
71fcac3
ae5f358
1d1d939
001c0d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,6 @@ Supported tools: | |
{tools}"# | ||
); | ||
|
||
err::abort(&exit_message); | ||
err::abort(exit_message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,14 @@ impl PrefetchProgress { | |
} | ||
} | ||
|
||
fn expected_err_msg(&self, tool_name: &str, msg: &str) { | ||
/// This method can take in any type that implements the [`Display`] trait | ||
fn expected_err_msg<Message: Display>(&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) { | ||
/// This method can take in any type that implements the [`Display`] trait | ||
fn unexpected_err_msg<Message: Display>(&self, tool_name: &str, msg: &Message) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like how only type-signatures changed but not the implementation of functions and the call sites became simpler as well! 🚀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is a perfect application of polymorphism, it does complicate the code a bit for people that aren't used to generic programming, but this cleans up the code a lot in my opinion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I like the simplification in this particular case 🙂 In the end, the generic function is written only once and you need to understand it once. But it's used many times and it can save a bit of boilerplate each time. |
||
let tool = format!("{}", style(tool_name).cyan().bold()); | ||
let err_msg = format!( | ||
r#"{emoji} {tool} {msg} | ||
|
@@ -106,7 +110,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 | ||
} | ||
|
@@ -125,7 +129,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 | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perfect 👨🏻🍳 🤌