feat: v0.1.1, support width and pem feature
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -144,7 +144,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "encode"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"base58",
|
||||
"base64",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "encode"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct CmdBase58 {
|
||||
}
|
||||
|
||||
impl CmdExec for CmdBase58 {
|
||||
fn exec(&self) -> XResult<()> {
|
||||
fn exec(&mut self) -> XResult<()> {
|
||||
if self.encode && self.decode {
|
||||
return simple_error!("Encode and decode cannot both assigned.");
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::util::read_file_or_stdin;
|
||||
use crate::util::{read_file_or_stdin, split_output_buff};
|
||||
use crate::CmdExec;
|
||||
use base64::engine::general_purpose::{
|
||||
STANDARD, STANDARD_NO_PAD,
|
||||
@@ -23,13 +23,19 @@ pub struct CmdBase64 {
|
||||
/// No pad
|
||||
#[arg(long, short = 'P')]
|
||||
pub no_pad: bool,
|
||||
/// Width
|
||||
#[arg(long, short = 'w')]
|
||||
pub width: Option<usize>,
|
||||
/// PEM
|
||||
#[arg(long)]
|
||||
pub pem: Option<String>,
|
||||
/// Input file
|
||||
#[arg(long, short = 'i')]
|
||||
pub r#in: Option<String>,
|
||||
}
|
||||
|
||||
impl CmdExec for CmdBase64 {
|
||||
fn exec(&self) -> XResult<()> {
|
||||
fn exec(&mut self) -> XResult<()> {
|
||||
if self.encode && self.decode {
|
||||
return simple_error!("Encode and decode cannot both assigned.");
|
||||
}
|
||||
@@ -47,7 +53,25 @@ impl CmdExec for CmdBase64 {
|
||||
std::io::stdout().write_all(&decoded)?;
|
||||
} else {
|
||||
let encoded = encoder.encode(&input);
|
||||
print!("{}", encoded);
|
||||
if let (Some(_), None) = (&self.pem, self.width) {
|
||||
self.width = Some(64);
|
||||
}
|
||||
let encoded_width = match self.width {
|
||||
None => encoded,
|
||||
Some(w) => {
|
||||
let mut buff = String::with_capacity(encoded.len() + 100);
|
||||
match &self.pem {
|
||||
None => split_output_buff(&mut buff, &encoded, w)?,
|
||||
Some(pem) => {
|
||||
buff.push_str(&format!("-----BEGIN {}-----\n", pem));
|
||||
split_output_buff(&mut buff, &encoded, w)?;
|
||||
buff.push_str(&format!("\n-----END {}-----", pem));
|
||||
}
|
||||
}
|
||||
buff
|
||||
}
|
||||
};
|
||||
print!("{}", encoded_width);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::util::read_file_or_stdin;
|
||||
use crate::util::{read_file_or_stdin, split_output};
|
||||
use crate::CmdExec;
|
||||
use clap::Args;
|
||||
use rust_util::{opt_result, simple_error, XResult};
|
||||
@@ -15,13 +15,16 @@ pub struct CmdHex {
|
||||
/// Upper case
|
||||
#[arg(long, short = 'u')]
|
||||
pub upper_case: bool,
|
||||
/// Width
|
||||
#[arg(long, short = 'w')]
|
||||
pub width: Option<usize>,
|
||||
/// Input file
|
||||
#[arg(long, short = 'i')]
|
||||
pub r#in: Option<String>,
|
||||
}
|
||||
|
||||
impl CmdExec for CmdHex {
|
||||
fn exec(&self) -> XResult<()> {
|
||||
fn exec(&mut self) -> XResult<()> {
|
||||
if self.encode && self.decode {
|
||||
return simple_error!("Encode and decode cannot both assigned.");
|
||||
}
|
||||
@@ -37,7 +40,11 @@ impl CmdExec for CmdHex {
|
||||
} else {
|
||||
hex::encode(&input)
|
||||
};
|
||||
print!("{}", encoded);
|
||||
let encoded_width = match self.width {
|
||||
None => encoded,
|
||||
Some(w) => split_output(&encoded, w)?,
|
||||
};
|
||||
print!("{}", encoded_width);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ struct Cli {
|
||||
}
|
||||
|
||||
trait CmdExec {
|
||||
fn exec(&self) -> XResult<()>;
|
||||
fn exec(&mut self) -> XResult<()>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
@@ -44,8 +44,8 @@ fn main() {
|
||||
fn inner_main() -> XResult<()> {
|
||||
let cli = Cli::parse();
|
||||
match cli.command {
|
||||
Commands::Hex(cmd_hex) => cmd_hex.exec(),
|
||||
Commands::Base64(cmd_base64) => cmd_base64.exec(),
|
||||
Commands::Base58(cmd_base58) => cmd_base58.exec(),
|
||||
Commands::Hex(mut cmd_hex) => cmd_hex.exec(),
|
||||
Commands::Base64(mut cmd_base64) => cmd_base64.exec(),
|
||||
Commands::Base58(mut cmd_base58) => cmd_base58.exec(),
|
||||
}
|
||||
}
|
||||
|
||||
21
src/util.rs
21
src/util.rs
@@ -1,4 +1,4 @@
|
||||
use rust_util::{opt_result, XResult};
|
||||
use rust_util::{opt_result, simple_error, XResult};
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
|
||||
@@ -23,3 +23,22 @@ pub fn read_stdin() -> XResult<Vec<u8>> {
|
||||
opt_result!(stdin.read_to_end(&mut buffer), "Read stdin failed: {}");
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
pub fn split_output(output: &str, width: usize) -> XResult<String> {
|
||||
let mut buff = String::with_capacity(output.len() + 100);
|
||||
split_output_buff(&mut buff, output, width)?;
|
||||
Ok(buff)
|
||||
}
|
||||
|
||||
pub fn split_output_buff(output_buffer: &mut String, output: &str, width: usize) -> XResult<()> {
|
||||
if width == 0 {
|
||||
return simple_error!("Width must be greater than zero.");
|
||||
}
|
||||
for (i, c) in output.chars().enumerate() {
|
||||
if i > 0 && i % width == 0 {
|
||||
output_buffer.push('\n');
|
||||
}
|
||||
output_buffer.push(c);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user