Skip to main content

nest_rs_ws_macros/
lib.rs

1//! WebSocket gateway decorator macros. Generated code uses absolute paths so
2//! this crate does not depend on the surface crates.
3//!
4//! `#[subscribe_message("event")]`, `#[on_connect]`, `#[on_disconnect]` are
5//! inert attributes consumed by `#[messages]`, same shape as the HTTP verb
6//! attributes consumed by `#[routes]`.
7
8use proc_macro::TokenStream;
9
10mod attr;
11mod gateway;
12mod messages;
13
14/// `#[gateway(path = "/ws")]` — the `@WebSocketGateway` analog. Generates
15/// `from_container`, `pub const PATH`, and the inherent helpers `#[messages]`
16/// reads back.
17///
18/// `namespace = MarkerType` mounts against `WsServer<MarkerType>` — a
19/// self-provided isolated registry. Omitted, uses `Global` from `WsModule`.
20///
21/// `#[use_guards(...)]` on the struct = connection-level guards, run on the
22/// HTTP upgrade request so a rejected handshake never opens the socket. The
23/// `Discoverable` impl is emitted by `#[messages]` (it needs the message
24/// table).
25#[proc_macro_attribute]
26pub fn gateway(args: TokenStream, input: TokenStream) -> TokenStream {
27    gateway::gateway(args, input)
28}
29
30/// Bind a `#[gateway]` impl block's message handlers. Each
31/// `#[subscribe_message("event")]` method handles `{ "event": "...", "data":
32/// ... }`; the owned parameter is deserialized from `data`, the return value
33/// serialized back under the same event (`()` => no reply).
34///
35/// `#[use_guards(...)]` beside a handler binds per-message guards that the
36/// Layer System dedups against the global chain. `#[on_connect]` /
37/// `#[on_disconnect]` are the lifecycle-hook analogs — `&self` with an
38/// optional `&WsClient`.
39///
40/// Emits `Gateway` (dispatcher + hooks) and `Discoverable` — the latter
41/// attaches an `HttpEndpointMeta` so the gateway self-mounts on the HTTP
42/// transport at `PATH`.
43#[proc_macro_attribute]
44pub fn messages(args: TokenStream, input: TokenStream) -> TokenStream {
45    messages::messages(args, input)
46}