Skip to content

Conversation

@tomasol
Copy link

@tomasol tomasol commented Nov 30, 2025

Add RustWasm::generate_to_out_dir that parses the wit folder, selects a world, generates bindings and places them in a file in the OUT_DIR. generate_to_out_dir_modify allows modifying the generated file.

Sample build.rs

use anyhow::Result;
use std::borrow::Cow;
use wit_bindgen_rust::Opts;

fn main() -> Result<()> {
    Opts {
        generate_all: true,
        additional_derive_attributes: vec![
            "serde::Serialize".to_string(),
            "serde::Deserialize".to_string(),
        ],
        ..Default::default()
    }
    .build()
    .generate_to_out_dir_modify(Some("any"), |contents| {
        let contents = String::from_utf8(contents.to_vec()).unwrap();
        let re = regex::Regex::new(r"(pub\s+enum\s+\w+)").unwrap();
        Cow::Owned(
            re.replace_all(&contents, "#[serde(rename_all = \"kebab-case\")]\n$1")
                .into_owned()
                .into_bytes(),
        )
    })
}

Then the following snipped would include it:

mod generated {
    include!(concat!(env!("OUT_DIR"), "/wit_bindgen_generated.rs"));
}

Although this should be simplified as well.

Closes #1423

Add `RustWasm::generate_to_out_dir` that parses the `wit` folder,
selects a world, generates bindings and places them in a file in the
`OUT_DIR`. `generate_to_out_dir_modify` allows modifying the generated
file.
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Thanks!

Was there anything else you'd like to address before merging?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust: Generating bindings in build.rs

2 participants