Skip to content
Closed
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
37 changes: 21 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(),
Expand Down