Эта страница ещё не переведена
Ниже показан английский оригинал.
CBOR
CborCodec reads and writes CBOR through ciborium, a compact binary format with the data model of JSON; it is on with the cbor feature.
A message type derives the same serde traits as for JSON, and a struct keeps its field names on the wire.
The media type is application/cbor.
The example names the codec on the scope with with_broker_codec, so it holds whichever other codec features are on.
With cbor as the only codec feature, DefaultCodec is CborCodec, and a mounting needs to name nothing.
A payload in another format is a decode failure, settled by the subscriber's failure policy.
Example
The example builds with CBOR as the only codec:
cargo test --example codec_cbor --no-default-features --features testing,macros,memory,cbor
- Macros
- Manual
#[subscriber("orders")]
async fn bill(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "billed");
HandlerOutcome::ack()
}
fn app() -> impl App {
// Every handler of this scope decodes CBOR.
RustStream::new(AppInfo::new("billing", "0.1.0")).with_broker_codec(
MemoryBroker::new(),
CborCodec,
|b| {
b.include(bill);
},
)
}
struct Bill;
impl Handle<Order> for Bill {
fn handle(
&self,
order: &Order,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
tracing::info!(order.id, "billed");
ready(Ok(()))
}
}
fn app() -> impl App {
// Every registration of this scope decodes CBOR.
RustStream::new(AppInfo::new("billing", "0.1.0")).with_broker_codec(
MemoryBroker::new(),
CborCodec,
|b| {
b.include(subscriber("orders", Bill).build());
},
)
}