Skip to content

Commit

Permalink
chore: modify according review
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jun 16, 2019
1 parent 2bd67a9 commit ec818c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion ckb-bin/src/subcommand/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pub fn init(args: InitArgs) -> Result<(), ExitCode> {
data = \"{}\"",
hash,
args.block_assembler_args.join("\", \""),
args.block_assembler_data.unwrap_or_default()
args.block_assembler_data
.unwrap_or_else(|| "0x".to_string())
)
}
None => {
Expand Down
17 changes: 13 additions & 4 deletions util/app-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_CODE_HASH)
.long(ARG_BA_CODE_HASH)
.value_name("code_hash")
.validator(|code_hash| is_hex(code_hash.as_str()))
.validator(is_hex)
.takes_value(true)
.help(
"Sets code_hash in [block_assembler] \
Expand All @@ -250,7 +250,7 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_ARG)
.long(ARG_BA_ARG)
.value_name("arg")
.validator(|arg| is_hex(arg.as_str()))
.validator(is_hex)
.multiple(true)
.number_of_values(1)
.help("Sets args in [block_assembler]"),
Expand All @@ -259,7 +259,7 @@ fn init() -> App<'static, 'static> {
Arg::with_name(ARG_BA_DATA)
.long(ARG_BA_DATA)
.value_name("data")
.validator(|data| is_hex(data.as_str()))
.validator(is_hex)
.help("Sets data in [block_assembler]"),
)
.arg(
Expand All @@ -277,13 +277,22 @@ fn init() -> App<'static, 'static> {
)
}

fn is_hex(hex: &str) -> Result<(), String> {
fn is_hex(hex: String) -> Result<(), String> {
let tmp = hex.as_bytes();
if tmp.len() < 2 {
Err("Must be a hexadecimal string".to_string())
} else if tmp.len() & 1 != 0 {
Err("Hexadecimal strings must be of even length".to_string())
} else if tmp[..2] == b"0x"[..] {
for byte in &tmp[2..] {
match byte {
b'A'...b'F' | b'a'...b'f' | b'0'...b'9' => continue,
invalid_char => {
return Err(format!("Hex has invalid char: {}", invalid_char));
}
}
}

Ok(())
} else {
Err("Must hex string".to_string())
Expand Down

0 comments on commit ec818c3

Please sign in to comment.