macros.rs

  1#[macro_export]
  2macro_rules! messages {
  3    ($(($name:ident, $priority:ident)),* $(,)?) => {
  4        pub fn build_typed_envelope(sender_id: PeerId, received_at: std::time::Instant, 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,
 10                        message_id: envelope.id,
 11                        payload,
 12                        received_at,
 13                    }))
 14                }, )*
 15                _ => None
 16            }
 17        }
 18
 19        $(
 20            impl EnvelopedMessage for $name {
 21                const NAME: &'static str = std::stringify!($name);
 22                const PRIORITY: MessagePriority = MessagePriority::$priority;
 23
 24                fn into_envelope(
 25                    self,
 26                    id: u32,
 27                    responding_to: Option<u32>,
 28                    original_sender_id: Option<PeerId>,
 29                ) -> Envelope {
 30                    Envelope {
 31                        id,
 32                        responding_to,
 33                        original_sender_id,
 34                        payload: Some(envelope::Payload::$name(self)),
 35                        ack_id: None,
 36                    }
 37                }
 38
 39                fn from_envelope(envelope: Envelope) -> Option<Self> {
 40                    if let Some(envelope::Payload::$name(msg)) = envelope.payload {
 41                        Some(msg)
 42                    } else {
 43                        None
 44                    }
 45                }
 46            }
 47        )*
 48    };
 49}
 50
 51#[macro_export]
 52macro_rules! request_messages {
 53    ($(($request_name:ident, $response_name:ident)),* $(,)?) => {
 54        $(impl RequestMessage for $request_name {
 55            type Response = $response_name;
 56        })*
 57    };
 58}
 59
 60#[macro_export]
 61macro_rules! entity_messages {
 62    ({$id_field:ident, $entity_type:ty}, $($name:ident),* $(,)?) => {
 63        $(impl EntityMessage for $name {
 64            type Entity = $entity_type;
 65
 66            fn remote_entity_id(&self) -> u64 {
 67                self.$id_field
 68            }
 69        })*
 70    };
 71}
 72
 73#[macro_export]
 74macro_rules! lsp_messages {
 75    ($(($request_name:ident, $response_name:ident, $stop_previous_requests:expr)),* $(,)?) => {
 76        $(impl LspRequestMessage for $request_name {
 77            type Response = $response_name;
 78
 79            fn to_proto_query(self) -> $crate::lsp_query::Request {
 80                $crate::lsp_query::Request::$request_name(self)
 81            }
 82
 83            fn response_to_proto_query(response: Self::Response) -> $crate::lsp_response::Response {
 84                $crate::lsp_response::Response::$response_name(response)
 85            }
 86
 87            fn buffer_id(&self) -> u64 {
 88                self.buffer_id
 89            }
 90
 91            fn buffer_version(&self) -> &[$crate::VectorClockEntry] {
 92                &self.version
 93            }
 94
 95            fn stop_previous_requests() -> bool {
 96                $stop_previous_requests
 97            }
 98        })*
 99    };
100}