Skip to main content

register_handlers

Macro register_handlers 

Source
macro_rules! register_handlers {
    ($router:expr, [ $($entry:tt)* ]) => { ... };
    (@entries $router:expr, ) => { ... };
    (@entries $router:expr, $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, args $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, streaming $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, streaming args $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, state $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, state args $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, state streaming $name:literal => $handler:path, $($rest:tt)*) => { ... };
    (@entries $router:expr, state streaming args $name:literal => $handler:path, $($rest:tt)*) => { ... };
}
Expand description

Bulk-register handler functions with a Router.

Each function is referenced by path in the expansion, so the compiler sees it as used — eliminating false dead_code warnings that occur when handler functions are only referenced through closure-based registration.

§Syntax

use allframe_core::register_handlers;

register_handlers!(router, [
    // Basic handler (zero-arg, returns String)
    "health" => health_check,

    // Handler with typed, deserializable args
    args "greet" => greet,

    // Streaming handler (receives StreamSender)
    streaming "feed" => live_feed,

    // Streaming handler with typed args
    streaming args "search" => search_stream,

    // State-only handler (receives State<Arc<S>>)
    state "get_status" => get_ollama_status,

    // State with typed args
    state args "save_key" => save_api_key,

    // State streaming (receives State<Arc<S>> + StreamSender)
    state streaming "stream_chat" => stream_chat,

    // State streaming with typed args
    state streaming args "start_job" => start_orchestrator,
]);

§Expands to

router.register("health", health_check);
router.register_with_args("greet", greet);
router.register_streaming("feed", live_feed);
router.register_streaming_with_args("search", search_stream);
router.register_with_state_only("get_status", get_ollama_status);
router.register_with_state("save_key", save_api_key);
router.register_streaming_with_state_only("stream_chat", stream_chat);
router.register_streaming_with_state("start_job", start_orchestrator);

§Why this helps

When handler functions are defined in separate modules and only called via router.register("name", || async { handler().await }), the compiler cannot always trace the usage chain and reports dead_code warnings. This macro passes each function directly as a function item, making the reference visible to rustc’s dead-code analysis.