fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAllow rust_wasm_bindgen rules to re-expose rust-analyzer providers (#3035)13746789

main
1.7 KiB55 linesraw
1"""Rust WASM-bindgen rules for interfacing with aspect-build/rules_js"""
2
3load("@aspect_rules_js//js:providers.bzl", "js_info")
4
5# buildifier: disable=bzl-visibility
6load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo")
7load("//private:wasm_bindgen.bzl", "WASM_BINDGEN_ATTR", "rust_wasm_bindgen_action")
8
9def _js_rust_wasm_bindgen_impl(ctx):
10    toolchain = ctx.toolchains[Label("//:toolchain_type")]
11
12    info = rust_wasm_bindgen_action(
13        ctx = ctx,
14        toolchain = toolchain,
15        wasm_file = ctx.attr.wasm_file,
16        target_output = ctx.attr.target,
17        flags = ctx.attr.bindgen_flags,
18    )
19
20    providers = [
21        DefaultInfo(
22            files = depset([info.wasm], transitive = [info.js, info.ts]),
23        ),
24        info,
25        # Return a structure that is compatible with the deps[] of a ts_library.
26        js_info(
27            target = ctx.label,
28            sources = info.js,
29            transitive_sources = info.js,
30            types = info.ts,
31            transitive_types = info.ts,
32        ),
33    ]
34
35    if RustAnalyzerGroupInfo in ctx.attr.wasm_file:
36        providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo])
37
38    if RustAnalyzerInfo in ctx.attr.wasm_file:
39        providers.append(ctx.attr.wasm_file[RustAnalyzerInfo])
40
41    return providers
42
43js_rust_wasm_bindgen = rule(
44    doc = """\
45Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [aspect-build/rules_js][abjs].
46
47[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
48[abjs]: https://github.com/aspect-build/rules_js
49""",
50    implementation = _js_rust_wasm_bindgen_impl,
51    attrs = WASM_BINDGEN_ATTR,
52    toolchains = [
53        str(Label("//:toolchain_type")),
54    ],
55)