From 82086f9eaf2cb8b43d4d9ab7b0dc7c51c076c483 Mon Sep 17 00:00:00 2001 From: gustavo-shigueo Date: Sun, 20 Apr 2025 15:11:19 -0300 Subject: [PATCH] Use Option for proper argument merging --- cli/src/cargo.rs | 9 ++++----- cli/src/config.rs | 40 ++++++++++++++++++++-------------------- cli/src/main.rs | 4 ++-- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/cli/src/cargo.rs b/cli/src/cargo.rs index 90607611..f37f7809 100644 --- a/cli/src/cargo.rs +++ b/cli/src/cargo.rs @@ -10,7 +10,7 @@ use crate::{config::ExportConfig, path}; macro_rules! feature { ($cargo_invocation: expr, $args: expr, { $($field: ident => $feature: literal),* $(,)? }) => { $( - if $args.$field { + if $args.$field.unwrap_or(false) { $cargo_invocation .arg("--features") .arg(format!("ts-rs/{}", $feature)); @@ -29,7 +29,7 @@ pub fn invoke(cfg: &ExportConfig) -> Result<()> { .arg("ts-rs/export") .arg("--features") .arg("ts-rs/generate-metadata") - .stdout(if cfg.no_capture { + .stdout(if cfg.no_capture.unwrap_or(false) { Stdio::inherit() } else { Stdio::piped() @@ -51,7 +51,7 @@ pub fn invoke(cfg: &ExportConfig) -> Result<()> { format => "format", }); - if cfg.no_capture { + if cfg.no_capture.unwrap_or(false) { cargo_invocation.arg("--").arg("--nocapture"); } else { cargo_invocation.arg("--quiet"); @@ -72,8 +72,7 @@ pub fn workspace_location() -> Result { match output.status.code() { Some(0) => Ok(PathBuf::from(std::str::from_utf8(&output.stdout)?) .parent() - .map(ToOwned::to_owned) - .unwrap_or_else(|| PathBuf::from("/"))), + .map_or_else(|| PathBuf::from("/"), ToOwned::to_owned)), Some(_) => bail!("{}", std::str::from_utf8(&output.stderr)?), None => bail!("Unable to obtain workspace path"), } diff --git a/cli/src/config.rs b/cli/src/config.rs index d4d41188..179ebea3 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -53,27 +53,27 @@ pub struct ExportConfig { pub output_directory: Option, /// Disables warnings caused by using serde attributes that ts-rs cannot process - #[arg(long)] - pub no_warnings: bool, + #[arg(long, default_missing_value = "true", num_args = 0..=1)] + pub no_warnings: Option, /// Adds the ".js" extension to import paths - #[arg(long)] - pub esm_imports: bool, + #[arg(long, default_missing_value = "true", num_args = 0..=1)] + pub esm_imports: Option, /// Formats the generated TypeScript files - #[arg(long)] - pub format: bool, + #[arg(long, default_missing_value = "true", num_args = 0..=1)] + pub format: Option, /// Generates an index.ts file in your --output-directory that re-exports all /// types generated by ts-rs - #[arg(long = "index")] + #[arg(long = "index", default_missing_value = "true", num_args = 0..=1)] #[serde(rename = "index")] - pub generate_index_ts: bool, + pub generate_index_ts: Option, /// Do not capture `cargo test`'s output, and pass --nocapture to the test binary - #[arg(long = "nocapture")] + #[arg(long = "nocapture", default_missing_value = "true", num_args = 0..=1)] #[serde(rename = "nocapture")] - pub no_capture: bool, + pub no_capture: Option, } impl Default for ExportConfig { @@ -82,11 +82,11 @@ impl Default for ExportConfig { overrides: HashMap::default(), config_file_path: None, output_directory: Some(PathBuf::from("./bindings")), - no_warnings: false, - esm_imports: false, - format: false, - generate_index_ts: false, - no_capture: false, + no_warnings: None, + esm_imports: None, + format: None, + generate_index_ts: None, + no_capture: None, } } } @@ -121,11 +121,11 @@ impl ExportConfig { Self { output_directory: self.output_directory.or(other.output_directory), overrides: other.overrides, - no_warnings: self.no_warnings || other.no_warnings, - esm_imports: self.esm_imports || other.esm_imports, - format: self.format || other.format, - generate_index_ts: self.generate_index_ts || other.generate_index_ts, - no_capture: self.no_capture || other.no_capture, + no_warnings: self.no_warnings.or(other.no_warnings), + esm_imports: self.esm_imports.or(other.esm_imports), + format: self.format.or(other.format), + generate_index_ts: self.generate_index_ts.or(other.generate_index_ts), + no_capture: self.no_capture.or(other.no_capture), config_file_path: None, } } diff --git a/cli/src/main.rs b/cli/src/main.rs index ef0e7466..ed1768b4 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -60,7 +60,7 @@ fn main() -> Result<()> { let metadata_content = fs::read_to_string(&metadata_path)?; let metadata = Metadata::try_from(&*metadata_content)?; - if !args.generate_index_ts || metadata.is_empty() { + if !args.generate_index_ts.unwrap_or(false) || metadata.is_empty() { return Ok(()); } @@ -88,7 +88,7 @@ fn main() -> Result<()> { index.write_all(NOTE)?; - if args.generate_index_ts { + if args.generate_index_ts.unwrap_or(false) { for path in metadata.export_paths() { index.write_fmt(format_args!("\nexport * from {path:?};"))?; }