Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cli/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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()
Expand All @@ -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");
Expand All @@ -72,8 +72,7 @@ pub fn workspace_location() -> Result<PathBuf> {
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"),
}
Expand Down
40 changes: 20 additions & 20 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ pub struct ExportConfig {
pub output_directory: Option<PathBuf>,

/// 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<bool>,
Comment on lines +56 to +57
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will parse CLI args as follows:

CLI argument Value
- None
--no-warnings Some(true)
--no-warnings true Some(true)
--no-warnings=true Some(true)
--no-warnings false Some(false)
--no-warnings=false Some(false)


/// 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<bool>,

/// Formats the generated TypeScript files
#[arg(long)]
pub format: bool,
#[arg(long, default_missing_value = "true", num_args = 0..=1)]
pub format: Option<bool>,

/// 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<bool>,

/// 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<bool>,
}

impl Default for ExportConfig {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will take whatever value is in the CLI args except None, in which case the config file is used instead, so now, if the config file has no-warnings = true and the CLI has --no-warnings=false, no_warnings will be Some(false), where as it would be true in the previous implementation

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,
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Collaborator Author

@gustavo-shigueo gustavo-shigueo May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what's the "best" way to check for Some(true), a few options came to mind:

  • == Some(true)
  • .is_some_and(|x| x)
  • .is_some_and(std::convert::identity)

Let me know if you have any particular preference @NyxCode

return Ok(());
}

Expand Down Expand Up @@ -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:?};"))?;
}
Expand Down
Loading