dink-sdk 0.3.1

Rust SDK for Dink edge mesh platform — JSON-over-NATS RPC for IoT and edge computing
Documentation
use async_trait::async_trait;

use crate::error::Result;
use crate::types::{ServiceDefinition, WebhookMethodMeta};

#[async_trait]
pub trait ServiceCaller: Send + Sync {
    async fn call(&self, edge_id: &str, service: &str, method: &str, req: &[u8])
        -> Result<Vec<u8>>;

    async fn subscribe(
        &self,
        edge_id: &str,
        service: &str,
        method: &str,
        req: &[u8],
        handler: Box<dyn for<'a> Fn(&'a [u8]) -> Result<()> + Send + Sync>,
    ) -> Result<Box<dyn Subscription>>;
}

pub trait Subscription: Send + Sync {
    fn unsubscribe(&self) -> Result<()>;
}

#[async_trait]
pub trait ServiceHandler: Send + Sync {
    fn definition(&self) -> ServiceDefinition;

    async fn handle_request(&self, method: &str, req_data: &[u8]) -> Result<Vec<u8>>;

    async fn handle_stream(
        &self,
        method: &str,
        req_data: &[u8],
        emit: Box<dyn Fn(Vec<u8>) -> Result<()> + Send + Sync>,
    ) -> Result<()>;

    /// Returns webhook metadata for methods annotated with `@webhook`.
    /// Generated handlers override this when webhooks are present.
    fn webhook_methods(&self) -> Vec<WebhookMethodMeta> {
        Vec::new()
    }
}