Skip to main content

Module macros

Module macros 

Source
Expand description

The [dispatch!] macro for pattern-matching over updates.

Instead of writing giant match blocks, dispatch! lets you register named handlers with optional guard clauses:

use layer_client::{Client, dispatch};
use layer_client::update::Update;

dispatch!(client, update,
NewMessage(msg) if !msg.outgoing() => {
    println!("Got: {:?}", msg.text());
},
MessageEdited(msg) => {
    println!("Edited: {:?}", msg.text());
},
CallbackQuery(cb) => {
    client.answer_callback_query(cb.query_id, Some("✓"), false).await?;
},
_ => {} // catch-all for unhandled variants
);

Each arm is VariantName(binding) [if guard] => { body }. The macro expands to a plain match statement: zero overhead.