Add a `revhex` utility command to reverse endianness.

This makes it easier to translate block hashes output by our debug logs into
the format used by other tools.
This commit is contained in:
Henry de Valence 2020-02-18 11:55:55 -08:00 committed by Deirdre Connolly
parent 80e7ee6dae
commit b951f13f06
2 changed files with 52 additions and 13 deletions

View File

@ -2,12 +2,14 @@
mod config;
mod connect;
mod revhex;
mod seed;
mod start;
mod version;
use self::{
config::ConfigCmd, connect::ConnectCmd, seed::SeedCmd, start::StartCmd, version::VersionCmd,
config::ConfigCmd, connect::ConnectCmd, revhex::RevhexCmd, seed::SeedCmd, start::StartCmd,
version::VersionCmd,
};
use crate::config::ZebradConfig;
use abscissa_core::{
@ -21,29 +23,33 @@ pub const CONFIG_FILE: &str = "zebrad.toml";
/// Zebrad Subcommands
#[derive(Command, Debug, Options, Runnable)]
pub enum ZebradCmd {
/// The `help` subcommand
#[options(help = "get usage information")]
Help(Help<Self>),
/// The `start` subcommand
#[options(help = "start the application")]
Start(StartCmd),
/// The `config` subcommand
#[options(help = "generate a skeleton configuration")]
Config(ConfigCmd),
/// The `version` subcommand
#[options(help = "display version information")]
Version(VersionCmd),
/// The `connect` subcommand
#[options(help = "testing stub for dumping network messages")]
Connect(ConnectCmd),
/// The `help` subcommand
#[options(help = "get usage information")]
Help(Help<Self>),
/// The `revhex` subcommand
#[options(help = "reverses the endianness of a hex string, like a block or transaction hash")]
Revhex(RevhexCmd),
/// The `seed` subcommand
#[options(help = "dns seeder")]
Seed(SeedCmd),
/// The `start` subcommand
#[options(help = "start the application")]
Start(StartCmd),
/// The `version` subcommand
#[options(help = "display version information")]
Version(VersionCmd),
}
/// This trait allows you to define how application configuration is loaded.

View File

@ -0,0 +1,33 @@
//! `revhex` subcommand - reverses hex endianness.
#![allow(clippy::never_loop)]
use abscissa_core::{Command, Options, Runnable};
/// `revhex` subcommand
#[derive(Command, Debug, Default, Options)]
pub struct RevhexCmd {
/// The hex string whose endianness will be reversed.
#[options(free)]
input: String,
}
impl Runnable for RevhexCmd {
/// Print endian-reversed hex string.
fn run(&self) {
println!(
"{}",
String::from_utf8(
self.input
.as_bytes()
.chunks(2)
.rev()
.map(|c| c.iter())
.flatten()
.cloned()
.collect::<Vec<u8>>(),
)
.expect("input should be ascii")
);
}
}