summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2018-04-30 21:49:24 +0200
committerGitHub <noreply@github.com>2018-04-30 21:49:24 +0200
commitd45eef299e147d503746adbddf130cd8a3a45a7b (patch)
treedaf6bbc18ed23ac81a836a6b6075b498ab1dadbf /bin
parent424a060cc84afee3875357e18e4746f789de4872 (diff)
parentb713718693ad616bf0cd32b0cf889105b7ca48a6 (diff)
Merge pull request #1476 from matthiasbeyer/imag-diagnostics/refactor
Minify code with helper macro
Diffstat (limited to 'bin')
-rw-r--r--bin/core/imag-diagnostics/src/main.rs87
1 files changed, 40 insertions, 47 deletions
diff --git a/bin/core/imag-diagnostics/src/main.rs b/bin/core/imag-diagnostics/src/main.rs
index a0cf9cf8..88368864 100644
--- a/bin/core/imag-diagnostics/src/main.rs
+++ b/bin/core/imag-diagnostics/src/main.rs
@@ -97,6 +97,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() {
let version = make_imag_version!();
let rt = generate_runtime_setup("imag-diagnostics",
@@ -178,64 +192,43 @@ fn main() {
let mut out = rt.stdout();
- let _ = writeln!(out, "imag version {}", env!("CARGO_PKG_VERSION"))
- .to_exit_code()
- .unwrap_or_exit();
- let _ = writeln!(out, "")
- .to_exit_code()
- .unwrap_or_exit();
- let _ = writeln!(out, "{} entries", n)
- .to_exit_code()
- .unwrap_or_exit();
+ do_write!(out, "imag version {}", { env!("CARGO_PKG_VERSION") });
+ do_write!(out, "");
+ do_write!(out, "{} entries", n);
+
for (k, v) in version_counts {
- let _ = writeln!(out, "{} entries with store version '{}'", v, k)
- .to_exit_code()
- .unwrap_or_exit();
+ do_write!(out, "{} entries with store version '{}'", v, k);
}
if n != 0 {
- let _ = writeln!(out, "{} header sections in the average entry", sum_header_sections / n)
- .to_exit_code()
- .unwrap_or_exit();
- 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();
+ do_write!(out, "{} header sections in the average entry", sum_header_sections / n);
+ do_write!(out, "{} average content bytecount", sum_bytecount_content / n);
+ do_write!(out, "{} average overall bytecount", sum_overall_byte_size / n);
+
if let Some((num, path)) = max_overall_byte_size {
- let _ = writeln!(out,
- "Largest Entry ({bytes} bytes): {path}",
- bytes = num,
- path = path
- .into_pathbuf()
- .map_err_trace_exit_unwrap(1)
- .to_str()
- .unwrap_or("Failed converting path to string")
- )
- .to_exit_code()
- .unwrap_or_exit();
+ do_write!(out, "Largest Entry ({} bytes): {}",
+ num,
+ path
+ .into_pathbuf()
+ .map_err_trace_exit_unwrap(1)
+ .to_str()
+ .unwrap_or("Failed converting path to string")
+ );
}
- let _ = writeln!(out, "{} average internal link count per entry", num_internal_links / n)
- .to_exit_code()
- .unwrap_or_exit();
+
+ do_write!(out, "{} average internal link count per entry", num_internal_links / n);
+
if let Some((num, path)) = max_internal_links {
- let _ = writeln!(out, "Entry with most internal links ({count}): {path}",
- count = num,
- path = path
+ do_write!(out, "Entry with most internal links ({}): {}",
+ num,
+ path
.into_pathbuf()
.map_err_trace_exit_unwrap(1)
.to_str()
.unwrap_or("Failed converting path to string")
- )
- .to_exit_code()
- .unwrap_or_exit();
+ );
}
- let _ = writeln!(out, "{} verified entries", verified_count)
- .to_exit_code()
- .unwrap_or_exit();
- let _ = writeln!(out, "{} unverified entries", unverified_count)
- .to_exit_code()
- .unwrap_or_exit();
+ do_write!(out, "{} verified entries", verified_count);
+ do_write!(out, "{} unverified entries", unverified_count);
}
}