studiole_command/macros/
web_macro.rs1use 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
81pub trait IRequest:
83 Clone + Debug + DeserializeOwned + Eq + Hash + PartialEq + Send + Serialize + Sync
84{
85}
86
87pub trait ISuccess: Clone + Debug + DeserializeOwned + Send + Serialize + Sync {}
89
90pub trait IFailure: Debug + Send + Sync {}
92
93pub trait IEvent<Req: IRequest, S: ISuccess>: Clone + Debug + Send + Sync {
95 fn new(kind: EventKind, request: Req, success: Option<S>) -> Self;
97 fn get_kind(&self) -> &EventKind;
99 fn get_request(&self) -> &Req;
101 fn get_success(&self) -> &Option<S>;
103}
104
105pub trait ICommandInfo {
107 type Request: IRequest;
109 #[cfg(feature = "server")]
111 type Command: ICommand<Self::Handler, Self::Success, Self::Failure>;
112 #[cfg(feature = "server")]
114 type Handler: IHandler;
115 type Success: ISuccess;
117 type Failure: IFailure;
119 type Event: IEvent<Self::Request, Self::Success>;
121}