• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Typesafe language bindings generator for the Tauri IPC bridge

tauri-bindgen

Typesafe language bindings generator for the Tauri IPC bridge

Online playground

MIT or Apache 2.0 licensed

Note: This project is still under heavy development and may not support all features yet. Feel free to try it out though, create issues, and open PRs!

About

Generate type-safe bindings for Tauri's IPC bridge. Currently supports Rust Host and Webview frontends (called Guests) in JavaScript, TypeScript, and Rust. Bindings are declared using *.wit files that describe exposed functions and shared types.

Rationale

Under the current IPC system, Tauri can make no compile-time guarantees on the correctness of your invoke calls and any mistakes will result in nasty runtime errors. For example you might have misspelled a command or parameter name which you will only notice when actually running the app!

tauri-bindgen will generate traits and invoke-handlers for the Host and Guest bindings for JavaScript, TypeScript, Rust and ReScript from a single, shared source of truth using the *.wit format as a Interface Definition Language (IDL). The generated bindings automatically take care of the heavy lifting, such as correctly calling the invoke function and serializing/deserializing parameters and results.

Here are a few reasons why that is cool:

  • Compile-time Checks

When using strongly typed languages, such as Rust, TypeScript or ReScript the generated code will automatically ensure that you are calling the API correctly, as long as it passes the type checking your golden. This is especially neat when working in a team, so your colleagues can't just change command signatures and pull the rug out from under you.

  • Easily auditable

Sometimes in bigger apps it's easy to loose track of all the commands your backend exposes, there might be an old version left somewhere that still get's exposed to the frontend. This is of course a big security risk. Having all possible commands be declared in a single place makes them easily auditable for you and possibly external security auditing professionals.

  • Automatic documentation

tauri-bindgen can also generate easy to read markdown and html descriptions of your interfaces for documentation purposes.

  • Future proof

We're planning a big rework of the IPC bridge in the coming months and tauri-bindgen has been designed with some of the early design-work in mind. A big rework of course means breaking changes, but using tauri bindgen isolates from these changes, just update Tauri, update tauri-bindgen, and re-generate your bindings and your code will continue to work!

Terminology

Host

Host refers to the Tauri rust core, so the place where your commands are implemented.

Guest

Guest refers to the code running in the Webview, this will usually be written in JavaScript/TypeScript, but can also be written in any other language that runs on the Web (through WASM) like Rust, C, or Swift.

Supported Bindings

  • Host - Generates a trait declaration and an invoke_handler from the interface. This generator can also be used through the tauri-bindgen-host crate (located at crates/host) and, has a generate! macro for generating code.
  • Guest JavaScript - Generates a module exposing all functions, functions internally know how to serialize and invoke their host counterpart.
  • Guest Typescript - The same as the JavaScript guest, but generates Typescript files.
  • Guest Rust - Generates bindings using wasm_bindgen that can be used in Rust compile-to-wasm frontend frameworks such as sycamore. You probably want to depend on the tauri-bindgen-guest-rust crate (located at crates/guest-rust) and use the generate! macro to generate code.
  • Guest ReScript - Generates bindings for the ReScript language.
  • Markdown - Generates a markdown description of the interface for documentation purposes.

Example

Declare your interface in a *.wit file:

interface greet {
  func greet(name: string) -> string
}

then you can generate the host bindings:

use tauri_bindgen_host::ipc_router_wip::{BuilderExt, Caller, Router};

tauri_bindgen_host::generate!({
    path: "../greet.wit",
    async: false,
    tracing: true
});

#[derive(Clone, Copy)]
struct GreetCtx;

impl greet::Greet for GreetCtx {
    fn greet(&self, name: String) -> String {
        format!(
            "Hello, {}! You've been greeted from code-generated Rust!",
            name
        )
    }
}

