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
use crate::ClientId;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Represents the recipient(s) of a message.
///
/// Messages may either be sent to a particular client by numeric id
/// (`MessageRecipient::Client(3)`), or be broadcast to all connected clients
/// (`MessageRecipient::Broadcast`).]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

pub enum MessageRecipient {
    Broadcast,
    Client(ClientId),
}

impl MessageRecipient {
    #[must_use]
    pub fn encode_u32(&self) -> u32 {
        match self {
            Self::Broadcast => 0,
            Self::Client(c) => (*c).into(),
        }
    }

    #[must_use]
    pub fn decode_u32(enc_client_id: u32) -> Self {
        match enc_client_id {
            0 => Self::Broadcast,
            c => Self::Client(c.into()),
        }
    }
}

impl From<ClientId> for MessageRecipient {
    fn from(c: ClientId) -> Self {
        MessageRecipient::Client(c)
    }
}

#[cfg(test)]
mod tests {
    use crate::{ClientId, MessageRecipient};

    #[test]
    fn test_decode() {
        assert_eq!(MessageRecipient::Broadcast, MessageRecipient::decode_u32(0));
        assert_eq!(
            MessageRecipient::Client(3.into()),
            MessageRecipient::decode_u32(3)
        );
        assert_eq!(
            MessageRecipient::Client(9.into()),
            ClientId::from(9u32).into()
        );

        assert_eq!(0, MessageRecipient::Broadcast.encode_u32());
        assert_eq!(443, MessageRecipient::Client(443.into()).encode_u32());
    }
}