From fd9df9fb639a1562a1989b61101005913f371970 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Tue, 16 Jan 2024 22:06:31 +0100 Subject: [PATCH] Upgrade to clap 4.4.18 --- Cargo.toml | 2 +- src/cli.rs | 125 ++++++++++++---------------------------------------- src/main.rs | 13 +++--- 3 files changed, 37 insertions(+), 103 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d157d59..508c30e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ required-features = ["model"] [dependencies] backtrace = "0.3.69" -clap = "2.34.0" +clap = { version = "4.4.18", features = ["derive", "env"] } codegen = "0.2.0" byteorder = "1.5.0" serde = "1.0.195" diff --git a/src/cli.rs b/src/cli.rs index f3fe35e..12518fd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,103 +1,36 @@ -use clap::AppSettings; -use clap::{App, Arg}; - -const ARG_RUST_FIELDS_NOT_PUBLIC: [&str; 5] = [ - "RUST_FIELDS_NOT_PUBLIC", - "RUST_FIELDS_NOT_PUBLIC", - "n", - "rust-fields-not-public", - "Whether the fields in the generated rust code are marked 'pub'", -]; - -const ARG_RUST_GETTER_AND_SETTER: [&str; 5] = [ - "RUST_GETTER_AND_SETTER", - "RUST_GETTER_AND_SETTER", - "g", - "rust-getter-and-setter", - "Whether to generate getter and setter for the fields of the generated rust structs", -]; - -const ARG_CONVERSION_TARGET: [&str; 5] = [ - "CONVERT_TO", - "CONVERT_TO", - "t", - "convert-to", - "The target to convert the input files to", -]; - -pub const CONVERSION_TARGET_RUST: &str = "rust"; -pub const CONVERSION_TARGET_PROTO: &str = "proto"; -pub const CONVERSION_TARGET_POSSIBLE_VALUES: [&str; 2] = - [CONVERSION_TARGET_RUST, CONVERSION_TARGET_PROTO]; - -#[derive(Debug)] +#[derive(clap::Parser, Debug)] +#[command(author, version, about, long_about = None)] // Read from `Cargo.toml` pub struct Parameters { + #[arg( + short = 'n', + long = "rust-fields-not-public", + env = "RUST_FIELDS_NOT_PUBLIC", + help = "Whether the fields in the generated rust code are marked 'pub'" + )] pub rust_fields_not_public: bool, + #[arg( + short = 'g', + long = "rust-getter-and-setter", + env = "RUST_GETTER_AND_SETTER", + help = "Whether to generate getter and setter for the fields of the generated rust structs" + )] pub rust_getter_and_setter: bool, - pub conversion_target: String, - pub source_files: Vec, + #[arg( + value_enum, + short = 't', + long = "convert-to", + env = "CONVERT_TO", + help = "The target to convert the input files to" + )] + pub conversion_target: ConversionTarget, + #[arg(env = "DESTINATION_DIR")] pub destination_dir: String, + #[arg(env = "SOURCE_FILES")] + pub source_files: Vec, } -pub fn arg<'a>(values: [&'a str; 5], default: Option<&'a str>) -> Arg<'a, 'a> { - let mut arg = Arg::with_name(values[0]) - .env(values[0]) - .value_name(values[1]) - .short(values[2]) - .long(values[3]) - //.help(values[4]) - .takes_value(true); - - if let Some(default) = default { - arg = arg.default_value(default); - } - - arg -} - -pub fn create_argument_parser<'a, 'b>() -> App<'a, 'b> { - App::new(env!("CARGO_PKG_NAME")) - .version(env!("CARGO_PKG_VERSION")) - .author(env!("CARGO_PKG_AUTHORS")) - .about(env!("CARGO_PKG_DESCRIPTION")) - .setting(AppSettings::ColoredHelp) - .arg(arg(ARG_RUST_FIELDS_NOT_PUBLIC, None).takes_value(false)) - .arg(arg(ARG_RUST_GETTER_AND_SETTER, None).takes_value(false)) - .arg( - arg(ARG_CONVERSION_TARGET, Some(CONVERSION_TARGET_RUST)) - .possible_values(&CONVERSION_TARGET_POSSIBLE_VALUES) - .next_line_help(true), - ) - .arg( - Arg::with_name("DESTINATION_DIR") - .required(true) - .multiple(false) - .value_name("DESTINATION_DIR"), - ) - .arg( - Arg::with_name("SOURCE_FILES") - .required(true) - .multiple(true) - .value_name("SOURCE_FILES"), - ) -} - -pub fn parse_parameters() -> Parameters { - let parser = create_argument_parser(); - let matches = parser.get_matches(); - Parameters { - rust_fields_not_public: matches.is_present(ARG_RUST_FIELDS_NOT_PUBLIC[0]), - rust_getter_and_setter: matches.is_present(ARG_RUST_GETTER_AND_SETTER[0]), - conversion_target: matches - .value_of_lossy(ARG_CONVERSION_TARGET[0]) - .expect("Missing conversion target") - .to_string(), - source_files: matches - .values_of_lossy("SOURCE_FILES") - .expect("Missing source files"), - destination_dir: matches - .value_of_lossy("DESTINATION_DIR") - .expect("Missing destination directory") - .to_string(), - } +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)] +pub enum ConversionTarget { + Rust, + Proto, } diff --git a/src/main.rs b/src/main.rs index 39a3c64..8c77eb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ pub mod macros {} pub mod internal_macros; #[macro_use] -pub extern crate serde_derive; +extern crate serde_derive; pub mod io; pub mod prelude; @@ -25,9 +25,11 @@ use asn1rs::converter::Converter; pub use asn1rs_model::gen; pub use asn1rs_model::model; pub use asn1rs_model::parser; +use crate::cli::ConversionTarget; + pub fn main() { - let params = cli::parse_parameters(); + let params = ::parse(); let mut converter = Converter::default(); for source in ¶ms.source_files { @@ -37,13 +39,12 @@ pub fn main() { } } - let result = match params.conversion_target.as_str() { - cli::CONVERSION_TARGET_RUST => converter.to_rust(¶ms.destination_dir, |rust| { + let result = match params.conversion_target { + ConversionTarget::Rust => converter.to_rust(¶ms.destination_dir, |rust| { rust.set_fields_pub(!params.rust_fields_not_public); rust.set_fields_have_getter_and_setter(params.rust_getter_and_setter); }), - cli::CONVERSION_TARGET_PROTO => converter.to_protobuf(¶ms.destination_dir), - e => panic!("Unexpected CONVERSION_TARGET={}", e), + ConversionTarget::Proto => converter.to_protobuf(¶ms.destination_dir), }; match result {