fn main() {
    let mut router: Router<GreetCtx> = Router::new(GreetCtx {});

    greet::add_to_router(&mut router, |ctx| ctx).unwrap();

    tauri::Builder::default()
        .ipc_router(router)
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

and lastly generate client bindings, this can be done for JavaScript, Typescript or ReScript using the following commands:

tauri-bindgen guest javascript greet.wit --prettier // we can run prettier on the generated code. requires a global prettier install
tauri-bindgen guest typescript greet.wit --prettier
tauri-bindgen guest rescript greet.wit --fmt // run `rescript format` on the generated code. requires a global rescript install

or for rust using the provided generate! macro:

tauri_bindgen_guest_rust::generate!({
    path: "greet.wit"
});

// now we can call greet
let greeting = greet::greet("Jonas").await;

see also the example.

Contributing

PRs are welcome!

Credits

This project has been inspired by and based off of the awesome wit-bindgen tool for WebAssembly by the Bytecode Alliance.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
Rust
81,362
star
2

awesome-tauri

๐Ÿš€ Awesome Tauri Apps, Plugins and Resources
4,224
star
3

wry

Cross-platform WebView library in Rust for Tauri.
Rust
3,491
star
4

cargo-mobile2

Rust on mobile made easy!
Rust
1,566
star
5

tao

The TAO of cross-platform windowing. A library in Rust built for Tauri.
Rust
1,532
star
6

create-tauri-app

Rapidly scaffold out a new tauri app project.
Rust
979
star
7

tauri-action

Build your Web application as a Tauri binary for macOS, Linux and Windows
TypeScript
870
star
8

plugins-workspace

All of the official Tauri plugins in one place!
Rust
807
star
9

tauri-docs

The source for all Tauri project documentation.
MDX
769
star
10

window-vibrancy

Make your windows vibrant.
Rust
609
star
11

tauri-vscode

Visual Studio Code Extension for Tauri apps development
TypeScript
459
star
12

vue-cli-plugin-tauri

Turn your Vue SPA into a lightweight cross-platform desktop app
JavaScript
372
star
13

tauri-plugin-sql

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
365
star
14

tauri-egui

Rust
343
star
15

tauri-plugin-store

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
275
star
16

muda

Menu Utilities for Desktop Applications in Rust.
Rust
234
star
17

tray-icon

Tray icons for Desktop Applications.
Rust
199
star
18

window-shadows

Add native shadows to your windows.
Rust
160
star
19

webkit2gtk-rs

WebKit2 bindings and wrappers for Rust
Rust
136
star
20

global-hotkey

Global hotkeys for Desktop Applications.
Rust
127
star
21

tauri-plugin-window-state

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
125
star
22

tauri-plugin-log

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
111
star
23

javascriptcore-rs

JavaScriptCore bindings and wrappers for Rust
Rust
91
star
24

tauri-plugin-positioner

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
91
star
25

meilisearch-docsearch

A quick search component for meilisearch, inspired by algolia/docsearch.
TypeScript
88
star
26

tauri-plugin-stronghold

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
88
star
27

tauri-plugin-authenticator

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
86
star
28

smoke-tests

A collection of frameworks used as a suite of smoke-tests for tauri
JavaScript
76
star
29

tauri-plugin-autostart

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
63
star
30

tauri-plugin-fs-watch

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
62
star
31

tauri-plugin-websocket

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
58
star
32

tauri-plugin-localhost

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
56
star
33

tauri-invoke-http

A custom invoke system for Tauri that leverages a localhost server
Rust
47
star
34

fix-path-env-rs

Rust
44
star
35

libappindicator-rs

Rust safe bindings for the libappindicator library
Rust
44
star
36

tauri-plugin-fs-extra

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
TypeScript
43
star
37

tauri-theia

Tauri Flavor of Theia
Rust
39
star
38

tauricon

Make icons for your tauri app with nodejs
TypeScript
30
star
39

tauri-plugin-upload

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
29
star
40

tauri-plugin-single-instance

[DEPRECATED] Please use the plugin from https://github.com/tauri-apps/plugins-workspace instead.
Rust
25
star
41

win7-notifications

Send Windows 10 styled notifications on Windows 7.
Rust
19
star
42

tauri-forage

Currified localForage with a side of extras.
TypeScript
18
star
43

tauri-hotkey-rs

Rust
17
star
44

rfcs

Request For Comments for the Tauri project
17
star
45

tauri-discord-bot

Tauri's Discord Bot
TypeScript
17
star
46

nsis-tauri-utils

A collection of NSIS plugins written in rust.
Rust
16
star
47

tauri-plugin-notification

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Kotlin
12
star
48

benchmark_results

11
star
49

tauri-github-bot

A GitHub bot for tauri-apps org to automate various tasks and intended to used only by tauri-apps org members.
TypeScript
10
star
50

gipfs

Git ops with IPFS
9
star
51

binary-releases

Prebuilt binaries for Tauri projects
9
star
52

governance-and-guidance

Rust
8
star
53

tauri-plugin-http

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
8
star
54

.github

Holds org-wide configuration files.
7
star
55

meetings

7
star
56

tauri-plugin-updater

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
7
star
57

tauri-search

[Archived] Tauri's Search Capabilities for Website leveraging MeiliSearch
TypeScript
6
star
58

tauri-plugin-shell

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
6
star
59

realworld

Realworld apps made with Tauri: Proof of Agnosis.
JavaScript
6
star
60

tauri.studio

Source code and build for the front-facing project website.
Vue
6
star
61

tauri-plugin-clipboard-manager

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
6
star
62

wry-mobile

Rust
6
star
63

tauri-plugin-fs

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
6
star
64

tauri-includedir

Rust
5
star
65

rustdocusaurus

POCโ€”turning rustdoc generated docs into Docusaurus (MD + sidebar)
JavaScript
5
star
66

docusaurus-meilisearch-indexer

JavaScript
5
star
67

tauri-plugin-os

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
JavaScript
5
star
68

tauri-plugin-persisted-scope

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
5
star
69

tauri-plugin-process

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
JavaScript
5
star
70

tauri-plugin-global-shortcut

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
5
star
71

dns-automation

This repo will manage DNS records automagically. Implementing Infrastructure as Code & GitOps practices.
HCL
4
star
72

tauri-plugin-dialog

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
4
star
73

tauri-plugin-cli

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
Rust
4
star
74

tauri-webpack

[Deprecated] webpack for no server
JavaScript
3
star
75

typedocusaurus

TypeScript
3
star
76

soup2-rs

Rust
2
star
77

benchmark_electron

Rust
2
star
78

automation

GitHub Actions available to the tauri-apps organization
JavaScript
2
star
79

gir-files

Shell
2
star
80

workflow-testbed

Test bed that will soon be deleted.
2
star
81

tauri-inliner-rs

Rust
1
star
82

tauri-plugin-window

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
TypeScript
1
star
83

msedgedriver-manifest-cache

A caching service (in the form of a static site) of the msedgedriver version manifest file that seems to go missing a lot.
Rust
1
star
84

tauri-dialog-rs

C
1
star
85

board-voter

Vue
1
star
86

tauri-plugin-app

[READ ONLY] This repository is a mirror, for issue tracking and development head to: https://github.com/tauri-apps/plugins-workspace
JavaScript
1
star