ruststream 0.7.0-rc.2

Async messaging framework for Rust: broker-agnostic traits, router, codecs, and a conformance harness for broker authors.
Documentation
//! Domain types and handlers, written as `#[subscriber]` functions.
//!
//! The first parameter is the decoded payload; the macro turns each function into a mountable
//! definition (a value named after the function) that `routes` collects into a `Router`. `confirm`
//! consumes `orders` and replies on `confirmations`; `on_cancel` handles `cancellations`.

use ruststream::runtime::HandlerOutcome;
use ruststream::{Outgoing, subscriber};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// An order placed on the `orders` channel.
///
/// `JsonSchema` lets `asyncapi gen` emit this payload's schema into the generated document.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct Order {
    pub id: u64,
    pub item: String,
    pub quantity: u32,
}

/// The reply published to `confirmations` for each order.
///
/// `Outgoing` is what declares it a message this service sends, and `name` is where it goes.
#[derive(Debug, Serialize, JsonSchema, Outgoing)]
#[outgoing(name = "confirmations")]
pub struct Confirmation {
    pub id: u64,
    pub accepted: bool,
}

/// Confirms an incoming order and publishes a `Confirmation` to `confirmations`.
///
/// The return value is the reply: the `publish` clause makes the runtime encode it and send it
/// through the publisher wired in `routes`.
#[subscriber("orders", publish)]
pub async fn confirm(order: &Order) -> Confirmation {
    Confirmation {
        id: order.id,
        accepted: order.quantity > 0,
    }
}

/// Logs cancellations, bound by plain name. No reply, so it returns a plain `HandlerOutcome`.
#[subscriber("cancellations")]
pub async fn on_cancel(order: &Order) -> HandlerOutcome {
    println!("order {} ({}) cancelled", order.id, order.item);
    HandlerOutcome::ack()
}