use std::error::Error;
use std::future::{Future, ready};
use ruststream::memory::prelude::*;
use ruststream::runtime::layers::TracingLayer;
use ruststream::runtime::{Identity, Stack};
use serde::Deserialize;
#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct Order {
id: u64,
quantity: u32,
}
struct Confirm;
impl Handle<Order> for Confirm {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
let _ = order.id;
ready(Ok(()))
}
}
struct Reject;
impl Handle<Order> for Reject {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
if order.quantity == 0 {
return ready(Err(HandlerOutcome::retry()));
}
ready(Ok(()))
}
}
fn routes() -> impl RouterDef<MemoryBroker> {
Router::new()
.include(subscriber("orders", Confirm).build())
.include(subscriber("returns", Reject).build())
}
fn app() -> RustStream<Stack<TracingLayer, Identity>> {
RustStream::new(AppInfo::new("orders", "0.1.0"))
.layer(TracingLayer::default())
.with_broker(MemoryBroker::new(), |b| b.include_router(routes()))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
ruststream::logging::init()?;
app().run().await?;
Ok(())
}