Skip to main content

studiole_command/macros/
web_macro.rs

1use crate::prelude::*;
2
3#[macro_export]
4macro_rules! define_commands_web {
5    ($($kind:ident($req:ty)),* $(,)?) => {
6        #[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
7        pub enum CommandRequest {
8            $(
9                $kind($req),
10            )*
11        }
12
13        impl IRequest for CommandRequest {}
14
15        $(
16            impl From<$req> for CommandRequest {
17                fn from(request: $req) -> Self {
18                    Self::$kind(request)
19                }
20            }
21        )*
22
23        #[derive(Clone, Debug, Deserialize, Serialize)]
24        pub enum CommandSuccess {
25            $(
26                $kind(<$req as Executable>::Response),
27            )*
28        }
29
30        impl ISuccess for CommandSuccess {}
31
32        #[derive(Debug)]
33        pub enum CommandFailure {
34            $(
35                $kind(<$req as Executable>::ExecutionError),
36            )*
37        }
38
39        impl IFailure for CommandFailure {}
40
41        #[derive(Clone, Debug, Deserialize, Serialize)]
42        pub struct CommandEvent {
43            kind: EventKind,
44            request: CommandRequest,
45            success: Option<CommandSuccess>,
46        }
47
48        impl IEvent<CommandRequest, CommandSuccess> for CommandEvent {
49            fn new(kind: EventKind, request: CommandRequest, success: Option<CommandSuccess>) -> Self {
50                Self { kind, request, success }
51            }
52
53            fn get_kind(&self) -> &EventKind {
54                &self.kind
55            }
56
57            fn get_request(&self) -> &CommandRequest {
58                &self.request
59            }
60
61            fn get_success(&self) -> &Option<CommandSuccess> {
62                &self.success
63            }
64        }
65
66        pub struct CommandInfo;
67
68        impl ICommandInfo for CommandInfo {
69            type Request = CommandRequest;
70            #[cfg(feature = "server")]
71            type Command =  Command;
72            #[cfg(feature = "server")]
73            type Handler = CommandHandler;
74            type Success = CommandSuccess;
75            type Failure = CommandFailure;
76            type Event = CommandEvent;
77        }
78    };
79}
80
81/// Marker trait for serializable command request enums.
82pub trait IRequest:
83    Clone + Debug + DeserializeOwned + Eq + Hash + PartialEq + Send + Serialize + Sync
84{
85}
86
87/// Marker trait for serializable command success enums.
88pub trait ISuccess: Clone + Debug + DeserializeOwned + Send + Serialize + Sync {}
89
90/// Marker trait for command failure enums.
91pub trait IFailure: Debug + Send + Sync {}
92
93/// A command lifecycle event carrying the request and optional success data.
94pub trait IEvent<Req: IRequest, S: ISuccess>: Clone + Debug + Send + Sync {
95    /// Create a new event.
96    fn new(kind: EventKind, request: Req, success: Option<S>) -> Self;
97    /// Lifecycle stage of the event.
98    fn get_kind(&self) -> &EventKind;
99    /// Request that triggered the event.
100    fn get_request(&self) -> &Req;
101    /// Success data, if the command succeeded.
102    fn get_success(&self) -> &Option<S>;
103}
104
105/// Associated types that define a complete command system.
106pub trait ICommandInfo {
107    /// Request enum type.
108    type Request: IRequest;
109    /// Command enum type (server only).
110    #[cfg(feature = "server")]
111    type Command: ICommand<Self::Handler, Self::Success, Self::Failure>;
112    /// Handler enum type (server only).
113    #[cfg(feature = "server")]
114    type Handler: IHandler;
115    /// Success enum type.
116    type Success: ISuccess;
117    /// Failure enum type.
118    type Failure: IFailure;
119    /// Event type.
120    type Event: IEvent<Self::Request, Self::Success>;
121}