use colored::Colorize;
use crate::replacements::Replacement;
macro_rules! make_noisy_print {
($func_name: ident,
$element_name: ident,
$warn_string_singular: expr,
$warn_string_plural: expr,
$for_pattern: pat,
$for_body: block) => {
fn $func_name(&self)
{
if self.$element_name.is_empty()
{
return;
}
let warn_string = if self.$element_name.len() == 1
{
$warn_string_singular
}
else
{
$warn_string_plural
};
println!("{}\n", warn_string.bold().red().underline().on_green());
for $for_pattern in &self.$element_name
{
$for_body
}
println!();
}
};
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Noisy
{
pub(crate) repls: Vec<Replacement>,
pub(crate) missing_labels: Vec<String>,
pub(crate) duplicated_labels: Vec<String>,
pub(crate) disordered_labels: Vec<(String, String)>,
pub(crate) malformatted_number_in_label: Vec<(String, String)>,
pub(crate) inconsistent_length_of_labels: Vec<(String, String)>,
}
impl Noisy
{
#[must_use]
pub(crate) const fn from_replacements(repls: Vec<Replacement>) -> Self
{
Self {
repls,
missing_labels: vec![],
duplicated_labels: vec![],
disordered_labels: vec![],
malformatted_number_in_label: vec![],
inconsistent_length_of_labels: vec![],
}
}
pub(crate) fn append_labels(
&mut self,
missing_labels: &mut Vec<String>,
duplicated_labels: &mut Vec<String>,
disordered_labels: &mut Vec<(String, String)>,
malformatted_number_in_label: &mut Vec<(String, String)>,
inconsistent_length_of_labels: &mut Vec<(String, String)>,
)
{
self.missing_labels.append(missing_labels);
self.duplicated_labels.append(duplicated_labels);
self.disordered_labels.append(disordered_labels);
self.malformatted_number_in_label.append(malformatted_number_in_label);
self.inconsistent_length_of_labels.append(inconsistent_length_of_labels);
}
const fn is_empty(&self) -> bool
{
self.repls.is_empty()
&& self.missing_labels.is_empty()
&& self.duplicated_labels.is_empty()
&& self.disordered_labels.is_empty()
&& self.malformatted_number_in_label.is_empty()
&& self.inconsistent_length_of_labels.is_empty()
}
make_noisy_print!(
print_repls,
repls,
"The following noisy replacement rule was triggered:",
"The following noisy replacement rules were triggered:",
rule,
{
println!("* Replacement of {:?} to {:?}", rule.from, rule.to);
}
);
make_noisy_print!(
print_missing_labels,
missing_labels,
"The following label is missing:",
"The following labels are missing:",
label,
{
println!("* {label:?}");
}
);
make_noisy_print!(
print_duplicated_labels,
duplicated_labels,
"The following label is duplicated:",
"The following labels are duplicated:",
label,
{
println!("* {label:?}");
}
);
make_noisy_print!(
print_disordered_labels,
disordered_labels,
"The following labels are out of order:",
"The following labels are out of order:",
(label_a, label_b),
{
println!("* Label {label_a:?} is before label {label_b:?}");
}
);
make_noisy_print!(
print_malformatted_number_in_labels,
malformatted_number_in_label,
"The following label has the following bad number in it:",
"The following labels have the following bad numbers in them:",
(label, num),
{
println!("* Label {label:?} wrongly contains {num:?}");
}
);
make_noisy_print!(
print_inconsistent_lengths_of_labels,
inconsistent_length_of_labels,
"The following label pair consists out of different number of pieces:",
"The following label pairs consist out of different number of pieces:",
(label_a, label_b),
{
println!("* {label_a:?} and {label_b:?}");
}
);
pub fn print(&self)
{
if self.is_empty()
{
return;
}
println!("{}\n\n", "\n\n\n\n\n\n\n\n".blink().bold().cyan().on_bright_red());
self.print_repls();
self.print_missing_labels();
self.print_duplicated_labels();
self.print_disordered_labels();
self.print_malformatted_number_in_labels();
self.print_inconsistent_lengths_of_labels();
println!("{}", "\n\n\n\n\n\n\n\n".blink().bold().cyan().on_bright_red());
}
}