Skip to content

Commit

Permalink
Upgrade to clap 4.4.18
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Jan 16, 2024
1 parent 7af3fc4 commit fd9df9f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
125 changes: 29 additions & 96 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[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<String>,
}

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,
}
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = <cli::Parameters as clap::Parser>::parse();
let mut converter = Converter::default();

for source in &params.source_files {
Expand All @@ -37,13 +39,12 @@ pub fn main() {
}
}

let result = match params.conversion_target.as_str() {
cli::CONVERSION_TARGET_RUST => converter.to_rust(&params.destination_dir, |rust| {
let result = match params.conversion_target {
ConversionTarget::Rust => converter.to_rust(&params.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(&params.destination_dir),
e => panic!("Unexpected CONVERSION_TARGET={}", e),
ConversionTarget::Proto => converter.to_protobuf(&params.destination_dir),
};

match result {
Expand Down

0 comments on commit fd9df9f

Please sign in to comment.