use ruststream::runtime::{App, AppInfo, HandlerResult, RustStream, TypedPublisher};
use ruststream::subscriber;
use ruststream_lapin::{DirectReplyTo, LapinBroker};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize)]
struct CheckStock {
sku: String,
quantity: u32,
}
#[derive(Debug, Serialize)]
struct Stock {
sku: String,
available: bool,
}
#[subscriber("inventory.check", publish("inventory.check.unrouted"))]
async fn check(req: &CheckStock) -> Result<Stock, HandlerResult> {
if req.sku.is_empty() {
return Err(HandlerResult::drop());
}
Ok(Stock {
sku: req.sku.clone(),
available: req.quantity <= 10,
})
}
#[ruststream::app]
fn app() -> impl App {
let broker = LapinBroker::new("amqp://localhost:5672").declare_topology(true);
RustStream::new(AppInfo::new("inventory", "0.1.0")).with_broker(broker, |b| {
let replies = TypedPublisher::new(b.broker().publisher()).transform(DirectReplyTo);
b.include_publishing(check, replies);
})
}