doido-cable-macros 0.0.10

Proc-macros declaring WebSocket channel handlers and broadcast wiring for Doido Cable.
Documentation
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, ItemStruct};

/// Marks a struct as a WebSocket channel.
///
/// Generates a [`doido_cable::ChannelName`] impl carrying the channel's
/// registration name — its struct name (`ChatChannel`), which ActionCable uses
/// in the `identifier` to route messages. The struct's own `Channel` impl
/// (lifecycle + `received`) is written by the user and re-emitted untouched.
#[proc_macro_attribute]
pub fn channel(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let input = parse_macro_input!(item as ItemStruct);
    let ident = &input.ident;
    let name = ident.to_string();

    quote! {
        #input

        impl doido_cable::ChannelName for #ident {
            fn channel_name() -> &'static str {
                #name
            }
        }

        impl #ident {
            /// The channel's registration name (generated by `#[channel]`).
            pub fn channel_name() -> &'static str {
                #name
            }

            /// The default stream name for this channel (its registration name).
            pub fn stream_name() -> &'static str {
                #name
            }
        }
    }
    .into()
}