use crate::modules::base::Hex;
use crate::modules::{base, Command, Module};
use clap::{Arg, ArgMatches, SubCommand};
use data_encoding::BASE32;
pub fn module<'a, 'b>() -> Module<'a, 'b> {
Module {
desc: "Hex / base32 conversion".to_string(),
commands: commands(),
get_cases: cases::cases,
}
}
pub fn commands<'a, 'b>() -> Vec<Command<'a, 'b>> {
vec![
Command {
app: SubCommand::with_name("h2b32")
.about("Convert hex to base32")
.arg(Arg::with_name("INPUT").required(false).index(1)),
f: h2b32,
},
Command {
app: SubCommand::with_name("b322h")
.about("Convert base32 to hex")
.arg(Arg::with_name("INPUT").required(false).index(1)),
f: b322h,
},
]
}
fn h2b32(matches: &ArgMatches) -> Result<Vec<String>, String> {
let input = base::input_string(matches)?;
let input: Vec<u8> = input.parse::<Hex>().map_err(|_| "Convert failed")?.into();
let result = BASE32.encode(&input);
Ok(vec![result])
}
fn b322h(matches: &ArgMatches) -> Result<Vec<String>, String> {
let input = base::input_string(matches)?;
let result = BASE32
.decode(input.as_ref())
.map_err(|_| "Convert failed")?;
let result = Hex::from(result).into();
Ok(vec![result])
}
mod cases {
use crate::modules::Case;
use linked_hash_map::LinkedHashMap;
pub fn cases() -> LinkedHashMap<&'static str, Vec<Case>> {
vec![
(
"h2b32",
vec![Case {
desc: "".to_string(),
input: vec!["0xc592"].into_iter().map(Into::into).collect(),
output: vec!["YWJA===="].into_iter().map(Into::into).collect(),
is_example: true,
is_test: true,
since: "0.1.0".to_string(),
}],
),
(
"b322h",
vec![Case {
desc: "".to_string(),
input: vec!["YWJA===="].into_iter().map(Into::into).collect(),
output: vec!["0xc592"].into_iter().map(Into::into).collect(),
is_example: true,
is_test: true,
since: "0.1.0".to_string(),
}],
),
]
.into_iter()
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::modules::base::test::test_module;
#[test]
fn test_cases() {
test_module(module());
}
}