From a424630c4587ac8c2343319e2c139a5a4d945479 Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Tue, 30 Apr 2024 21:16:15 +0900 Subject: [PATCH] Update clap to v4 --- Cargo.toml | 3 +- examples/flate.rs | 158 +++++++++++++++++++--------------------- flate_bench/Cargo.toml | 3 +- flate_bench/src/main.rs | 44 ++++++----- 4 files changed, 101 insertions(+), 107 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 56dda40..ef420f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,13 +20,14 @@ crc32fast = { version = "1.1.1", default-features = false } dary_heap = "0.3.5" libflate_lz77 = { path = "libflate_lz77", version = "2.0.0", default-features = false } core2 = { version = "0.4", default-features = false, features = ["alloc"] } +clap = { version = "4", features = ["derive"] } [features] default = ["std"] std = ["libflate_lz77/std", "core2/std"] [dev-dependencies] -clap = "2" +clap = "4" [workspace] members = ["libflate_lz77"] diff --git a/examples/flate.rs b/examples/flate.rs index 5162da4..9bba420 100644 --- a/examples/flate.rs +++ b/examples/flate.rs @@ -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 = if input_filename == "-" { Box::new(io::stdin()) } else { @@ -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 = if output_filename == "-" { Box::new(io::stdout()) } else if output_filename == "/dev/null" { @@ -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::().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); } } diff --git a/flate_bench/Cargo.toml b/flate_bench/Cargo.toml index 1396752..e97c6a5 100644 --- a/flate_bench/Cargo.toml +++ b/flate_bench/Cargo.toml @@ -2,9 +2,10 @@ name = "flate_bench" version = "0.1.0" authors = ["Takeru Ohta "] +edition = "2021" [dependencies] -clap = "3" +clap = { version = "4", features = ["derive"] } flate2 = "*" inflate = "*" libflate = {path = "../"} diff --git a/flate_bench/src/main.rs b/flate_bench/src/main.rs index 9b27724..2d3e581 100644 --- a/flate_bench/src/main.rs +++ b/flate_bench/src/main.rs @@ -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() @@ -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[..], @@ -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[..]), @@ -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[..]), @@ -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;