aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/hex.rs
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2021-05-13 05:35:37 +0000
committerNicholas Rodrigues Lordello <[email protected]>2021-05-13 05:35:37 +0000
commit223cd670577c55d65762a056782124dc4c905f04 (patch)
tree1bc0cf9010c7af1c3ff2732856360b88c289b43e /src/cmd/hex.rs
parentacd9b487c189d7f82ae9187f0fffd0607478abc4 (diff)
Use input files for binary input and add utilities for hex encoding
Diffstat (limited to 'src/cmd/hex.rs')
-rw-r--r--src/cmd/hex.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/cmd/hex.rs b/src/cmd/hex.rs
new file mode 100644
index 0000000..7b4e606
--- /dev/null
+++ b/src/cmd/hex.rs
@@ -0,0 +1,49 @@
+//! Module implementing the `address` subcommand for displaying the public
+//! address for corresponding account.
+
+use crate::cmd;
+use anyhow::Result;
+use std::{
+ io::{self, Write},
+ path::PathBuf,
+ str,
+};
+use structopt::StructOpt;
+
+#[derive(Debug, StructOpt)]
+pub struct Options {
+ #[structopt(subcommand)]
+ op: Op,
+}
+
+#[derive(Debug, StructOpt)]
+enum Op {
+ /// Encode binary data as a hexadecimal string.
+ Encode {
+ /// Path to the data to encode. Use `-` for standard in.
+ #[structopt(name = "DATA", default_value = "-")]
+ data: PathBuf,
+ },
+
+ /// Decode a hexadecimal string as binary data.
+ Decode {
+ /// Path to the data to decode. Use `-` for standard in.
+ #[structopt(name = "DATA", default_value = "-")]
+ data: PathBuf,
+ },
+}
+
+pub fn run(options: Options) -> Result<()> {
+ match options.op {
+ Op::Encode { data } => {
+ let data = cmd::read_input(&data)?;
+ println!("0x{}", hex::encode(data));
+ }
+ Op::Decode { data } => {
+ let data = cmd::read_input(&data)?;
+ let bytes = cmd::permissive_hex(str::from_utf8(&data)?)?;
+ io::stdout().write_all(&bytes)?;
+ }
+ }
+ Ok(())
+}