From 9d10c5fe0722aad26b05e2cb53a3523b6a497c64 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 9 Sep 2024 23:55:40 +0800 Subject: [PATCH] feat: v0.1.1, support width and pem feature --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmd_base58.rs | 2 +- src/cmd_base64.rs | 30 +++++++++++++++++++++++++++--- src/cmd_hex.rs | 13 ++++++++++--- src/main.rs | 8 ++++---- src/util.rs | 21 ++++++++++++++++++++- 7 files changed, 64 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a28c9b7..d0ea773 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,7 +144,7 @@ dependencies = [ [[package]] name = "encode" -version = "0.1.0" +version = "0.1.1" dependencies = [ "base58", "base64", diff --git a/Cargo.toml b/Cargo.toml index c168a37..28e1f5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "encode" -version = "0.1.0" +version = "0.1.1" edition = "2021" [dependencies] diff --git a/src/cmd_base58.rs b/src/cmd_base58.rs index 7dd442e..51e213d 100644 --- a/src/cmd_base58.rs +++ b/src/cmd_base58.rs @@ -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."); } diff --git a/src/cmd_base64.rs b/src/cmd_base64.rs index 551ea90..1d39ead 100644 --- a/src/cmd_base64.rs +++ b/src/cmd_base64.rs @@ -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, + /// PEM + #[arg(long)] + pub pem: Option, /// Input file #[arg(long, short = 'i')] pub r#in: Option, } 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(()) } diff --git a/src/cmd_hex.rs b/src/cmd_hex.rs index b04dbb1..b9de067 100644 --- a/src/cmd_hex.rs +++ b/src/cmd_hex.rs @@ -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, /// Input file #[arg(long, short = 'i')] pub r#in: Option, } 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(()) } diff --git a/src/main.rs b/src/main.rs index 755666c..1649b1a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(), } } diff --git a/src/util.rs b/src/util.rs index 74d7cb6..4fe0929 100644 --- a/src/util.rs +++ b/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> { opt_result!(stdin.read_to_end(&mut buffer), "Read stdin failed: {}"); Ok(buffer) } + +pub fn split_output(output: &str, width: usize) -> XResult { + 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(()) +}