aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/hex.rs
blob: 7b4e606eec505f6721c0b07079e1a3a7e790e4ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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(())
}