1#[macro_export]
2macro_rules! messages {
3 ($(($name:ident, $priority:ident)),* $(,)?) => {
4 pub fn build_typed_envelope(sender_id: ConnectionId, envelope: Envelope) -> Option<Box<dyn AnyTypedEnvelope>> {
5 match envelope.payload {
6 $(Some(envelope::Payload::$name(payload)) => {
7 Some(Box::new(TypedEnvelope {
8 sender_id,
9 original_sender_id: envelope.original_sender_id.map(PeerId),
10 message_id: envelope.id,
11 payload,
12 }))
13 }, )*
14 _ => None
15 }
16 }
17
18 $(
19 impl EnvelopedMessage for $name {
20 const NAME: &'static str = std::stringify!($name);
21 const PRIORITY: MessagePriority = MessagePriority::$priority;
22
23 fn into_envelope(
24 self,
25 id: u32,
26 responding_to: Option<u32>,
27 original_sender_id: Option<u32>,
28 ) -> Envelope {
29 Envelope {
30 id,
31 responding_to,
32 original_sender_id,
33 payload: Some(envelope::Payload::$name(self)),
34 }
35 }
36
37 fn from_envelope(envelope: Envelope) -> Option<Self> {
38 if let Some(envelope::Payload::$name(msg)) = envelope.payload {
39 Some(msg)
40 } else {
41 None
42 }
43 }
44 }
45 )*
46 };
47}
48
49#[macro_export]
50macro_rules! request_messages {
51 ($(($request_name:ident, $response_name:ident)),* $(,)?) => {
52 $(impl RequestMessage for $request_name {
53 type Response = $response_name;
54 })*
55 };
56}
57
58#[macro_export]
59macro_rules! entity_messages {
60 ($id_field:ident, $($name:ident),* $(,)?) => {
61 $(impl EntityMessage for $name {
62 fn remote_entity_id(&self) -> u64 {
63 self.$id_field
64 }
65 })*
66 };
67}