Skip to content

Commit

Permalink
Merge pull request #77 from sile/update-clap
Browse files Browse the repository at this point in the history
Update clap to v4
  • Loading branch information
sile authored Apr 30, 2024
2 parents 27628cb + 192980e commit 04565c3
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ default = ["std"]
std = ["libflate_lz77/std", "core2/std"]

[dev-dependencies]
clap = "2"
clap = { version = "4", features = ["derive"] }

[workspace]
members = ["libflate_lz77"]
Expand Down
158 changes: 73 additions & 85 deletions examples/flate.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,47 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate clap;
extern crate libflate;

#[cfg(not(feature = "std"))]
fn main() {}

#[cfg(feature = "std")]
fn main() {
use clap::App;
use clap::Arg;
use clap::SubCommand;
use clap::Parser;
use libflate::gzip;
use libflate::zlib;
use std::fs;
use std::io;
use std::io::Read;
use std::io::Write;
use std::process;

let matches = App::new("deflate")
.arg(
Arg::with_name("INPUT")
.short("i")
.long("input")
.value_name("FILE")
.takes_value(true)
.default_value("-"),
)
.arg(
Arg::with_name("OUTPUT")
.short("o")
.long("output")
.value_name("FILE")
.takes_value(true)
.default_value("-"),
)
.arg(Arg::with_name("VERBOSE").short("v").long("verbose"))
.subcommand(SubCommand::with_name("copy"))
.subcommand(
SubCommand::with_name("byte-read").arg(
Arg::with_name("UNIT")
.short("u")
.long("unit")
.takes_value(true)
.default_value("1"),
),
)
.subcommand(SubCommand::with_name("gzip-decode"))
.subcommand(SubCommand::with_name("gzip-decode-multi"))
.subcommand(SubCommand::with_name("gzip-encode"))
.subcommand(SubCommand::with_name("zlib-decode"))
.subcommand(SubCommand::with_name("zlib-encode"))
.get_matches();
#[derive(Parser)]
struct Args {
#[clap(short, long, default_value = "-")]
input: String,

#[clap(short, long, default_value = "-")]
output: String,

#[clap(short, long)]
verbose: bool,

#[clap(subcommand)]
command: Command,
}

let input_filename = matches.value_of("INPUT").unwrap();
#[derive(clap::Subcommand)]
enum Command {
Copy,
ByteRead {
#[clap(short, long, default_value = "1")]
unit: usize,
},
GzipDecode,
GzipDecodeMulti,
GzipEncode,
ZlibDecode,
ZlibEncode,
}

let args = Args::parse();
let input_filename = &args.input;
let input: Box<dyn io::Read> = if input_filename == "-" {
Box::new(io::stdin())
} else {
Expand All @@ -64,7 +51,7 @@ fn main() {
};
let mut input = io::BufReader::new(input);

let output_filename = matches.value_of("OUTPUT").unwrap();
let output_filename = &args.output;
let output: Box<dyn io::Write> = if output_filename == "-" {
Box::new(io::stdout())
} else if output_filename == "/dev/null" {
Expand All @@ -77,49 +64,50 @@ fn main() {
};
let mut output = io::BufWriter::new(output);

let verbose = matches.is_present("VERBOSE");
if let Some(_matches) = matches.subcommand_matches("copy") {
io::copy(&mut input, &mut output).expect("Coyping failed");
} else if let Some(matches) = matches.subcommand_matches("byte-read") {
let unit = matches
.value_of("UNIT")
.and_then(|x| x.parse::<usize>().ok())
.unwrap();
let mut buf = vec![0; unit];
let mut reader = input;
let mut count = 0;
while let Ok(size) = reader.read(&mut buf) {
if size == 0 {
break;
let verbose = args.verbose;
match args.command {
Command::Copy => {
io::copy(&mut input, &mut output).expect("Coyping failed");
}
Command::ByteRead { unit } => {
let mut buf = vec![0; unit];
let mut reader = input;
let mut count = 0;
while let Ok(size) = reader.read(&mut buf) {
if size == 0 {
break;
}
count += size;
}
count += size;
println!("COUNT: {}", count);
}
println!("COUNT: {}", count);
} else if let Some(_matches) = matches.subcommand_matches("gzip-decode") {
let mut decoder = gzip::Decoder::new(input).expect("Read GZIP header failed");
if verbose {
let _ = writeln!(&mut io::stderr(), "HEADER: {:?}", decoder.header());
Command::GzipDecode => {
let mut decoder = gzip::Decoder::new(input).expect("Read GZIP header failed");
if verbose {
let _ = writeln!(&mut io::stderr(), "HEADER: {:?}", decoder.header());
}
io::copy(&mut decoder, &mut output).expect("Decoding GZIP stream failed");
}
io::copy(&mut decoder, &mut output).expect("Decoding GZIP stream failed");
} else if let Some(_matches) = matches.subcommand_matches("gzip-decode-multi") {
let mut decoder = gzip::MultiDecoder::new(input).expect("Read GZIP header failed");
io::copy(&mut decoder, &mut output).expect("Decoding GZIP stream failed");
} else if let Some(_matches) = matches.subcommand_matches("gzip-encode") {
let mut encoder = gzip::Encoder::new(output).unwrap();
io::copy(&mut input, &mut encoder).expect("Encoding GZIP stream failed");
encoder.finish().into_result().unwrap();
} else if let Some(_matches) = matches.subcommand_matches("zlib-decode") {
let mut decoder = zlib::Decoder::new(input).expect("Read ZLIB header failed");
if verbose {
let _ = writeln!(&mut io::stderr(), "HEADER: {:?}", decoder.header());
Command::GzipDecodeMulti => {
let mut decoder = gzip::MultiDecoder::new(input).expect("Read GZIP header failed");
io::copy(&mut decoder, &mut output).expect("Decoding GZIP stream failed");
}
Command::GzipEncode => {
let mut encoder = gzip::Encoder::new(output).unwrap();
io::copy(&mut input, &mut encoder).expect("Encoding GZIP stream failed");
encoder.finish().into_result().unwrap();
}
Command::ZlibDecode => {
let mut decoder = zlib::Decoder::new(input).expect("Read ZLIB header failed");
if verbose {
let _ = writeln!(&mut io::stderr(), "HEADER: {:?}", decoder.header());
}
io::copy(&mut decoder, &mut output).expect("Decoding ZLIB stream failed");
}
Command::ZlibEncode => {
let mut encoder = zlib::Encoder::new(output).unwrap();
io::copy(&mut input, &mut encoder).expect("Encoding ZLIB stream failed");
encoder.finish().into_result().unwrap();
}
io::copy(&mut decoder, &mut output).expect("Decoding ZLIB stream failed");
} else if let Some(_matches) = matches.subcommand_matches("zlib-encode") {
let mut encoder = zlib::Encoder::new(output).unwrap();
io::copy(&mut input, &mut encoder).expect("Encoding ZLIB stream failed");
encoder.finish().into_result().unwrap();
} else {
println!("{}", matches.usage());
process::exit(1);
}
}
3 changes: 2 additions & 1 deletion flate_bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
name = "flate_bench"
version = "0.1.0"
authors = ["Takeru Ohta <phjgt308@gmail.com>"]
edition = "2021"

[dependencies]
clap = "3"
clap = { version = "4", features = ["derive"] }
flate2 = "*"
inflate = "*"
libflate = {path = "../"}
44 changes: 24 additions & 20 deletions flate_bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
extern crate clap;
extern crate flate2;
extern crate inflate;
extern crate libflate;

use clap::Parser;
use std::fs;
use std::io;
use std::io::Read;
use std::time;
use clap::App;
use clap::Arg;

#[derive(Parser)]
struct Args {
#[clap(short, long)]
disable_flate2: bool,

#[clap(short, long)]
disable_inflate: bool,

#[clap(short, long)]
disable_libflate: bool,

input: String,
}

fn main() {
let matches = App::new("flate_bench")
.arg(Arg::with_name("INPUT").index(1).required(true))
.arg(Arg::with_name("DISABLE_FLATE2").long("disable-flate2"))
.arg(Arg::with_name("DISABLE_INFLATE").long("disable-inflate"))
.arg(Arg::with_name("DISABLE_LIBFLATE").long("disable-libflate"))
.get_matches();
let args = Args::parse();

let input_file_path = matches.value_of("INPUT").unwrap();
let input_file_path = &args.input;
let mut plain = Vec::new();
fs::File::open(input_file_path)
.unwrap()
Expand All @@ -27,14 +30,14 @@ fn main() {

println!("");
println!("# ENCODE (input_size={})", plain.len());
if !matches.is_present("DISABLE_LIBFLATE") {
if !args.disable_libflate {
bench(
"- libflate",
&plain[..],
libflate::deflate::Encoder::new(BenchWriter::new()),
);
}
if !matches.is_present("DISABLE_FLATE2") {
if !args.disable_flate2 {
bench(
"- flate2",
&plain[..],
Expand All @@ -51,7 +54,7 @@ fn main() {
writer.finish().unwrap()
};
println!("# DECODE (input_size={})", compressed.len());
if !matches.is_present("DISABLE_LIBFLATE") {
if !args.disable_libflate {
bench(
"- libflate",
libflate::deflate::Decoder::new(&compressed[..]),
Expand All @@ -63,14 +66,14 @@ fn main() {
BenchWriter::new(),
);
}
if !matches.is_present("DISABLE_FLATE2") {
if !args.disable_flate2 {
bench(
"- flate2",
flate2::read::DeflateDecoder::new(&compressed[..]),
BenchWriter::new(),
);
}
if !matches.is_present("DISABLE_INFLATE") {
if !args.disable_inflate {
bench(
"- inflate",
InflateReader::new(&compressed[..]),
Expand Down Expand Up @@ -174,7 +177,8 @@ where
return Ok(len);
}
let size = {
let (size, output) = self.inflate
let (size, output) = self
.inflate
.update(&self.input_buf[self.input_offset..])
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
self.input_offset += size;
Expand Down

0 comments on commit 04565c3

Please sign in to comment.