Minify code with helper macro

This commit is contained in:
Matthias Beyer 2018-04-30 14:15:34 +02:00
parent 76966bcd6c
commit b713718693
1 changed files with 40 additions and 47 deletions

View File

@ -95,6 +95,20 @@ impl Diagnostic {
} }
} }
macro_rules! do_write {
($dest:ident, $pattern:tt) => {
let _ = writeln!($dest, $pattern)
.to_exit_code()
.unwrap_or_exit();
};
($dest:ident, $pattern:tt, $( $args:expr ),*) => {
let _ = writeln!($dest, $pattern, $( $args ),*)
.to_exit_code()
.unwrap_or_exit();
}
}
fn main() { fn main() {
let version = make_imag_version!(); let version = make_imag_version!();
let rt = generate_runtime_setup("imag-diagnostics", let rt = generate_runtime_setup("imag-diagnostics",
@ -158,64 +172,43 @@ fn main() {
let mut out = rt.stdout(); let mut out = rt.stdout();
let _ = writeln!(out, "imag version {}", env!("CARGO_PKG_VERSION")) do_write!(out, "imag version {}", { env!("CARGO_PKG_VERSION") });
.to_exit_code() do_write!(out, "");
.unwrap_or_exit(); do_write!(out, "{} entries", n);
let _ = writeln!(out, "")
.to_exit_code()
.unwrap_or_exit();
let _ = writeln!(out, "{} entries", n)
.to_exit_code()
.unwrap_or_exit();
for (k, v) in version_counts { for (k, v) in version_counts {
let _ = writeln!(out, "{} entries with store version '{}'", v, k) do_write!(out, "{} entries with store version '{}'", v, k);
.to_exit_code()
.unwrap_or_exit();
} }
if n != 0 { if n != 0 {
let _ = writeln!(out, "{} header sections in the average entry", sum_header_sections / n) do_write!(out, "{} header sections in the average entry", sum_header_sections / n);
.to_exit_code() do_write!(out, "{} average content bytecount", sum_bytecount_content / n);
.unwrap_or_exit(); do_write!(out, "{} average overall bytecount", sum_overall_byte_size / n);
let _ = writeln!(out, "{} average content bytecount", sum_bytecount_content / n)
.to_exit_code()
.unwrap_or_exit();
let _ = writeln!(out, "{} average overall bytecount", sum_overall_byte_size / n)
.to_exit_code()
.unwrap_or_exit();
if let Some((num, path)) = max_overall_byte_size { if let Some((num, path)) = max_overall_byte_size {
let _ = writeln!(out, do_write!(out, "Largest Entry ({} bytes): {}",
"Largest Entry ({bytes} bytes): {path}", num,
bytes = num, path
path = path .into_pathbuf()
.into_pathbuf() .map_err_trace_exit_unwrap(1)
.map_err_trace_exit_unwrap(1) .to_str()
.to_str() .unwrap_or("Failed converting path to string")
.unwrap_or("Failed converting path to string") );
)
.to_exit_code()
.unwrap_or_exit();
} }
let _ = writeln!(out, "{} average internal link count per entry", num_internal_links / n)
.to_exit_code() do_write!(out, "{} average internal link count per entry", num_internal_links / n);
.unwrap_or_exit();
if let Some((num, path)) = max_internal_links { if let Some((num, path)) = max_internal_links {
let _ = writeln!(out, "Entry with most internal links ({count}): {path}", do_write!(out, "Entry with most internal links ({}): {}",
count = num, num,
path = path path
.into_pathbuf() .into_pathbuf()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
.to_str() .to_str()
.unwrap_or("Failed converting path to string") .unwrap_or("Failed converting path to string")
) );
.to_exit_code()
.unwrap_or_exit();
} }
let _ = writeln!(out, "{} verified entries", verified_count) do_write!(out, "{} verified entries", verified_count);
.to_exit_code() do_write!(out, "{} unverified entries", unverified_count);
.unwrap_or_exit();
let _ = writeln!(out, "{} unverified entries", unverified_count)
.to_exit_code()
.unwrap_or_exit();
} }
} }