feat: allow override of buffer size

This commit is contained in:
鲁树人 2024-09-05 02:21:34 +01:00
parent 0f1233b45a
commit 6c5c82ee1c
2 changed files with 15 additions and 11 deletions

View File

@ -3,6 +3,7 @@ use clap::Args;
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
use crate::Cli;
/// Decrypt a QMCv1 file
#[derive(Args)]
@ -16,16 +17,13 @@ pub struct ArgsQMCv1 {
input: PathBuf,
}
// 4MiB buffer is working well on my machine.
const BUFFER_SIZE: usize = 4 * 1024 * 1024;
impl ArgsQMCv1 {
pub fn run(&self) -> Result<i32> {
pub fn run(&self, cli: &Cli) -> Result<i32> {
let mut file_input = File::open(&self.input)?;
let mut file_output = File::create(&self.output)?;
let mut offset = 0usize;
let mut buffer = vec![0u8; BUFFER_SIZE].into_boxed_slice();
let mut buffer = vec![0u8; cli.buffer_size].into_boxed_slice();
while let Ok(n) = file_input.read(&mut buffer) {
if n == 0 {
break;

View File

@ -6,22 +6,28 @@ use std::time::Instant;
mod cmd;
/// um_cli (rust ver.)
/// A cli-tool to unlock encrypted audio files.
#[derive(Parser)]
#[command(name = "um_cli")]
#[command(version = "0.1")]
#[command(about = "um_cli (rust ver.)", long_about = None)]
struct Cli {
pub struct Cli {
#[clap(subcommand)]
command: Option<Commands>,
/// Time command
/// Be more verbose about what is going on.
#[clap(long, short, action=clap::ArgAction::SetTrue)]
time: Option<bool>,
verbose: Option<bool>,
/// Preferred buffer size when reading file, in bytes.
/// Default to 4MiB.
#[clap(long, short = 'B', default_value_t=4*1024*1024)]
buffer_size: usize
}
fn run_command(cli: &Cli) -> Result<i32> {
match &cli.command {
Some(Commands::QMCv1(cmd)) => cmd.run(),
Some(Commands::QMCv1(cmd)) => cmd.run(&cli),
None => {
// https://github.com/clap-rs/clap/issues/3857#issuecomment-1161796261
todo!("implement a sensible default command, similar to um/cli");
@ -37,7 +43,7 @@ fn main() {
-1
});
let duration = start.elapsed();
if let Some(true) = cli.time {
if let Some(true) = cli.verbose {
eprintln!("time: {:?}", duration);
};
exit(code);