Skip to main content

Crate xmachines_codec

Crate xmachines_codec 

Source
Expand description

§xmachines-codec

Codecs for the events and snapshots of xmachines-rs machines: what turns an event ({ "type": ..., ... }) or an actor system’s snapshot (XState’s SystemSnapshot) into bytes and back, for hosts that move them over a wire or keep them in a log (the Kafka service, WASI and Node.js hosts, a broker).

§When to use it

Use it in a host that reads events from records or writes snapshots to a log, and wants to choose the encoding by name, as a machine chooses its evaluators by language. The crate uses nothing of a service or of Kafka (of xmachines-runtime, only the snapshot view type), so any host can use it. JSON is built in; MessagePack, protobuf and Avro are crates of their own.

§The contract

  • EventCodec: decode(&[u8]) -> Result<Value, Error> reads an event from a record’s bytes, and encode(&Value, &mut Vec<u8>) appends an event’s encoding.
  • SnapshotCodec: format() is the first byte of every record it writes, compact() says whether it always takes the compact form of a snapshot, encode(Snapshot, &mut Vec<u8>) appends a record and decode(&[u8]) -> Result<Value, Error> reads one back to the snapshot’s JSON. A Snapshot is the JSON or the runtime’s view of an actor system (xmachines_runtime::Actor::persist_view and persist_compact), which a codec serializes directly, without building the JSON.
  • Codec: one format, built for a machine: a name, the media types of its records, and optional codecs of input events, emitted events and snapshots. Codecs that follow a machine’s schemas (protobuf, Avro) are built from its bundle, so a Codec serves the machines it was built for.
  • Codecs: the codecs a host reads and writes, by name, with JSON (json) registered from the start. A media type and a snapshot format byte each belong to one codec (Error::Conflict otherwise), so by_media_type finds the codec of a record’s content-type, and decode_snapshot reads a record written by any registered codec, by its format byte, whatever codec writes new ones.
  • Error: Decode, Encode, Schema, UnknownFormat and Conflict, each with its message.

The format bytes are JSON 1, MessagePack 2, protobuf 3 and Avro 0 (the Schema Registry’s magic byte). varint has the varint and zigzag helpers the binary codecs share.

§Example

use serde_json::json;
use xmachines_codec::Codecs;

let codecs = Codecs::new();
let json = codecs.by_media_type("application/json; charset=utf-8").unwrap();
let event = json.events().unwrap().decode(br#"{ "type": "go" }"#).unwrap();
assert_eq!(event, json!({ "type": "go" }));

let mut record = Vec::new();
json.snapshots().unwrap().encode((&json!({ "root": "m" })).into(), &mut record).unwrap();
assert_eq!(record[0], xmachines_codec::json::FORMAT);
assert_eq!(codecs.decode_snapshot(&record).unwrap(), json!({ "root": "m" }));

§License

MIT

Modules§

json
JSON, the built-in codec (json): events as JSON text, and snapshot records of a format byte and the snapshot’s JSON text, readable with any tool.
varint
Base-128 varints (least significant group first, as protobuf, Avro and the Kafka service’s changelog marks write them) and zigzag signed integers.

Structs§

Codec
A codec of one format, built for the machines it serves: its name, the media types of its event records, and its codecs of input events, emitted events and snapshots (each optional: protobuf has no emitted events, and a codec built for a machine without event schemas has no input events).
Codecs
The codecs a host reads and writes records with, by name, with JSON always among them. Media types and snapshot format bytes each belong to one codec, so an event record’s content type and a snapshot record’s first byte find the codec that reads it.

Enums§

Error
Why a codec could not read or write something, or could not be built or registered.
Snapshot
A snapshot to write: XState’s SystemSnapshot as JSON, or the runtime’s view of an actor system, which serializes as that JSON would without building it (xmachines_runtime::Actor::persist_view and persist_compact).

Traits§

EventCodec
Events as bytes: a record’s bytes to the JSON event a machine handles, and an event to bytes.
SnapshotCodec
Snapshots as records: XState’s SystemSnapshot (its JSON, or the runtime’s view of it) to bytes, and bytes back to the JSON a restore reads.