use std::error::Error;
use std::future::{Future, ready};
use ruststream::memory::prelude::*;
#[derive(Clone, PartialEq, prost::Message)]
struct Order {
#[prost(uint64, tag = "1")]
id: u64,
#[prost(string, tag = "2")]
sku: String,
}
impl Serialized for Order {
type Error = prost::EncodeError;
fn wire_bytes(&self, buf: &mut BytesMut) -> Result<WireBytes<'_>, Self::Error> {
prost::Message::encode(self, &mut *buf)?;
Ok(WireBytes::InBuffer)
}
}
impl MessageWire for Order {
type Wire = SerializedWire;
}
impl ReplyShape for Order {
type Body = Self;
type Headers = ();
type Wire = SerializedReply;
}
impl Deserialized for Order {
type Output<'a> = Self;
type Error = prost::DecodeError;
fn from_payload(payload: &[u8]) -> Result<Self, Self::Error> {
prost::Message::decode(payload)
}
}
impl Input for Order {
type Axis = SoloDeserialized<Self>;
}
impl OutgoingDestination for Order {
type Form = FixedName;
const DESTINATION: &'static str = "orders";
}
impl MessageHeaders for Order {
type Contract = NoHeaders;
}
impl<M: OutSlot> OutMessages<M> for Order {
fn outgoing() -> Vec<OutgoingMessageMetadata> {
vec![
OutgoingMessageMetadata::new(Self::DESTINATION, std::any::type_name::<Self>())
.with_serialized(true),
]
}
}
struct Receive;
impl Handle<Order> for Receive {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
println!("order {} of {}", order.id, order.sku);
ready(Ok(()))
}
}
fn app() -> RustStream {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(MemoryBroker::new(), |b| {
b.include(subscriber("orders", Receive).build());
b.after_startup(Publish, async move |publisher| {
publisher
.message(&Order {
id: 7,
sku: "widget".to_owned(),
})
.publish()
.await
});
})
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
app().run().await?;
Ok(())
}