use crate::Result;
use async_trait::async_trait;
use peat_schema::command::v1::{CommandAcknowledgment, CommandStatus, HierarchicalCommand};
#[async_trait]
pub trait CommandStorage: Send + Sync {
async fn publish_command(&self, command: &HierarchicalCommand) -> Result<String>;
async fn get_command(&self, command_id: &str) -> Result<Option<HierarchicalCommand>>;
async fn query_commands_by_target(&self, target_id: &str) -> Result<Vec<HierarchicalCommand>>;
async fn delete_command(&self, command_id: &str) -> Result<()>;
async fn publish_acknowledgment(&self, ack: &CommandAcknowledgment) -> Result<String>;
async fn get_acknowledgments(&self, command_id: &str) -> Result<Vec<CommandAcknowledgment>>;
async fn update_command_status(&self, status: &CommandStatus) -> Result<()>;
async fn get_command_status(&self, command_id: &str) -> Result<Option<CommandStatus>>;
async fn observe_commands(
&self,
node_id: &str,
callback: Box<
dyn Fn(
HierarchicalCommand,
)
-> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>
+ Send
+ Sync,
>,
) -> Result<ObserverHandle>;
async fn observe_acknowledgments(
&self,
issuer_id: &str,
callback: Box<
dyn Fn(
CommandAcknowledgment,
)
-> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>
+ Send
+ Sync,
>,
) -> Result<ObserverHandle>;
}
pub struct ObserverHandle {
inner: std::sync::Arc<dyn std::any::Any + Send + Sync>,
}
impl ObserverHandle {
pub fn new<T: std::any::Any + Send + Sync>(handle: T) -> Self {
Self {
inner: std::sync::Arc::new(handle),
}
}
pub fn inner(&self) -> &std::sync::Arc<dyn std::any::Any + Send + Sync> {
&self.inner
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_observer_handle_creation() {
}
}