refactor short_summary
function in logs.rs to be less redundant
This commit is contained in:
parent
1cbbc7527f
commit
3a8160412c
1 changed files with 75 additions and 81 deletions
156
src/logs.rs
156
src/logs.rs
|
@ -55,76 +55,55 @@ pub fn display_summary(
|
||||||
|
|
||||||
/// Returns a string summary of the total number of failed and successful article downloads
|
/// Returns a string summary of the total number of failed and successful article downloads
|
||||||
fn short_summary(download_count: DownloadCount) -> String {
|
fn short_summary(download_count: DownloadCount) -> String {
|
||||||
// TODO: Refactor this
|
|
||||||
if download_count.total
|
if download_count.total
|
||||||
!= download_count.successful + download_count.failed + download_count.partial
|
!= download_count.successful + download_count.failed + download_count.partial
|
||||||
{
|
{
|
||||||
panic!("initial_count must be equal to the sum of failed and successful count")
|
panic!("initial_count must be equal to the sum of failed and successful count")
|
||||||
}
|
}
|
||||||
let get_noun = |count: usize| if count == 1 { "article" } else { "articles" };
|
let get_noun = |count: usize| if count == 1 { "article" } else { "articles" };
|
||||||
if download_count.successful == download_count.total && download_count.successful == 1 {
|
let get_summary = |count, label, color: Color| {
|
||||||
"Article downloaded successfully".green().to_string()
|
if count == 0 {
|
||||||
} else if download_count.total == download_count.failed && download_count.failed == 1 {
|
return "".to_string();
|
||||||
"Article failed to download".red().to_string()
|
};
|
||||||
} else if download_count.total == download_count.partial && download_count.partial == 1 {
|
|
||||||
"Article partially failed to download".yellow().to_string()
|
{
|
||||||
} else if download_count.successful == download_count.total {
|
if count == 1 && count == download_count.total {
|
||||||
"All articles downloaded successfully".green().to_string()
|
"Article".to_string() + label
|
||||||
} else if download_count.failed == download_count.total {
|
} else if count == download_count.total {
|
||||||
"All articles failed to download".red().to_string()
|
"All ".to_string() + get_noun(count) + label
|
||||||
} else if download_count.partial == download_count.total {
|
} else {
|
||||||
"All articles partially failed to download"
|
count.to_string() + " " + get_noun(count) + label
|
||||||
.yellow()
|
}
|
||||||
.to_string()
|
}
|
||||||
} else if download_count.partial == 0 {
|
.color(color)
|
||||||
format!(
|
|
||||||
"{} {} downloaded successfully, {} {} failed",
|
|
||||||
download_count.successful,
|
|
||||||
get_noun(download_count.successful),
|
|
||||||
download_count.failed,
|
|
||||||
get_noun(download_count.failed)
|
|
||||||
)
|
|
||||||
.yellow()
|
|
||||||
.to_string()
|
|
||||||
} else if download_count.successful == 0
|
|
||||||
&& download_count.partial > 0
|
|
||||||
&& download_count.failed > 0
|
|
||||||
{
|
|
||||||
format!(
|
|
||||||
"{} {} partially failed to download, {} {} failed",
|
|
||||||
download_count.partial,
|
|
||||||
get_noun(download_count.partial),
|
|
||||||
download_count.failed,
|
|
||||||
get_noun(download_count.failed)
|
|
||||||
)
|
|
||||||
.yellow()
|
|
||||||
.to_string()
|
|
||||||
} else if download_count.failed == 0
|
|
||||||
&& download_count.successful > 0
|
|
||||||
&& download_count.partial > 0
|
|
||||||
{
|
|
||||||
format!(
|
|
||||||
"{} {} downloaded successfully, {} {} partially failed to download",
|
|
||||||
download_count.successful,
|
|
||||||
get_noun(download_count.successful),
|
|
||||||
download_count.partial,
|
|
||||||
get_noun(download_count.partial)
|
|
||||||
)
|
|
||||||
.yellow()
|
|
||||||
.to_string()
|
.to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut summary = get_summary(
|
||||||
|
download_count.successful,
|
||||||
|
" downloaded successfully",
|
||||||
|
Color::BrightGreen,
|
||||||
|
);
|
||||||
|
|
||||||
|
let partial_summary = get_summary(
|
||||||
|
download_count.partial,
|
||||||
|
" partially failed to download",
|
||||||
|
Color::Yellow,
|
||||||
|
);
|
||||||
|
|
||||||
|
if !summary.is_empty() && !partial_summary.is_empty() {
|
||||||
|
summary = summary + ", " + &partial_summary;
|
||||||
} else {
|
} else {
|
||||||
format!(
|
summary = summary + &partial_summary;
|
||||||
"{} {} downloaded successfully, {} {} partially failed to download, {} {} failed",
|
|
||||||
download_count.successful,
|
|
||||||
get_noun(download_count.successful),
|
|
||||||
download_count.partial,
|
|
||||||
get_noun(download_count.partial),
|
|
||||||
download_count.failed,
|
|
||||||
get_noun(download_count.failed)
|
|
||||||
)
|
|
||||||
.yellow()
|
|
||||||
.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let failed_summary = get_summary(download_count.failed, " failed to download", Color::Red);
|
||||||
|
if !summary.is_empty() && !failed_summary.is_empty() {
|
||||||
|
summary = summary + ", " + &failed_summary;
|
||||||
|
} else {
|
||||||
|
summary = summary + &failed_summary;
|
||||||
|
}
|
||||||
|
summary
|
||||||
}
|
}
|
||||||
|
|
||||||
struct DownloadCount {
|
struct DownloadCount {
|
||||||
|
@ -192,7 +171,7 @@ mod tests {
|
||||||
fn test_short_summary() {
|
fn test_short_summary() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(1, 1, 0, 0)),
|
short_summary(DownloadCount::new(1, 1, 0, 0)),
|
||||||
"Article downloaded successfully".green().to_string()
|
"Article downloaded successfully".bright_green().to_string()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(1, 0, 0, 1)),
|
short_summary(DownloadCount::new(1, 0, 0, 1)),
|
||||||
|
@ -200,7 +179,9 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(10, 10, 0, 0)),
|
short_summary(DownloadCount::new(10, 10, 0, 0)),
|
||||||
"All articles downloaded successfully".green().to_string()
|
"All articles downloaded successfully"
|
||||||
|
.bright_green()
|
||||||
|
.to_string()
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(10, 0, 0, 10)),
|
short_summary(DownloadCount::new(10, 0, 0, 10)),
|
||||||
|
@ -208,39 +189,52 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(10, 8, 0, 2)),
|
short_summary(DownloadCount::new(10, 8, 0, 2)),
|
||||||
"8 articles downloaded successfully, 2 articles failed"
|
format!(
|
||||||
.yellow()
|
"{}, {}",
|
||||||
.to_string()
|
"8 articles downloaded successfully".bright_green(),
|
||||||
|
"2 articles failed to download".red()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(10, 1, 0, 9)),
|
short_summary(DownloadCount::new(10, 1, 0, 9)),
|
||||||
"1 article downloaded successfully, 9 articles failed"
|
format!(
|
||||||
.yellow()
|
"{}, {}",
|
||||||
.to_string()
|
"1 article downloaded successfully".bright_green(),
|
||||||
|
"9 articles failed to download".red()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(7, 6, 0, 1)),
|
short_summary(DownloadCount::new(7, 6, 0, 1)),
|
||||||
"6 articles downloaded successfully, 1 article failed"
|
format!(
|
||||||
.yellow()
|
"{}, {}",
|
||||||
.to_string()
|
"6 articles downloaded successfully".bright_green(),
|
||||||
|
"1 article failed to download".red()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(7, 4, 2, 1)),
|
short_summary(DownloadCount::new(7, 4, 2, 1)),
|
||||||
"4 articles downloaded successfully, 2 articles partially failed to download, 1 article failed"
|
format!(
|
||||||
.yellow()
|
"{}, {}, {}",
|
||||||
.to_string()
|
"4 articles downloaded successfully".bright_green(),
|
||||||
|
"2 articles partially failed to download".yellow(),
|
||||||
|
"1 article failed to download".red()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(12, 6, 6, 0)),
|
short_summary(DownloadCount::new(12, 6, 6, 0)),
|
||||||
"6 articles downloaded successfully, 6 articles partially failed to download"
|
format!(
|
||||||
.yellow()
|
"{}, {}",
|
||||||
.to_string()
|
"6 articles downloaded successfully".bright_green(),
|
||||||
|
"6 articles partially failed to download".yellow()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(5, 0, 4, 1)),
|
short_summary(DownloadCount::new(5, 0, 4, 1)),
|
||||||
"4 articles partially failed to download, 1 article failed"
|
format!(
|
||||||
.yellow()
|
"{}, {}",
|
||||||
.to_string()
|
"4 articles partially failed to download".yellow(),
|
||||||
|
"1 article failed to download".red()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
short_summary(DownloadCount::new(4, 0, 4, 0)),
|
short_summary(DownloadCount::new(4, 0, 4, 0)),
|
||||||
|
|
Reference in a new issue