pub struct Router { /* private fields */ }Expand description
Declarative command router for the WASM bridge.
Maps command names to handlers, with an optional fallback for unnamed or unrecognized commands.
ⓘ
use infinity_bridge_wasm::Router;
use serde_json::{json, Value};
use std::cell::RefCell;
let state = RefCell::new(MyState::new());
let router = Router::new()
.command("get_state", {
let state = state.clone();
move |_payload: &Value| {
let s = state.borrow();
Ok(json!({ "temp": s.temperature }))
}
})
.command("set_config", {
let state = state.clone();
move |payload: &Value| {
let mut s = state.borrow_mut();
s.apply_config(payload);
Ok(json!({"ok": true}))
}
})
.event("config_updated", {
let state = state.clone();
move |data: &Value| {
let mut s = state.borrow_mut();
s.apply_config(data);
}
})
.fallback(|name, payload| {
Err(format!("unknown command: {}", name.unwrap_or("<unnamed>")))
});Implementations§
Source§impl Router
impl Router
pub fn new() -> Self
pub fn command( self, name: &'static str, handler: impl Fn(&Value) -> Result<Value, String> + 'static, ) -> Self
pub fn event( self, name: &'static str, handler: impl Fn(&Value) + 'static, ) -> Self
pub fn fallback( self, handler: impl Fn(Option<&str>, &Value) -> Result<Value, String> + 'static, ) -> Self
Trait Implementations§
Source§impl BridgeHandler for Router
impl BridgeHandler for Router
Auto Trait Implementations§
impl Freeze for Router
impl !RefUnwindSafe for Router
impl !Send for Router
impl !Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
impl !UnwindSafe for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more