|
| 1 | +# CLI Tool Example: When to Use `rust_wasm_component` vs `rust_wasm_component_bindgen` |
| 2 | + |
| 3 | +This example demonstrates the **key differences** between the two Rust WebAssembly component rules and when to use each one. |
| 4 | + |
| 5 | +## Rule Comparison |
| 6 | + |
| 7 | +### `rust_wasm_component` (Lower-Level Rule) |
| 8 | +**Use for**: CLI tools, utilities, WASI-only components |
| 9 | + |
| 10 | +```starlark |
| 11 | +rust_wasm_component( |
| 12 | + name = "file_processor_cli", |
| 13 | + srcs = ["src/cli_tool.rs"], |
| 14 | + deps = ["@crates//:clap", "@crates//:anyhow"], |
| 15 | + # No 'wit' attribute - uses WASI only |
| 16 | + rustc_flags = ["-C", "opt-level=3"], # Custom optimization |
| 17 | +) |
| 18 | +``` |
| 19 | + |
| 20 | +### `rust_wasm_component_bindgen` (High-Level Rule) |
| 21 | +**Use for**: Components with custom interfaces, inter-component communication |
| 22 | + |
| 23 | +```starlark |
| 24 | +rust_wasm_component_bindgen( |
| 25 | + name = "file_processor_component", |
| 26 | + srcs = ["src/component_lib.rs"], |
| 27 | + wit = ":processor_interfaces", # Custom WIT interfaces |
| 28 | + profiles = ["release"], # Simplified configuration |
| 29 | +) |
| 30 | +``` |
| 31 | + |
| 32 | +## When to Use Each |
| 33 | + |
| 34 | +### Use `rust_wasm_component` when: |
| 35 | + |
| 36 | +1. **CLI Tools & Utilities** |
| 37 | + - Command-line applications run by users |
| 38 | + - Tools that process files, manipulate data, etc. |
| 39 | + - Applications that use WASI but don't export custom functions |
| 40 | + |
| 41 | +2. **WASI-Only Components** |
| 42 | + - Components that only import WASI capabilities (filesystem, stdio, etc.) |
| 43 | + - No custom interfaces to export to other components |
| 44 | + - Simple input/output processing |
| 45 | + |
| 46 | +3. **Legacy WASM Module Conversion** |
| 47 | + - Converting existing `.wasm` modules to component format |
| 48 | + - Wrapping external tools for component compatibility |
| 49 | + - Migration of existing WebAssembly applications |
| 50 | + |
| 51 | +4. **Custom Build Requirements** |
| 52 | + - Need specific rustc flags or optimization settings |
| 53 | + - Complex compilation pipelines |
| 54 | + - Performance-critical components requiring fine-tuned compilation |
| 55 | + |
| 56 | +### Use `rust_wasm_component_bindgen` when: |
| 57 | + |
| 58 | +1. **Custom Component Interfaces** |
| 59 | + - Exporting functions for other components to call |
| 60 | + - Defining custom WIT interfaces and types |
| 61 | + - Building reusable component libraries |
| 62 | + |
| 63 | +2. **Inter-Component Communication** |
| 64 | + - Components designed to be composed with others |
| 65 | + - Microservices architectures using WAC |
| 66 | + - Plugin systems and modular applications |
| 67 | + |
| 68 | +3. **Standard Component Development** |
| 69 | + - Most typical component development workflows |
| 70 | + - Following component model best practices |
| 71 | + - Leveraging automatic binding generation |
| 72 | + |
| 73 | +4. **Simplified Development Experience** |
| 74 | + - Want automatic WIT binding generation |
| 75 | + - Prefer conventional configuration over custom flags |
| 76 | + - Building components for consumption by others |
| 77 | + |
| 78 | +## Examples in This Directory |
| 79 | + |
| 80 | +### CLI Tool (`file_processor_cli`) |
| 81 | +- **File**: `src/cli_tool.rs` |
| 82 | +- **Use case**: Command-line file processing utility |
| 83 | +- **Interfaces**: WASI only (filesystem, stdio) |
| 84 | +- **Usage**: `wasmtime run file_processor_cli.wasm -- upper -i input.txt -o output.txt` |
| 85 | + |
| 86 | +### Component Library (`file_processor_component`) |
| 87 | +- **File**: `src/component_lib.rs` + `wit/processor.wit` |
| 88 | +- **Use case**: Reusable file processing functions for other components |
| 89 | +- **Interfaces**: Custom WIT interfaces + WASI |
| 90 | +- **Usage**: Called by other components via WIT interfaces |
| 91 | + |
| 92 | +## Building and Testing |
| 93 | + |
| 94 | +```bash |
| 95 | +# Build both examples |
| 96 | +bazel build //examples/cli_tool_example:file_processor_cli |
| 97 | +bazel build //examples/cli_tool_example:file_processor_component |
| 98 | + |
| 99 | +# Compare component sizes and characteristics |
| 100 | +bazel build //examples/cli_tool_example:component_comparison |
| 101 | +cat bazel-bin/examples/cli_tool_example/comparison.txt |
| 102 | + |
| 103 | +# Test the CLI tool |
| 104 | +echo "hello world" > test.txt |
| 105 | +wasmtime run bazel-bin/examples/cli_tool_example/file_processor_cli.wasm -- upper -i test.txt -o upper.txt |
| 106 | +cat upper.txt # Should show "HELLO WORLD" |
| 107 | + |
| 108 | +# Inspect the component library |
| 109 | +wasm-tools component wit bazel-bin/examples/cli_tool_example/file_processor_component.wasm |
| 110 | +``` |
| 111 | + |
| 112 | +## Key Takeaways |
| 113 | + |
| 114 | +- **`rust_wasm_component`**: Lower-level, more control, CLI tools, WASI-only |
| 115 | +- **`rust_wasm_component_bindgen`**: Higher-level, automatic bindings, custom interfaces |
| 116 | +- **Choose based on use case**: CLI utilities vs component libraries |
| 117 | +- **Both are valid**: Different tools for different jobs in the WebAssembly ecosystem |
| 118 | + |
| 119 | +Most developers should start with `rust_wasm_component_bindgen` for typical component development, and use `rust_wasm_component` when they need the specific capabilities it provides. |
0 commit comments