use crate::{size, status_board::GLOBAL_STATUS_BOARD};
use derive_setters::Setters;
use std::fmt::Write;
#[derive(Debug, Default, Setters, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[setters(prefix = "with_")]
pub struct ProgressReport<Size: size::Size> {
pub items: u64,
pub total: Size,
pub errors: u64,
pub linked: u64,
pub shared: Size,
}
impl<Size: size::Size + Into<u64>> ProgressReport<Size> {
const TEXT_MAX_LEN: usize = 145;
fn text(self) -> String {
let ProgressReport {
items,
total,
errors,
linked,
shared,
} = self;
let mut text = String::with_capacity(Self::TEXT_MAX_LEN);
let total: u64 = total.into();
write!(text, "\r(scanned {items}, total {total}").unwrap();
if linked != 0 {
write!(text, ", linked {linked}").unwrap();
}
let shared: u64 = shared.into();
if shared != 0 {
write!(text, ", shared {shared}").unwrap();
}
if errors != 0 {
write!(text, ", erred {errors}").unwrap();
}
text.push(')');
text
}
pub const TEXT: fn(Self) = |report| {
GLOBAL_STATUS_BOARD.temporary_message(&report.text());
};
}
#[test]
fn text_max_len() {
use crate::size::Bytes;
let correct_value = ProgressReport::<Bytes> {
items: u64::MAX,
total: u64::MAX.into(),
errors: u64::MAX,
linked: u64::MAX,
shared: u64::MAX.into(),
}
.text()
.len();
assert_eq!(ProgressReport::<Bytes>::TEXT_MAX_LEN, correct_value);
}