From 9e64c99475710017c84f015a03dd4a0aabc7e575 Mon Sep 17 00:00:00 2001 From: lovasoa Date: Wed, 4 Oct 2023 23:06:46 +0200 Subject: [PATCH] remove deprecated structopt replace with clap --- Cargo.lock | 176 +++++++++-------------------------------------- Cargo.toml | 4 +- src/arguments.rs | 87 ++++++++++++----------- src/main.rs | 3 +- 4 files changed, 83 insertions(+), 187 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 836021a..04bd0d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -43,15 +43,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" -[[package]] -name = "ansi_term" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" -dependencies = [ - "winapi", -] - [[package]] name = "anstream" version = "0.6.4" @@ -113,17 +104,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi 0.1.19", - "libc", - "winapi", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -278,21 +258,6 @@ dependencies = [ "inout", ] -[[package]] -name = "clap" -version = "2.34.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" -dependencies = [ - "ansi_term", - "atty", - "bitflags 1.3.2", - "strsim", - "textwrap", - "unicode-width", - "vec_map", -] - [[package]] name = "clap" version = "4.4.6" @@ -300,6 +265,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d04704f56c2cde07f43e8e2c154b43f216dc5c92fc98ada720177362f953b956" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -308,8 +274,22 @@ version = "4.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e231faeaca65ebd1ea3c737966bf858971cd38c3849107aa3ea7de90a804e45" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -415,7 +395,7 @@ dependencies = [ "anes", "cast", "ciborium", - "clap 4.4.6", + "clap", "criterion-plot", "is-terminal", "itertools 0.10.5", @@ -524,11 +504,12 @@ checksum = "4f8a51dd197fa6ba5b4dc98a990a43cc13693c23eb0089ebb0fcc1f04152bca6" [[package]] name = "dezoomify-rs" -version = "2.11.2" +version = "2.11.3" dependencies = [ "aes", "base64", "cbc", + "clap", "colour", "criterion", "custom_error", @@ -555,7 +536,6 @@ dependencies = [ "serde_urlencoded", "serde_yaml", "sha-1", - "structopt", "tempdir", "tokio", "url", @@ -787,7 +767,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.37", + "syn", ] [[package]] @@ -905,21 +885,9 @@ checksum = "7dfda62a12f55daeae5015f81b0baea145391cb4520f86c248fc615d72640d12" [[package]] name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - -[[package]] -name = "hermit-abi" -version = "0.1.19" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", -] +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" @@ -1149,7 +1117,7 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" dependencies = [ - "hermit-abi 0.3.3", + "hermit-abi", "rustix", "windows-sys 0.48.0", ] @@ -1353,7 +1321,7 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi 0.3.3", + "hermit-abi", "libc", ] @@ -1407,7 +1375,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.37", + "syn", ] [[package]] @@ -1499,7 +1467,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.37", + "syn", ] [[package]] @@ -1587,30 +1555,6 @@ dependencies = [ "num-integer", ] -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.67" @@ -1989,7 +1933,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.37", + "syn", ] [[package]] @@ -2153,33 +2097,9 @@ checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" [[package]] name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "structopt" -version = "0.3.26" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10" -dependencies = [ - "clap 2.34.0", - "lazy_static", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "subtle" @@ -2187,17 +2107,6 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.37" @@ -2262,15 +2171,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - [[package]] name = "thiserror" version = "1.0.49" @@ -2288,7 +2188,7 @@ checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" dependencies = [ "proc-macro2", "quote", - "syn 2.0.37", + "syn", ] [[package]] @@ -2354,7 +2254,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.37", + "syn", ] [[package]] @@ -2500,12 +2400,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" - [[package]] name = "unicode-width" version = "0.1.11" @@ -2562,12 +2456,6 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - [[package]] name = "version_check" version = "0.9.4" @@ -2620,7 +2508,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.37", + "syn", "wasm-bindgen-shared", ] @@ -2654,7 +2542,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.37", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 7d91860..eb0364e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dezoomify-rs" -version = "2.11.2" +version = "2.11.3" authors = ["lovasoa"] edition = "2021" license-file = "LICENSE" @@ -17,7 +17,7 @@ png = "0.17" tokio = { version = "1", features = ["full"] } futures = "0.3" custom_error = "1.7" -structopt = "0.3" +clap = { version = "4.4.6", features = ["derive"] } evalexpr = "11" regex = "1" lazy_static = "1.4" diff --git a/src/arguments.rs b/src/arguments.rs index 24ebacc..e7d91bb 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -1,44 +1,52 @@ use std::path::PathBuf; use std::time::Duration; +use clap::Parser; use regex::Regex; -use structopt::StructOpt; use crate::dezoomer::Dezoomer; use super::{auto, stdin_line, Vec2d, ZoomError}; -#[derive(StructOpt, Debug)] -#[structopt(author, about)] +#[derive(Parser, Debug)] +#[command(author, version, about, disable_help_flag = true)] pub struct Arguments { - /// Input URL or local file name + /// Displays this help message + #[arg(short = '?', long = "help", action = clap::ArgAction::Help)] + pub display_help: (), + + /// Input URL or local file name. By default, the program will ask for it interactively. pub input_uri: Option, - /// File to which the resulting image should be saved - #[structopt(parse(from_os_str))] + /// File to which the resulting image should be saved. By default the program will + /// generate a name based on the image metadata if available. Otherwise, it will + /// generate a name in the format "dezoomified[_N].{jpg,png}" depending on which + /// files already exist in the current directory, and whether the target image size fits + /// in a JPEG or not. + #[arg()] pub outfile: Option, /// Name of the dezoomer to use - #[structopt(short, long, default_value = "auto")] + #[arg(short, long, default_value = "auto")] dezoomer: String, /// If several zoom levels are available, then select the largest one - #[structopt(short, long)] + #[arg(short, long)] pub largest: bool, /// If several zoom levels are available, then select the one with the largest width that /// is inferior to max-width. - #[structopt(short = "w", long = "max-width")] + #[arg(short = 'w', long = "max-width")] max_width: Option, /// If several zoom levels are available, then select the one with the largest height that /// is inferior to max-height. - #[structopt(short = "h", long = "max-height")] + #[arg(short = 'h', long = "max-height")] max_height: Option, /// Degree of parallelism to use. At most this number of /// tiles will be downloaded at the same time. - #[structopt(short = "n", long = "parallelism", default_value = "16")] + #[arg(short = 'n', long = "parallelism", default_value = "16")] pub parallelism: usize, /// Number of new attempts to make when a tile load fails @@ -46,73 +54,74 @@ pub struct Arguments { /// generic dezoomer, which relies on failed tile loads to detect the /// dimensions of the image. On the contrary, if a server is not reliable, /// set this value to a higher number. - #[structopt(short = "r", long = "retries", default_value = "1")] + #[arg(short = 'r', long = "retries", default_value = "1")] pub retries: usize, /// Amount of time to wait before retrying a request that failed. /// Applies only to the first retry. Subsequent retries follow an /// exponential backoff strategy: each one is twice as long as /// the previous one. - #[structopt(long, default_value = "2s", parse(try_from_str = parse_duration))] + #[arg(long, default_value = "2s", value_parser = parse_duration)] pub retry_delay: Duration, /// A number between 0 and 100 expressing how much to compress the output image. /// For lossy output formats such as jpeg, this affects the quality of the resulting image. /// 0 means less compression, 100 means more compression. /// Currently affects only the JPEG and PNG encoders. - #[structopt(long, default_value = "20")] + #[arg(long, default_value = "20")] pub compression: u8, /// Sets an HTTP header to use on requests. /// This option can be repeated in order to set multiple headers. /// You can use `-H "Referer: URL"` where URL is the URL of the website's /// viewer page in order to let the site think you come from the legitimate viewer. - #[structopt( - short = "H", + #[arg( + short = 'H', long = "header", - parse(try_from_str = parse_header), + value_parser = parse_header, number_of_values = 1 )] pub headers: Vec<(String, String)>, /// Maximum number of idle connections per host allowed at the same time - #[structopt(long, default_value = "32")] + #[arg(long, default_value = "32")] pub max_idle_per_host: usize, /// Whether to accept connecting to insecure HTTPS servers - #[structopt(long)] + #[arg(long)] pub accept_invalid_certs: bool, /// Minimum amount of time to wait between two consequent requests. /// This throttles the flow of image tile requests coming from your computer, /// reducing the risk of crashing the remote server of getting banned for making too many /// requests in a short succession. - #[structopt(long, default_value = "0s", parse(try_from_str = parse_duration))] + #[arg(short = 'i', long, default_value = "0s", value_parser = parse_duration)] pub min_interval: Duration, /// Maximum time between the beginning of a request and the end of a response before ///the request should be interrupted and considered failed - #[structopt(long, default_value = "30s", parse(try_from_str = parse_duration))] + #[arg(long, default_value = "30s", value_parser = parse_duration)] pub timeout: Duration, /// Time after which we should give up when trying to connect to a server - #[structopt(long = "connect-timeout", default_value = "6s", parse(try_from_str = parse_duration))] + #[arg(long = "connect-timeout", default_value = "6s", value_parser = parse_duration)] pub connect_timeout: Duration, /// Level of logging verbosity. Set it to "debug" to get all logging messages. - #[structopt(long, default_value = "warn")] + #[arg(long, default_value = "warn")] pub logging: String, /// A place to store the image tiles when after they are downloaded and decrypted. /// By default, tiles are not stored to disk (which is faster), but using a tile cache allows /// retrying partially failed downloads, or stitching the tiles with an external program. - #[structopt(short = "c", long = "tile-cache")] + #[arg(short = 'c', long = "tile-cache")] pub tile_storage_folder: Option, } impl Default for Arguments { fn default() -> Self { Arguments { + display_help: (), input_uri: None, outfile: None, dezoomer: "auto".to_string(), @@ -199,20 +208,21 @@ fn parse_duration(s: &str) -> Result { } #[test] -fn test_headers_and_input() -> Result<(), structopt::clap::Error> { - let args: Arguments = StructOpt::from_iter_safe( - [ - "dezoomify-rs", - "--header", - "Referer: http://test.com", - "--header", - "User-Agent: custom", - "--header", - "A:B", - "input-url", - ] - .iter(), - )?; +fn test_headers_and_input() { + let args = Arguments::parse_from([ + "dezoomify-rs", + // + "--header", + "Referer: http://test.com", + // + "--header", + "User-Agent: custom", + // + "--header", + "A:B", + // + "input-url", + ]); assert_eq!(args.input_uri, Some("input-url".into())); assert_eq!( args.headers, @@ -222,7 +232,6 @@ fn test_headers_and_input() -> Result<(), structopt::clap::Error> { ("A".into(), "B".into()), ] ); - Ok(()) } #[test] diff --git a/src/main.rs b/src/main.rs index 0df05e4..dfc1f08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ use colour::{green_ln, red_ln, yellow_ln}; use human_panic::setup_panic; -use structopt::StructOpt; use dezoomify_rs::{Arguments, dezoomify, ZoomError}; @@ -9,7 +8,7 @@ async fn main() { setup_panic!(); let has_args = std::env::args_os().count() > 1; let mut has_errors = false; - let args: Arguments = Arguments::from_args(); + let args: Arguments = clap::Parser::parse(); init_log(&args); loop {