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
50
51
52
|
//! Module implementing the `hex` subcommand for encoding and decoding
//! hexadecimal strings.
//!
//! This subcommand exists because of the lack of an "easy" POSIX-compatible
//! way of decoding hex.
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(())
}
|