summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_api/src/message.rs
blob: bbcda85725b353cc1f3e93ec74f18aee3146fdd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use bevy_reflect::{TypeUuid, TypeUuidDynamic};
use downcast_rs::{impl_downcast, DowncastSync};
use serde::Serialize;

use crate::address::AnyMessageBox;

/// An object that can be sent between [`Plugin`]s
///
/// This trait is a marker trait for all types that can be used as messages which can be sent
/// between plugins in thin-edge.
pub trait Message:
    Send + std::fmt::Debug + DynMessage + DowncastSync + TypeUuidDynamic + 'static
{
}

impl_downcast!(sync Message);

/// A bag of messages making it easier to work with dynamic messages
pub trait DynMessage {
    /// Get the type name of this message
    fn type_name(&self) -> &'static str {
        std::any::type_name::<Self>()
    }
}

impl<M: 'static> DynMessage for M {}

/// Register that the [`Message`] can receive replies of kind `R`: [`Message`]
pub trait AcceptsReplies: Message {
    /// The reply type that can be sent to implementing messages as replies
    type Reply: Message;
}

/// A message that can contain any other message
///
/// This is solely used in conjunction with [`AnyMessages`](crate::plugin::AnyMessages) and should not generally be used
/// otherwise.
///
/// To construct it, you will need to have a message and call [`AnyMessage::from_message`]
#[derive(Debug, TypeUuid)]
#[uuid = "e7e5c87b-2022-4687-8650-DEADBEEEEEEF"]
pub struct AnyMessage(pub(crate) AnyMessageBox);

impl std::ops::Deref for AnyMessage {
    type Target = dyn Message;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

impl AnyMessage {
    /// Construct a new [`AnyMessage`] from a message
    pub fn from_message<M: Message>(m: M) -> Self {
        AnyMessage(Box::new(m))
    }

    /// Try to downcast this message to a specific message
    pub fn downcast<M: Message>(self) -> Result<M, Self> {
        Ok(*self.0.downcast().map_err(AnyMessage)?)
    }

    /// Take out the raw boxed message
    ///
    /// Note
    ///
    /// This is an advanced API and should only be used if needed.
    /// Prefer using `AnyMessage::downcast` if possible
    pub fn into_raw(self) -> AnyMessageBox {
        self.0
    }
}

impl Message for AnyMessage {}

/// The type of a message as used by `tedge_api` to represent a type
#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash)]
pub struct MessageType {
    name: &'static str,
    kind: MessageKind,
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
struct Uuid(bevy_reflect::Uuid);

#[derive(Debug, Clone, Serialize, Eq, PartialEq, Hash)]
enum MessageKind {
    Wildcard,
    #[serde(skip)]
    Typed(Uuid),
}

impl MessageType {
    /// Does this [`MessageType`] satisfy another [`MessageType`]
    ///
    /// ## Note
    /// A message type from [`AnyMessage`] acts as a 'wildcard', being satisfied by any other type
    /// (even itself).
    /// The reverse is not true, a specific type cannot be satisfied by a 'wildcard' (i.e.
    /// [`AnyMessage`]).
    ///
    /// [`MessageType::satisfy`] is thus reflexive but not symmetric nor transitive, meaning that it cannot be
    /// used for `PartialEq`.
    #[must_use]
    pub fn satisfy(&self, other: &Self) -> bool {
        match (&self.kind, &other.kind) {
            (MessageKind::Wildcard, _) => true,
            (_, MessageKind::Wildcard) => false,
            (MessageKind::Typed(ty_l), MessageKind::Typed(ty_r)) => ty_l.eq(ty_r),
        }
    }

    /// Get the [`MessageType`] for a `M`:[`Message`]
    #[must_use]
    pub fn for_message<M: Message + TypeUuid>() -> Self {
        let id = M::TYPE_UUID;
        MessageType {
            name: std::any::type_name::<M>(),
            kind: if id == AnyMessage::TYPE_UUID {
                MessageKind::Wildcard
            } else {
                MessageKind::Typed(Uuid(id))
            },
        }
    }

    pub(crate) fn from_message(msg: &dyn Message) -> Self {
        let id = msg.type_uuid();
        MessageType {
            name: TypeUuidDynamic::type_name(msg),
            kind: if id == AnyMessage::TYPE_UUID {
                MessageKind::Wildcard
            } else {
                MessageKind::Typed(Uuid(id))
            },
        }
    }

    /// Get the type's name
    #[must_use]
    pub fn name(&self) -> &'static str {
        self.name
    }
}

/// A message to tell the core to stop thin-edge
#[derive(Debug, TypeUuid)]
#[uuid = "812b7235-671f-4722-b01a-333578b2a4ea"]
pub struct StopCore;

impl Message for StopCore {}

crate::make_receiver_bundle!(pub struct CoreMessages(StopCore));

#[cfg(test)]
mod tests {
    use bevy_reflect::TypeUuid;

    use crate::Message;

    use super::{AnyMessage, MessageType};

    #[derive(Debug, TypeUuid)]
    #[uuid = "0c2ed228-5ff0-4ba5-a0d3-3648f7eb6558"]
    struct Bar;

    impl Message for Bar {}

    #[derive(Debug, TypeUuid)]
    #[uuid = "8c3beacb-327d-422d-a914-47006d521ba5"]
    struct Foo;

    impl Message for Foo {}

    #[test]
    fn assert_satisfy_laws_for_types() {
        let bar_type = MessageType::for_message::<Bar>();
        let any_message_type = MessageType::for_message::<AnyMessage>();
        let foo_type = MessageType::for_message::<Foo>();

        assert!(any_message_type.satisfy(&bar_type));
        assert!(any_message_type.satisfy(&foo_type));

        assert!(!bar_type.satisfy(&any_message_type));
        assert!(!bar_type.satisfy(&foo_type));
    }
}