feat: allow override of buffer size
This commit is contained in:
parent
0f1233b45a
commit
6c5c82ee1c
@ -3,6 +3,7 @@ use clap::Args;
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use crate::Cli;
|
||||||
|
|
||||||
/// Decrypt a QMCv1 file
|
/// Decrypt a QMCv1 file
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
@ -16,16 +17,13 @@ pub struct ArgsQMCv1 {
|
|||||||
input: PathBuf,
|
input: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4MiB buffer is working well on my machine.
|
|
||||||
const BUFFER_SIZE: usize = 4 * 1024 * 1024;
|
|
||||||
|
|
||||||
impl ArgsQMCv1 {
|
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_input = File::open(&self.input)?;
|
||||||
let mut file_output = File::create(&self.output)?;
|
let mut file_output = File::create(&self.output)?;
|
||||||
|
|
||||||
let mut offset = 0usize;
|
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) {
|
while let Ok(n) = file_input.read(&mut buffer) {
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
break;
|
break;
|
||||||
|
@ -6,22 +6,28 @@ use std::time::Instant;
|
|||||||
|
|
||||||
mod cmd;
|
mod cmd;
|
||||||
|
|
||||||
|
/// um_cli (rust ver.)
|
||||||
|
/// A cli-tool to unlock encrypted audio files.
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command(name = "um_cli")]
|
#[command(name = "um_cli")]
|
||||||
#[command(version = "0.1")]
|
#[command(version = "0.1")]
|
||||||
#[command(about = "um_cli (rust ver.)", long_about = None)]
|
pub struct Cli {
|
||||||
struct Cli {
|
|
||||||
#[clap(subcommand)]
|
#[clap(subcommand)]
|
||||||
command: Option<Commands>,
|
command: Option<Commands>,
|
||||||
|
|
||||||
/// Time command
|
/// Be more verbose about what is going on.
|
||||||
#[clap(long, short, action=clap::ArgAction::SetTrue)]
|
#[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> {
|
fn run_command(cli: &Cli) -> Result<i32> {
|
||||||
match &cli.command {
|
match &cli.command {
|
||||||
Some(Commands::QMCv1(cmd)) => cmd.run(),
|
Some(Commands::QMCv1(cmd)) => cmd.run(&cli),
|
||||||
None => {
|
None => {
|
||||||
// https://github.com/clap-rs/clap/issues/3857#issuecomment-1161796261
|
// https://github.com/clap-rs/clap/issues/3857#issuecomment-1161796261
|
||||||
todo!("implement a sensible default command, similar to um/cli");
|
todo!("implement a sensible default command, similar to um/cli");
|
||||||
@ -37,7 +43,7 @@ fn main() {
|
|||||||
-1
|
-1
|
||||||
});
|
});
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
if let Some(true) = cli.time {
|
if let Some(true) = cli.verbose {
|
||||||
eprintln!("time: {:?}", duration);
|
eprintln!("time: {:?}", duration);
|
||||||
};
|
};
|
||||||
exit(code);
|
exit(code);
|
||||||
|
Loading…
Reference in New Issue
Block a user