下面显示的是英文原文。
Prelude
Glob file::prelude on a stream file, stdio::prelude on a pipeline, and the crate prelude only in a service that uses both.
Each glob carries the core's own prelude, so #[subscriber], RustStream, HandlerOutcome and Out come with it.
use ruststream_sea_file::file::prelude::*; adds FileBroker, the descriptor FileStream, the positions FilePosition, the context keys Position and SeekHandle, and the publish policy as Publish.
use ruststream_sea_file::stdio::prelude::*; adds StdioBroker and its policy as Publish.
Publish is the name every broker crate gives its publish policy, so a mount site reads the same on every transport.
Moving a service from one transport to the other changes the glob and the broker, and leaves the mount sites alone.
The two transports would claim the same name Publish, so the crate prelude carries neither.
A service on both brokers globs it and writes the prefixed FilePublish and StdioPublish.
Example
One service records orders from a stream file and answers orders read on standard input.
- Macros
- Manual
use ruststream_sea_file::prelude::*;
#[subscriber(FileStream::new("orders"), reply("receipts"))]
async fn record(order: &Order) -> Receipt {
Receipt { order: order.id }
}
#[subscriber("orders", reply("receipts"))]
async fn answer(order: &Order) -> Receipt {
Receipt { order: order.id }
}
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0"))
.with_broker(FileBroker::new("/var/lib/orders/orders.ss"), |b| {
b.include(record).out_reply(FilePublish);
})
.with_broker(StdioBroker::new(), |b| {
b.include(answer)
.out_reply(StdioPublish)
.out_retry(StdioPublish)
.to("orders.retry");
})
}
use ruststream_sea_file::prelude::*;
struct Record;
impl Handle<Order, Receipt> for Record {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<Receipt, HandlerOutcome>> {
ready(Ok(Receipt { order: order.id }))
}
}
struct Answer;
impl Handle<Order, Receipt> for Answer {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<Receipt, HandlerOutcome>> {
ready(Ok(Receipt { order: order.id }))
}
}
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0"))
.with_broker(FileBroker::new("/var/lib/orders/orders.ss"), |b| {
let record = subscriber(FileStream::new("orders"), Record);
b.include(record.reply().to("receipts").build())
.out_reply(FilePublish);
})
.with_broker(StdioBroker::new(), |b| {
b.include(subscriber("orders", Answer).reply().to("receipts").build())
.out_reply(StdioPublish)
.out_retry(StdioPublish)
.to("orders.retry");
})
}