Refactor default block size into constant

This commit is contained in:
merkrafter authored and David Peter committed 2021-11-14 09:26:21 +01:00
1 parent 99435cd215
commit 37d4180f9a
3 files changed
+35 -6

No files matched your search

Generated
+23
View File
@@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "0.7.18" version = "0.7.18"
@@ -96,6 +98,26 @@ dependencies = [
"unicode-width", "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]] [[package]]
name = "difflib" name = "difflib"
version = "0.4.0" version = "0.4.0"
@@ -141,6 +163,7 @@ dependencies = [
"assert_cmd", "assert_cmd",
"atty", "atty",
"clap", "clap",
"const_format",
"libc", "libc",
"predicates", "predicates",
"thiserror", "thiserror",
+1
View File
@@ -14,6 +14,7 @@ edition = "2018"
ansi_term = "0.12" ansi_term = "0.12"
anyhow = "1.0.31" anyhow = "1.0.31"
atty = "0.2" atty = "0.2"
const_format = "0.2.22"
libc = "0.2" libc = "0.2"
thiserror = "1.0.19" thiserror = "1.0.19"
+11 -6
View File
@@ -11,10 +11,14 @@ use atty::Stream;
use anyhow::{anyhow, Context, Error as AnyhowError}; use anyhow::{anyhow, Context, Error as AnyhowError};
use const_format::formatcp;
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
use hexyl::{BorderStyle, Input, Printer}; use hexyl::{BorderStyle, Input, Printer};
const DEFAULT_BLOCK_SIZE: i64 = 512;
fn run() -> Result<(), AnyhowError> { fn run() -> Result<(), AnyhowError> {
let app = App::new(crate_name!()) let app = App::new(crate_name!())
.setting(AppSettings::ColorAuto) .setting(AppSettings::ColorAuto)
@@ -76,10 +80,11 @@ fn run() -> Result<(), AnyhowError> {
.long("block-size") .long("block-size")
.takes_value(true) .takes_value(true)
.value_name("SIZE") .value_name("SIZE")
.help( .help(formatcp!(
"Sets the size of the `block` unit to SIZE.\n\ "Sets the size of the `block` unit to SIZE (default is {}).\n\
Examples: --block-size=1024, --block-size=4kB", Examples: --block-size=1024, --block-size=4kB",
), DEFAULT_BLOCK_SIZE
)),
) )
.arg( .arg(
Arg::with_name("nosqueezing") Arg::with_name("nosqueezing")
@@ -146,7 +151,7 @@ fn run() -> Result<(), AnyhowError> {
}) })
}) })
.transpose()? .transpose()?
.unwrap_or_else(|| PositiveI64::new(512).unwrap()); .unwrap_or_else(|| PositiveI64::new(DEFAULT_BLOCK_SIZE).unwrap());
let skip_arg = matches let skip_arg = matches
.value_of("skip") .value_of("skip")
@@ -473,7 +478,7 @@ fn test_parse_byte_offset() {
macro_rules! success { macro_rules! success {
($input: expr, $expected_kind: ident $expected_value: expr) => { ($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) => { ($input: expr, $expected_kind: ident $expected_value: expr; block_size: $block_size: expr) => {
assert_eq!( assert_eq!(
@@ -491,7 +496,7 @@ fn test_parse_byte_offset() {
macro_rules! error { macro_rules! error {
($input: expr, $expected_err: expr) => { ($input: expr, $expected_err: expr) => {
assert_eq!( assert_eq!(
parse_byte_offset($input, PositiveI64::new(512).unwrap()), parse_byte_offset($input, PositiveI64::new(DEFAULT_BLOCK_SIZE).unwrap()),
Err($expected_err), Err($expected_err),
); );
}; };