feat: init commit

This commit is contained in:
2024-09-08 19:00:18 +08:00
parent 8dfb5b480d
commit f4dd5339bb
8 changed files with 581 additions and 3 deletions

14
src/cmd_base58.rs Normal file
View File

@@ -0,0 +1,14 @@
use clap::Args;
use rust_util::XResult;
use crate::CmdExec;
#[derive(Debug, Args)]
pub struct CmdBase58 {
}
impl CmdExec for CmdBase58 {
fn exec(&self) -> XResult<()> {
todo!()
}
}

12
src/cmd_base64.rs Normal file
View File

@@ -0,0 +1,12 @@
use crate::CmdExec;
use clap::Args;
use rust_util::XResult;
#[derive(Debug, Args)]
pub struct CmdBase64 {}
impl CmdExec for CmdBase64 {
fn exec(&self) -> XResult<()> {
todo!()
}
}

44
src/cmd_hex.rs Normal file
View File

@@ -0,0 +1,44 @@
use crate::util::read_file_or_stdin;
use crate::CmdExec;
use clap::Args;
use rust_util::{opt_result, simple_error, XResult};
use std::io::Write;
#[derive(Debug, Args)]
pub struct CmdHex {
/// Encode
#[arg(long, short = 'e')]
pub encode: bool,
/// Decode
#[arg(long, short = 'd')]
pub decode: bool,
/// Upper case
#[arg(long, short = 'u')]
pub upper_case: bool,
/// Input file
#[arg(long, short = 'i')]
pub r#in: Option<String>,
}
impl CmdExec for CmdHex {
fn exec(&self) -> XResult<()> {
if self.encode && self.decode {
return simple_error!("Encode and decode cannot both assigned.");
}
let input = read_file_or_stdin(&self.r#in)?;
if self.decode {
let decoded = opt_result!(hex::decode(&input), "Decode failed: {}");
std::io::stdout().write_all(&decoded)?;
} else {
let encoded = if self.upper_case {
hex::encode_upper(input)
} else {
hex::encode(&input)
};
print!("{}", encoded);
}
Ok(())
}
}

51
src/main.rs Normal file
View File

@@ -0,0 +1,51 @@
mod cmd_hex;
mod cmd_base64;
mod cmd_base58;
mod util;
use crate::cmd_base58::CmdBase58;
use crate::cmd_base64::CmdBase64;
use crate::cmd_hex::CmdHex;
use clap::{Parser, Subcommand};
use rust_util::{failure, XResult};
#[derive(Debug, Parser)]
#[command(name = "encode")]
#[command(about = "Encode util", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
trait CmdExec {
fn exec(&self) -> XResult<()>;
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Hex encoding
#[command(arg_required_else_help = true, short_flag = 'x')]
Hex(CmdHex),
/// Base64 encoding
#[command(arg_required_else_help = true, short_flag = '6')]
Base64(CmdBase64),
/// Base58 encoding
#[command(arg_required_else_help = true, short_flag = '5')]
Base58(CmdBase58),
}
fn main() {
if let Err(e) = inner_main() {
failure!("Encode failed: {}", e);
std::process::exit(-1);
}
}
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(),
}
}

25
src/util.rs Normal file
View File

@@ -0,0 +1,25 @@
use rust_util::{opt_result, XResult};
use std::fs;
use std::io::Read;
pub fn read_file_or_stdin(path: &Option<String>) -> XResult<Vec<u8>> {
match path {
None => read_stdin(),
Some(path) => if path == "-" {
read_stdin()
} else {
read_file(path)
}
}
}
pub fn read_file(path: &str) -> XResult<Vec<u8>> {
Ok(opt_result!(fs::read(path), "Read file: {} failed: {}", path))
}
pub fn read_stdin() -> XResult<Vec<u8>> {
let mut buffer = vec![];
let mut stdin = std::io::stdin();
opt_result!(stdin.read_to_end(&mut buffer), "Read stdin failed: {}");
Ok(buffer)
}