use std::error::Error;
use std::future::{Future, ready};
use ruststream::memory::prelude::*;
use serde::Deserialize;
#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct Order {
id: u64,
}
#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct Shipment {
order_id: u64,
}
struct Accept;
impl Handle<Order> for Accept {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
println!("accepted order {}", order.id);
ready(Ok(()))
}
}
struct Dispatch;
impl Handle<Shipment> for Dispatch {
fn handle(
&self,
shipment: &Shipment,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
println!("dispatched shipment for order {}", shipment.order_id);
ready(Ok(()))
}
}
fn orders() -> Router<MemoryBroker, impl RouterDef<MemoryBroker>> {
Router::new().include(subscriber("orders", Accept).build())
}
fn shipping() -> Router<MemoryBroker, impl RouterDef<MemoryBroker>> {
Router::new().include(subscriber("shipments", Dispatch).build())
}
fn app() -> RustStream {
RustStream::new(AppInfo::new("routing", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
let all = orders().merge(shipping());
b.include_router(all);
})
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
app().run().await?;
Ok(())
}