From bd3b8b4bb78755d553b9d3222d28722414998394 Mon Sep 17 00:00:00 2001 From: Justin Gaffney Date: Sat, 22 Mar 2025 14:03:50 +1000 Subject: [PATCH 1/3] Allow passing async options to wit-bindgen from package metadata --- src/bindings.rs | 15 ++++++++++----- src/metadata.rs | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index dab7c287..8d3f729f 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -12,14 +12,14 @@ use indexmap::{IndexMap, IndexSet}; use semver::Version; use wasm_pkg_client::PackageRef; use wit_bindgen_core::Files; -use wit_bindgen_rust::{AsyncConfig, Opts, WithOption}; +use wit_bindgen_rust::{Opts, WithOption}; use wit_component::DecodedWasm; use wit_parser::{ Interface, Package, PackageName, Resolve, Type, TypeDefKind, TypeOwner, UnresolvedPackageGroup, World, WorldId, WorldItem, WorldKey, }; -use crate::{metadata::Ownership, registry::PackageDependencyResolution}; +use crate::{metadata::{AsyncConfig, Ownership}, registry::PackageDependencyResolution}; // Used to format `unlocked-dep` import names for dependencies on // other components. @@ -135,9 +135,14 @@ impl<'a> BindingsGenerator<'a> { pub_export_macro: settings.pub_export_macro, generate_unused_types: settings.generate_unused_types, disable_custom_section_link_helpers: settings.disable_custom_section_link_helpers, - - // TODO: pipe this through to the CLI options, requires valid serde impls - async_: AsyncConfig::None, + async_: match &settings.async_ { + AsyncConfig::None => wit_bindgen_rust::AsyncConfig::None, + AsyncConfig::Some { imports, exports } => wit_bindgen_rust::AsyncConfig::Some { + imports: imports.clone(), + exports: exports.clone(), + }, + AsyncConfig::All => wit_bindgen_rust::AsyncConfig::All, + } }; let mut files = Files::default(); diff --git a/src/metadata.rs b/src/metadata.rs index a9abdb76..46ddea29 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -56,6 +56,18 @@ impl FromStr for Ownership { } } +#[derive(Default, Debug, Clone, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum AsyncConfig { + #[default] + None, + Some { + imports: Vec, + exports: Vec, + }, + All, +} + /// Configuration for bindings generation. #[derive(Debug, Clone, Deserialize)] #[serde(default)] @@ -110,6 +122,16 @@ pub struct Bindings { /// Disabling this can shave a few bytes off a binary but makes /// library-based usage of `generate!` prone to breakage. pub disable_custom_section_link_helpers: bool, + /// Determines which functions to lift or lower `async`, if any. + /// + /// Accepted values are: + /// - none + /// - all + /// - some=[,...], where each is of the form: + /// - import: or + /// - export: + #[serde(rename = "async")] + pub async_: AsyncConfig, } impl Default for Bindings { @@ -132,6 +154,7 @@ impl Default for Bindings { pub_export_macro: Default::default(), generate_unused_types: Default::default(), disable_custom_section_link_helpers: Default::default(), + async_: Default::default(), } } } From 54e426d6a84268b7ec41970b81500e677378607a Mon Sep 17 00:00:00 2001 From: Justin Gaffney Date: Sat, 22 Mar 2025 14:07:40 +1000 Subject: [PATCH 2/3] Run `cargo fmt` --- src/bindings.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/bindings.rs b/src/bindings.rs index 8d3f729f..1b8e730e 100644 --- a/src/bindings.rs +++ b/src/bindings.rs @@ -19,7 +19,10 @@ use wit_parser::{ World, WorldId, WorldItem, WorldKey, }; -use crate::{metadata::{AsyncConfig, Ownership}, registry::PackageDependencyResolution}; +use crate::{ + metadata::{AsyncConfig, Ownership}, + registry::PackageDependencyResolution, +}; // Used to format `unlocked-dep` import names for dependencies on // other components. @@ -142,7 +145,7 @@ impl<'a> BindingsGenerator<'a> { exports: exports.clone(), }, AsyncConfig::All => wit_bindgen_rust::AsyncConfig::All, - } + }, }; let mut files = Files::default(); From 4cd6759956237fcfed107771cc3e51efb1e089e0 Mon Sep 17 00:00:00 2001 From: Justin Gaffney Date: Tue, 25 Mar 2025 19:34:18 +1000 Subject: [PATCH 3/3] Remove copied doco that was specific to wit-bindgen parsing CLI args --- src/metadata.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/metadata.rs b/src/metadata.rs index 46ddea29..672207e7 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -123,13 +123,6 @@ pub struct Bindings { /// library-based usage of `generate!` prone to breakage. pub disable_custom_section_link_helpers: bool, /// Determines which functions to lift or lower `async`, if any. - /// - /// Accepted values are: - /// - none - /// - all - /// - some=[,...], where each is of the form: - /// - import: or - /// - export: #[serde(rename = "async")] pub async_: AsyncConfig, }