pub struct Router { /* private fields */ }Expand description
Named command registry with synchronous dispatch.
Implementations§
Source§impl Router
impl Router
Sourcepub fn register<F>(&self, name: impl Into<String>, handler: F)
pub fn register<F>(&self, name: impl Into<String>, handler: F)
Register a handler for a command name.
If a handler was already registered under name it is replaced.
Sourcepub fn register_simple<F>(&self, name: impl Into<String>, handler: F)
pub fn register_simple<F>(&self, name: impl Into<String>, handler: F)
Register a handler that takes no payload.
The incoming payload bytes are silently discarded.
Sourcepub fn register_json<F, A, R>(&self, name: impl Into<String>, handler: F)
pub fn register_json<F, A, R>(&self, name: impl Into<String>, handler: F)
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.
Sourcepub fn register_json_result<F, A, R, E>(
&self,
name: impl Into<String>,
handler: F,
)
pub fn register_json_result<F, A, R, E>( &self, name: impl Into<String>, handler: F, )
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.
Sourcepub fn register_binary<F, A, R>(&self, name: impl Into<String>, handler: F)
pub fn register_binary<F, A, R>(&self, name: impl Into<String>, handler: F)
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.
Sourcepub fn register_with_context<F>(&self, name: impl Into<String>, handler: F)
pub fn register_with_context<F>(&self, name: impl Into<String>, handler: F)
Register a context-aware handler.
Handlers generated by the #[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.
Sourcepub fn call_with_context(
&self,
name: &str,
payload: Vec<u8>,
ctx: &dyn Any,
) -> Result<Vec<u8>, Error>
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.
Sourcepub fn call_or_error_bytes_with_context(
&self,
name: &str,
payload: Vec<u8>,
ctx: &dyn Any,
) -> Vec<u8> ⓘ
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.
Sourcepub fn call(&self, name: &str, payload: Vec<u8>) -> Result<Vec<u8>, Error>
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.
Sourcepub fn call_or_error_bytes(&self, name: &str, payload: Vec<u8>) -> Vec<u8> ⓘ
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>.