myko/core/command/
traits.rs1use std::{fmt::Debug, sync::Arc};
4
5use serde::{Serialize, de::DeserializeOwned};
6use serde_json::Value;
7
8use crate::{client::MykoClient, common::with_transaction::WithTransaction, wire::WrappedCommand};
9
10pub trait CommandId {
15 fn command_id(&self) -> Arc<str>;
16}
17
18pub trait CommandIdStatic {
20 const COMMAND_ID: &'static str;
22
23 fn command_id_static() -> &'static str {
25 Self::COMMAND_ID
26 }
27}
28
29pub trait CommandResultType {
31 type Result: Serialize + DeserializeOwned + Clone + Send + Sync + 'static;
32}
33
34pub trait AnyCommand: WithTransaction + CommandId + Debug + Send + Sync + 'static {
37 fn to_value(&self) -> Value;
39}
40
41pub trait MykoCommand<T: DeserializeOwned + Clone + Send + Sync + 'static> {
43 fn handle(
44 &self,
45 client: &MykoClient,
46 ) -> hyphae::Cell<Option<Result<T, String>>, hyphae::CellImmutable>;
47}
48
49pub trait CommandParams:
60 Serialize
61 + DeserializeOwned
62 + Clone
63 + Send
64 + Sync
65 + CommandId
66 + CommandIdStatic
67 + CommandResultType
68 + Debug
69 + 'static
70{
71}
72
73impl<T> CommandParams for T where
75 T: Serialize
76 + DeserializeOwned
77 + Clone
78 + Send
79 + Sync
80 + CommandId
81 + CommandIdStatic
82 + CommandResultType
83 + Debug
84 + 'static
85{
86}
87
88impl From<&dyn AnyCommand> for WrappedCommand {
94 fn from(command: &dyn AnyCommand) -> Self {
95 WrappedCommand {
96 command: command.to_value(),
97 command_id: command.command_id().to_string(),
98 }
99 }
100}
101
102impl From<Arc<dyn AnyCommand>> for WrappedCommand {
103 fn from(command: Arc<dyn AnyCommand>) -> Self {
104 WrappedCommand::from(command.as_ref())
105 }
106}
107
108impl From<&Arc<dyn AnyCommand>> for WrappedCommand {
109 fn from(command: &Arc<dyn AnyCommand>) -> Self {
110 WrappedCommand::from(command.as_ref())
111 }
112}