Skip to content

feat(ffi): Add webui_protocol_tokens FFI function to extract CSS token names#272

Merged
mohamedmansour merged 2 commits intomainfrom
user/akroshg/webui-ffi-protocol-tokens
Apr 26, 2026
Merged

feat(ffi): Add webui_protocol_tokens FFI function to extract CSS token names#272
mohamedmansour merged 2 commits intomainfrom
user/akroshg/webui-ffi-protocol-tokens

Conversation

@akroshg
Copy link
Copy Markdown
Contributor

@akroshg akroshg commented Apr 24, 2026

Adds webui_protocol_tokens — a new C-compatible FFI function that deserializes a WebUI protocol buffer and returns the CSS token name list as a
newline-delimited, heap-allocated string.

Motivation

Host languages (C#, Go, Python, C++) need to extract CSS design-token names from a compiled WebUI protocol without reaching into Rust internals. This
provides a safe, zero-dependency FFI entry point for that use case.

API contract

┌───────────────────────────────┬───────────────────────────────┬─────────────────────────────────┐
│ Input                         │ Return value                  │ webui_last_error                │
├───────────────────────────────┼───────────────────────────────┼─────────────────────────────────┤
│ Valid protocol with tokens    │ "token1\ntoken2\n…"           │ cleared                         │
├───────────────────────────────┼───────────────────────────────┼─────────────────────────────────┤
│ Valid protocol with no tokens │ "" (empty string, non-NULL)   │ cleared                         │
├───────────────────────────────┼───────────────────────────────┼─────────────────────────────────┤
│ Zero-length buffer (non-null) │ "" (empty protocol)           │ cleared                         │
├───────────────────────────────┼───────────────────────────────┼─────────────────────────────────┤
│ NULL pointer                  │ NULL                          │ "protocol_data is null"         │
├───────────────────────────────┼───────────────────────────────┼─────────────────────────────────┤
│ Invalid protobuf bytes        │ NULL                          │ "failed to parse protobuf: …"   │
└───────────────────────────────┴───────────────────────────────┴─────────────────────────────────┘

Caller must free the returned string with webui_free.

Changes

  • crates/webui-ffi/src/lib.rs — New webui_protocol_tokens function following existing FFI patterns (catch_unwind, null-check, thread-local error,
    CString ownership transfer).
  • crates/webui-ffi/include/webui_ffi.h — C header declaration with full # Safety documentation.
  • crates/webui-ffi/tests/ffi_test.rs — 7 integration tests:
  • Empty token list → ""
  • Single token
  • Multiple tokens (newline-delimited)
  • Order and duplicates preserved
  • Null pointer → NULL + error
  • Zero-length buffer → "" (valid empty protocol)
  • Invalid protobuf → NULL + error

Add a new C-compatible FFI function that deserializes a WebUI protocol
buffer and returns the CSS token name list as a newline-delimited string.

- Follows existing FFI patterns: catch_unwind, null-check, thread-local error
- Returns empty string for empty token lists (not NULL)
- Accepts zero-length buffers as valid empty protocol
- Includes 7 integration tests covering happy paths and error cases
- Header file updated with full safety documentation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
mohamedmansour
mohamedmansour previously approved these changes Apr 24, 2026
@mohamedmansour
Copy link
Copy Markdown
Contributor

mohamedmansour commented Apr 24, 2026

Don't we need to support the webui-node and webui-wasm as well to expose this? For it to be complete.

Expose the CSS token extraction API across all binding surfaces for
complete parity with the FFI function added earlier:

- webui-node: napi function returning Vec<String> (JS string array)
- webui-wasm: wasm_bindgen function returning JS array via serde

Includes tests for both crates covering empty, populated, order
preservation, and error cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@akroshg
Copy link
Copy Markdown
Contributor Author

akroshg commented Apr 24, 2026

Fixed with latest update
Added protocol_tokens to both bindings in the latest push:

  • webui-node: protocol_tokens(Buffer) → string[] via napi
  • webui-wasm: protocol_tokens(string) → string[] via wasm_bindgen

@mohamedmansour mohamedmansour merged commit efedc19 into main Apr 26, 2026
21 checks passed
@mohamedmansour mohamedmansour deleted the user/akroshg/webui-ffi-protocol-tokens branch April 26, 2026 07:43
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.

2 participants