Skip to main content

beetry_plugin/
channel_macro.rs

1/// Creates a channel plugin for a message type.
2///
3/// The `channel!` macro creates a plugin that:
4///
5/// - exposes a `ChannelSpec` for the editor and persistence layer
6/// - provides a channel factory for reconstruction
7/// - registers itself through the plugin inventory
8/// - makes the message type available in the editor's Channels section
9///
10/// Example:
11///
12/// ```no_run
13/// # use beetry_macros::Message;
14/// # use beetry_message::Message;
15/// # use beetry_message::type_hash::{self, TypeHash};
16/// #[derive(Debug, Clone, Copy, Default, TypeHash, Message)]
17/// pub struct VehicleState {
18///     pub ready: bool,
19///     pub parked: bool,
20///     pub speed_mps: f32,
21/// }
22///
23/// beetry_plugin::channel! {VehicleStateChannel: VehicleState}
24/// ```
25#[macro_export]
26macro_rules! channel {
27    ($plugin_id:ident : $msg_ty:ty) => {
28        pub struct $plugin_id {
29            spec: $crate::__macro_support::ChannelSpec,
30            factory: $crate::Factory,
31        }
32
33        impl $crate::Plugin for $plugin_id {
34            type Spec = $crate::__macro_support::ChannelSpec;
35            type Factory = $crate::Factory;
36
37            fn new() -> Self
38            where
39                Self: Sized,
40            {
41                Self {
42                    spec: $crate::__macro_support::ChannelSpec::new::<$msg_ty>(),
43                    factory: $crate::Factory::from_msg::<$msg_ty>(),
44                }
45            }
46
47            fn spec(&self) -> &Self::Spec {
48                &self.spec
49            }
50
51            fn factory(&self) -> &Self::Factory {
52                &self.factory
53            }
54
55            fn into_parts(self: Box<Self>) -> (Self::Spec, Self::Factory) {
56                (self.spec, self.factory)
57            }
58        }
59
60        $crate::submit!($crate::ChannelPluginConstructor::new::<$plugin_id>());
61    };
62}