From f9cac996fa4e4f83582cc1886d9c8dfd190e0d81 Mon Sep 17 00:00:00 2001 From: Guillaume 'Yugo' Coguiec Date: Tue, 3 Sep 2024 11:34:38 -0400 Subject: [PATCH] feat: Add ability to customize bindings output path. --- src/lib.rs | 37 +++++++++++++++++++++---------------- src/metadata.rs | 3 +++ 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5035cfb1..ba1e5bf9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -829,14 +829,17 @@ async fn generate_package_bindings( None => return Ok(HashMap::new()), }; - // TODO: make the output path configurable - let output_dir = resolution - .metadata - .manifest_path - .parent() - .unwrap() - .join("src"); - let bindings_path = output_dir.join("bindings.rs"); + let bindings_path_meta = &resolution.metadata.section.bindings.path; + let bindings_path = if bindings_path_meta.is_absolute() { + bindings_path_meta + } else { + &resolution + .metadata + .manifest_path + .parent() + .unwrap() + .join(bindings_path_meta) + }; config.terminal().status( "Generating", @@ -845,20 +848,22 @@ async fn generate_package_bindings( name = resolution.metadata.name, path = bindings_path .strip_prefix(cwd) - .unwrap_or(&bindings_path) + .unwrap_or(bindings_path) .display() ), )?; let bindings = generator.generate()?; - fs::create_dir_all(&output_dir).with_context(|| { - format!( - "failed to create output directory `{path}`", - path = output_dir.display() - ) - })?; + if let Some(output_dir) = bindings_path.parent() { + fs::create_dir_all(output_dir).with_context(|| { + format!( + "failed to create output directory `{path}`", + path = output_dir.display() + ) + })?; + } - fs::write(&bindings_path, bindings).with_context(|| { + fs::write(bindings_path, bindings).with_context(|| { format!( "failed to write bindings file `{path}`", path = bindings_path.display() diff --git a/src/metadata.rs b/src/metadata.rs index 4eac2117..b58b574d 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -60,6 +60,8 @@ impl FromStr for Ownership { #[derive(Debug, Clone, Deserialize)] #[serde(default)] pub struct Bindings { + /// The path where bindings will be generated (default to `src/bindings.rs`). + pub path: PathBuf, /// Whether or not to run `rustfmt` on the bindings; defaults to true. pub format: bool, /// The ownership model for generated types. @@ -106,6 +108,7 @@ pub struct Bindings { impl Default for Bindings { fn default() -> Self { Self { + path: PathBuf::from("src/bindings.rs"), format: true, ownership: Default::default(), derives: Default::default(),