Skip to main content

PluginBuilder

Struct PluginBuilder 

Source
pub struct PluginBuilder { /* private fields */ }
Expand description

Builder for the conduit Tauri v2 plugin.

Collects command registrations and configuration, then produces a TauriPlugin via build.

Implementations§

Source§

impl PluginBuilder

Source

pub fn new() -> Self

Create a new, empty plugin builder.

Source

pub fn command<F>(self, name: impl Into<String>, handler: F) -> Self
where F: Fn(Vec<u8>) -> Vec<u8> + Send + Sync + 'static,

Register a raw command handler (Vec<u8> in, Vec<u8> out).

Command names correspond to the path segment in the conduit://localhost/invoke/<cmd_name> URL.

Source

pub fn handler( self, name: impl Into<String>, handler: impl ConduitHandler, ) -> Self

Register a #[tauri_conduit::command]-generated handler.

Works with both sync and async handlers. Sync handlers are dispatched inline. Async handlers are spawned on the tokio runtime — truly async, exactly like #[tauri::command].

use tauri_conduit::{command, handler};

#[command]
fn greet(name: String) -> String {
    format!("Hello, {name}!")
}

#[command]
async fn fetch_user(state: State<'_, Db>, id: u64) -> Result<User, String> {
    state.get_user(id).await.map_err(|e| e.to_string())
}

tauri_plugin_conduit::init()
    .handler("greet", handler!(greet))
    .handler("fetch_user", handler!(fetch_user))
    .build()
Source

pub fn handler_raw<F>(self, name: impl Into<String>, handler: F) -> Self
where F: Fn(Vec<u8>, &dyn Any) -> Result<Vec<u8>, Error> + Send + Sync + 'static,

Register a raw closure handler (legacy API).

Accepts the same closure signature as the pre-ConduitHandler .handler(): Fn(Vec<u8>, &dyn Any) -> Result<Vec<u8>, Error>. This is a synchronous handler dispatched via Router::register_with_context.

Use this for backward compatibility when migrating from closure-based registration. For new code, prefer handler with #[tauri_conduit::command] + handler!().

Source

pub fn command_json<F, A, R>(self, name: impl Into<String>, handler: F) -> Self
where F: Fn(A) -> R + Send + Sync + 'static, A: DeserializeOwned + 'static, R: Serialize + 'static,

Typed JSON handler. Deserializes the request payload as A and serializes the response as R.

Unlike Tauri’s #[tauri::command], this takes a single argument type (not named parameters) and does not support async or State injection.

.command_json("greet", |name: String| format!("Hello, {name}!"))
Source

pub fn command_json_result<F, A, R, E>( self, name: impl Into<String>, handler: F, ) -> Self
where F: Fn(A) -> Result<R, E> + Send + Sync + 'static, A: DeserializeOwned + 'static, R: Serialize + 'static, E: Display + 'static,

Typed JSON handler that returns Result<R, E>.

Like command_json, but the handler returns Result<R, E> where E: Display. On success, R is serialized to JSON. On error, the error’s Display text is returned to the caller.

For Tauri-style named parameters with Result returns, prefer handler with #[tauri_conduit::command] instead:

use tauri_conduit::command;

#[command]
fn divide(a: f64, b: f64) -> Result<f64, String> {
    if b == 0.0 { Err("division by zero".into()) }
    else { Ok(a / b) }
}

// Preferred:
.handler("divide", divide)
Source

pub fn command_binary<F, A, Ret>( self, name: impl Into<String>, handler: F, ) -> Self
where F: Fn(A) -> Ret + Send + Sync + 'static, A: Decode + 'static, Ret: Encode + 'static,

Register a typed binary command handler.

The request payload is decoded via the Decode trait and the response is encoded via Encode. No JSON involved — raw bytes in, raw bytes out.

.command_binary("process", |tick: MarketTick| tick)
Source

pub fn channel(self, name: impl Into<String>) -> Self

Register a lossy channel with the default capacity (64 KB).

Oldest frames are silently dropped when the buffer is full. Best for telemetry, game state, and real-time data where freshness matters more than completeness.

§Panics

Panics if the name is empty, contains characters outside [a-zA-Z0-9_-], or duplicates an already-registered channel name.

Source

pub fn channel_with_capacity( self, name: impl Into<String>, capacity: usize, ) -> Self

Register a lossy channel with a custom byte capacity.

§Panics

Panics if the name is empty, contains characters outside [a-zA-Z0-9_-], or duplicates an already-registered channel name.

Source

pub fn channel_ordered(self, name: impl Into<String>) -> Self

Register an ordered channel with the default capacity (64 KB).

No frames are ever dropped. When the buffer is full, PluginState::push returns an error (backpressure). Best for transaction logs, control messages, and any data that must arrive intact and in order.

§Panics

Panics if the name is empty, contains characters outside [a-zA-Z0-9_-], or duplicates an already-registered channel name.

Source

pub fn channel_ordered_with_capacity( self, name: impl Into<String>, max_bytes: usize, ) -> Self

Register an ordered channel with a custom byte limit.

A max_bytes of 0 means unbounded — the buffer grows without limit.

§Panics

Panics if the name is empty, contains characters outside [a-zA-Z0-9_-], or duplicates an already-registered channel name.

Source

pub fn build<R: Runtime>(self) -> TauriPlugin<R>

Build the Tauri v2 plugin.

This consumes the builder and returns a TauriPlugin that can be passed to tauri::Builder::plugin.

§Dispatch model

Commands are dispatched through a two-tier system:

  1. #[command] handlers (registered via .handler()) are checked first. These support named parameters, State<T> injection, Result returns, and async — full parity with #[tauri::command].

  2. Raw Router handlers (registered via .command(), .command_json(), .command_binary()) are the fallback. These are simpler Vec<u8> -> Vec<u8> functions with no injection or async support.

If a command name exists in both tiers, the #[command] handler takes priority and a debug warning is printed.

Trait Implementations§

Source§

impl Debug for PluginBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PluginBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.