xmachines-codec 4.0.0

Pluggable codecs for the events and snapshots of xmachines-rs machines, with JSON built in
Documentation
# 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

```rust
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" }));
```

## Related crates

- [`xmachines-codec-msgpack`]https://docs.rs/xmachines-codec-msgpack: MessagePack events and snapshots (`msgpack`).
- [`xmachines-codec-protobuf`]https://docs.rs/xmachines-codec-protobuf: Protocol Buffers events and snapshots (`protobuf`).
- [`xmachines-codec-avro`]https://docs.rs/xmachines-codec-avro: Avro events, emitted events and snapshots in the Schema Registry wire format (`avro`).
- [`xmachines-kafka`]https://docs.rs/xmachines-kafka: the service that reads and writes its records with these codecs.

## License

MIT