Skip to main content

Router

Struct Router 

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

Named command registry with synchronous dispatch.

Implementations§

Source§

impl Router

Source

pub fn new() -> Self

Create an empty dispatch table.

Source

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

Register a handler for a command name.

If a handler was already registered under name it is replaced.

Source

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

Register a handler that takes no payload.

The incoming payload bytes are silently discarded.

Source

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

Register a JSON handler for a command name.

The incoming payload is deserialised from JSON into A, the handler is called with the typed value, and the return value R is serialised back to JSON bytes. Returns Error::Serialize on deserialisation failure.

Source

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

Register a fallible JSON handler for a command name.

Like register_json, but the handler returns Result<R, E>. On Ok(value), the value is serialised to JSON. On Err(e), the error’s Display text is returned as Error::Handler.

Source

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

Register a binary handler for a command name.

The incoming payload is decoded via the Decode trait into A, the handler is called with the typed value, and the return value R is encoded via Encode back to bytes. Returns Error::DecodeFailed if the payload cannot be decoded.

Source

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

Register a context-aware handler.

Handlers generated by the #[tauri_conduit::command] macro have the signature fn(Vec<u8>, &dyn Any) -> Result<Vec<u8>, Error> and handle their own deserialization, State extraction, and serialization internally.

Source

pub fn call_with_context( &self, name: &str, payload: Vec<u8>, ctx: &dyn Any, ) -> Result<Vec<u8>, Error>

Dispatch a command by name with an opaque context.

The context is passed through to the handler. For handlers registered via register_with_context (i.e., #[command]-generated handlers), the context is typically an &AppHandle<Wry> that enables State<T> extraction.

Source

pub fn call_or_error_bytes_with_context( &self, name: &str, payload: Vec<u8>, ctx: &dyn Any, ) -> Vec<u8>

Dispatch a command by name with context, returning raw bytes in all cases.

On success the handler’s response bytes are returned. On failure the error’s Display text is returned as UTF-8 bytes.

Source

pub fn call(&self, name: &str, payload: Vec<u8>) -> Result<Vec<u8>, Error>

Dispatch a command by name.

Returns the handler’s response bytes on success, or Error::UnknownCommand if no handler is registered for name.

Source

pub fn call_or_error_bytes(&self, name: &str, payload: Vec<u8>) -> Vec<u8>

Dispatch a command by name, returning raw bytes in all cases.

On success the handler’s response bytes are returned. On failure the error’s Display text is returned as UTF-8 bytes. This is a convenience wrapper for call sites (such as the custom protocol handler) that must always produce a Vec<u8>.

Source

pub fn has(&self, name: &str) -> bool

Check whether a command is registered.

Trait Implementations§

Source§

impl Debug for Router

Source§

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

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

impl Default for Router

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.