Skip to main content

bitwarden_ffi_macro/
lib.rs

1//! Proc macros for FFI bindings (WASM, UniFFI).
2//!
3//! Provides:
4//! - `#[wasm_export]` attribute macro for hiding WASM binding methods from Rust consumers.
5
6use proc_macro::TokenStream;
7
8mod wasm_export;
9
10/// Attribute macro for impl blocks containing WASM bindings.
11///
12/// Methods within the impl block can be marked with `#[wasm_only]` to indicate they are
13/// intended only for JavaScript consumers via `wasm_bindgen`, not for direct Rust usage.
14///
15/// For each `#[wasm_only]` method, the macro:
16/// - Renames it with a `__wasm_only_` prefix (e.g. `subscribe` -> `__wasm_only_subscribe`)
17/// - Adds `#[wasm_bindgen(js_name = "subscribe")]` to preserve the JS API (if no `js_name` is
18///   already set)
19/// - Adds `#[doc(hidden)]` to hide it from Rust documentation
20/// - Adds `#[deprecated]` so it shows with strikethrough in IDE autocomplete
21///
22/// This must be placed **before** (above) the `#[wasm_bindgen]` attribute so it expands first.
23///
24/// # Example
25///
26/// ```ignore
27/// #[wasm_export]
28/// #[wasm_bindgen(js_class = IpcClient)]
29/// impl JsIpcClient {
30///     // Left as-is: Rust code may need to construct this type
31///     #[wasm_bindgen(js_name = newWithSdkInMemorySessions)]
32///     pub fn new_with_sdk_in_memory_sessions(...) -> JsIpcClient { ... }
33///
34///     // Mangled: Rust consumers should use the inner client directly
35///     #[wasm_only]
36///     pub async fn subscribe(&self) -> Result<JsIpcClientSubscription, SubscribeError> { ... }
37/// }
38/// ```
39#[proc_macro_attribute]
40pub fn wasm_export(_attr: TokenStream, item: TokenStream) -> TokenStream {
41    wasm_export::wasm_export(item.into()).into()
42}