From 37d4180f9a73c61eb01a12c52d8ccad9c8cc3514 Mon Sep 17 00:00:00 2001 From: merkrafter Date: Sun, 31 Oct 2021 15:29:41 +0100 Subject: [PATCH] Refactor default block size into constant --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 1 + src/bin/hexyl.rs | 17 +++++++++++------ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c6b060a..d53e841 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "aho-corasick" version = "0.7.18" @@ -96,6 +98,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "const_format" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22bc6cd49b0ec407b680c3e380182b6ac63b73991cb7602de350352fc309b614" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef196d5d972878a48da7decb7686eded338b4858fbabeed513d63a7c98b2b82d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "difflib" version = "0.4.0" @@ -141,6 +163,7 @@ dependencies = [ "assert_cmd", "atty", "clap", + "const_format", "libc", "predicates", "thiserror", diff --git a/Cargo.toml b/Cargo.toml index 8bcb2cd..2657ce2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ edition = "2018" ansi_term = "0.12" anyhow = "1.0.31" atty = "0.2" +const_format = "0.2.22" libc = "0.2" thiserror = "1.0.19" diff --git a/src/bin/hexyl.rs b/src/bin/hexyl.rs index ca24ddc..55f072c 100644 --- a/src/bin/hexyl.rs +++ b/src/bin/hexyl.rs @@ -11,10 +11,14 @@ use atty::Stream; use anyhow::{anyhow, Context, Error as AnyhowError}; +use const_format::formatcp; + use thiserror::Error as ThisError; use hexyl::{BorderStyle, Input, Printer}; +const DEFAULT_BLOCK_SIZE: i64 = 512; + fn run() -> Result<(), AnyhowError> { let app = App::new(crate_name!()) .setting(AppSettings::ColorAuto) @@ -76,10 +80,11 @@ fn run() -> Result<(), AnyhowError> { .long("block-size") .takes_value(true) .value_name("SIZE") - .help( - "Sets the size of the `block` unit to SIZE.\n\ + .help(formatcp!( + "Sets the size of the `block` unit to SIZE (default is {}).\n\ Examples: --block-size=1024, --block-size=4kB", - ), + DEFAULT_BLOCK_SIZE + )), ) .arg( Arg::with_name("nosqueezing") @@ -146,7 +151,7 @@ fn run() -> Result<(), AnyhowError> { }) }) .transpose()? - .unwrap_or_else(|| PositiveI64::new(512).unwrap()); + .unwrap_or_else(|| PositiveI64::new(DEFAULT_BLOCK_SIZE).unwrap()); let skip_arg = matches .value_of("skip") @@ -473,7 +478,7 @@ fn test_parse_byte_offset() { macro_rules! success { ($input: expr, $expected_kind: ident $expected_value: expr) => { - success!($input, $expected_kind $expected_value; block_size: 512) + success!($input, $expected_kind $expected_value; block_size: DEFAULT_BLOCK_SIZE) }; ($input: expr, $expected_kind: ident $expected_value: expr; block_size: $block_size: expr) => { assert_eq!( @@ -491,7 +496,7 @@ fn test_parse_byte_offset() { macro_rules! error { ($input: expr, $expected_err: expr) => { assert_eq!( - parse_byte_offset($input, PositiveI64::new(512).unwrap()), + parse_byte_offset($input, PositiveI64::new(DEFAULT_BLOCK_SIZE).unwrap()), Err($expected_err), ); };