diff --git a/zebrad/src/commands.rs b/zebrad/src/commands.rs index 635461de3..f7962246e 100644 --- a/zebrad/src/commands.rs +++ b/zebrad/src/commands.rs @@ -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), - - /// 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), + + /// 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. diff --git a/zebrad/src/commands/revhex.rs b/zebrad/src/commands/revhex.rs new file mode 100644 index 000000000..1e9258651 --- /dev/null +++ b/zebrad/src/commands/revhex.rs @@ -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::>(), + ) + .expect("input should be ascii") + ); + } +}