talk_loco_client/talk/stream/
mod.rs

1pub mod command;
2
3use self::command::{
4    ChgMeta, DecunRead, Left, Msg, SyncDlMsg, SyncJoin, SyncLinkCr, SyncLinkPf, SyncMemT, SyncRewr,
5};
6use command::Kickout;
7use futures_loco_protocol::loco_protocol::command::BoxedCommand;
8
9macro_rules! create_enum {
10    (
11        $(#[$meta:meta])*
12        $vis:vis enum $name:ident {
13            $(
14                $(#[$variant_meta:meta])*
15                $method:literal => $variant_name:ident$(($variant_ty:ty))?
16            ),* $(,)?
17        }
18    ) => {
19        $(#[$meta])*
20        $vis enum $name {
21            $(
22                $(#[$variant_meta])*
23                $variant_name$(($variant_ty))?,
24            )*
25
26            #[doc = "Unknown command"]
27            Unknown(BoxedCommand),
28        }
29
30        impl $name {
31            pub fn deserialize_from(command: BoxedCommand) -> ::bson::de::Result<Self> {
32                Ok(match &*command.header.method {
33                    $(
34                        $method => StreamCommand::$variant_name$((::bson::from_slice::<$variant_ty>(&command.data)?))?,
35                    )*
36
37                    _ => StreamCommand::Unknown(command),
38                })
39            }
40        }
41
42        $(
43            $(
44                impl From<$variant_ty> for $name {
45                    fn from(value: $variant_ty) -> Self {
46                        Self::$variant_name(value)
47                    }
48                }
49            )?
50        )*
51
52        impl From<BoxedCommand> for $name {
53            fn from(value: BoxedCommand) -> Self {
54                Self::Unknown(value)
55            }
56        }
57    };
58}
59
60create_enum!(
61    #[derive(Debug)]
62    pub enum StreamCommand {
63        "KICKOUT" => Kickout(Kickout),
64        "CHANGESVR" => SwitchServer,
65
66        "MSG" => Chat(Msg),
67        "DECUNREAD" => ChatRead(DecunRead),
68        "CHGMETA" => ChangeMeta(ChgMeta),
69
70        "SYNCJOIN" => SyncChannelJoin(SyncJoin),
71        "SYNCDLMSG" => SyncChatDeletion(SyncDlMsg),
72        "SYNCREWR" => SyncRewrite(SyncRewr),
73
74        "SYNCLINKCR" => SyncLinkCreation(SyncLinkCr),
75        "SYNCMEMT" => SyncOpenUserType(SyncMemT),
76        "SYNCLINKPR" => SyncLinkProfile(SyncLinkPf),
77
78        "LEFT" => Left(Left),
79    }
80);