use std::{fmt::Debug, sync::Arc};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::Value;
use crate::{client::MykoClient, common::with_transaction::WithTransaction, wire::WrappedCommand};
pub trait CommandId {
fn command_id(&self) -> Arc<str>;
}
pub trait CommandIdStatic {
const COMMAND_ID: &'static str;
fn command_id_static() -> &'static str {
Self::COMMAND_ID
}
}
pub trait CommandResultType {
type Result: Serialize + DeserializeOwned + Clone + Send + Sync + 'static;
}
pub trait AnyCommand: WithTransaction + CommandId + Debug + Send + Sync + 'static {
fn to_value(&self) -> Value;
}
pub trait MykoCommand<T: DeserializeOwned + Clone + Send + Sync + 'static> {
fn handle(
&self,
client: &MykoClient,
) -> hyphae::Cell<Option<Result<T, String>>, hyphae::CellImmutable>;
}
pub trait CommandParams:
Serialize
+ DeserializeOwned
+ Clone
+ Send
+ Sync
+ CommandId
+ CommandIdStatic
+ CommandResultType
+ Debug
+ 'static
{
}
impl<T> CommandParams for T where
T: Serialize
+ DeserializeOwned
+ Clone
+ Send
+ Sync
+ CommandId
+ CommandIdStatic
+ CommandResultType
+ Debug
+ 'static
{
}
impl From<&dyn AnyCommand> for WrappedCommand {
fn from(command: &dyn AnyCommand) -> Self {
WrappedCommand {
command: command.to_value(),
command_id: command.command_id().to_string(),
}
}
}
impl From<Arc<dyn AnyCommand>> for WrappedCommand {
fn from(command: Arc<dyn AnyCommand>) -> Self {
WrappedCommand::from(command.as_ref())
}
}
impl From<&Arc<dyn AnyCommand>> for WrappedCommand {
fn from(command: &Arc<dyn AnyCommand>) -> Self {
WrappedCommand::from(command.as_ref())
}
}