doido_cable_macros/lib.rs
1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{parse_macro_input, ItemStruct};
4
5/// Marks a struct as a WebSocket channel.
6///
7/// Generates a [`doido_cable::ChannelName`] impl carrying the channel's
8/// registration name — its struct name (`ChatChannel`), which ActionCable uses
9/// in the `identifier` to route messages. The struct's own `Channel` impl
10/// (lifecycle + `received`) is written by the user and re-emitted untouched.
11#[proc_macro_attribute]
12pub fn channel(_attr: TokenStream, item: TokenStream) -> TokenStream {
13 let input = parse_macro_input!(item as ItemStruct);
14 let ident = &input.ident;
15 let name = ident.to_string();
16
17 quote! {
18 #input
19
20 impl doido_cable::ChannelName for #ident {
21 fn channel_name() -> &'static str {
22 #name
23 }
24 }
25
26 impl #ident {
27 /// The channel's registration name (generated by `#[channel]`).
28 pub fn channel_name() -> &'static str {
29 #name
30 }
31
32 /// The default stream name for this channel (its registration name).
33 pub fn stream_name() -> &'static str {
34 #name
35 }
36 }
37 }
38 .into()
39}