Expand description
§xmachines-codec-protobuf
Protocol Buffers for xmachines-rs machines, an xmachines-codec codec named protobuf, without generated code or protoc: a hand-written wire format, a generic JSON Value message, and snapshots and input events whose messages derive from a machine’s schemas.
§When to use it
Use it when producers already speak protobuf (devices, services with generated clients), or for the smallest snapshot records: a small order actor’s snapshot is 248 bytes (484 in MessagePack’s compact form). Events need the machine’s bundle, since their messages follow its event schemas; snapshots need nothing.
- Snapshots (
Snapshots) are records of format byte 3 and aSystemSnapshotmessage (packagexmachines.snapshot.v1,snapshot_proto_file, checked in asproto/xmachines_snapshot.proto): XState’sSystemSnapshotin its compact form, with field numbers instead of names, contexts, events and outputs as genericValuemessages, and any key or shape a message does not describe in itsextrafield (field 100), so every snapshot round-trips value for value, as JavaScript reads it (a whole double within the safe-integer range reads back as an integer). - Events (
EventSchema) follow a.protogenerated from a bundle’sschemas.events(packagexmachines.events.<machine id>,EventSchema::proto_file): anEventenvelope with one oneof member per event type, in schema order. Field numbers follow property order unless Zod’s.meta({ protoField: n })sets them; string enums become enums, integerssint64(uint64whenminimum >= 0), recordsmap<string, T>, discriminated unions a oneof, and anything else the genericValue. Absent required fields decode to protobuf’s defaults, absent nullable ones tonull.
The codec has no emitted events. Its media types are application/x-protobuf, application/protobuf and application/vnd.google.protobuf.
§Example
use std::sync::Arc;
use serde_json::json;
use xmachines_codec_protobuf::EventSchema;
let bundle = json!({
"machine": { "id": "meter" },
"schemas": { "events": { "reading": {
"type": "object", "properties": { "value": { "type": "number" } }, "required": ["value"]
} } }
});
let schema = Arc::new(EventSchema::from_bundle(&bundle).unwrap());
assert!(schema.proto_file().contains("package xmachines.events.meter;"));
let codec = xmachines_codec_protobuf::codec(Some(schema));
let events = codec.events().unwrap();
let mut bytes = Vec::new();
events.encode(&json!({ "type": "reading", "value": 21.5 }), &mut bytes).unwrap();
assert_eq!(events.decode(&bytes).unwrap(), json!({ "type": "reading", "value": 21.5 }));codec(None) is the codec of snapshots alone.
§Contract
The snapshot .proto is generated by the crate and checked in; a unit test fails when it is stale, and XSTATE_WRITE_PROTO=1 cargo test -p xmachines-codec-protobuf --lib rewrites it. The crate’s tests round-trip every recorded XState snapshot of the repository’s fixtures and read the records with prost as an independent decoder.
§Related crates
xmachines-codec: the codec traits and their registry.xmachines-kafka: itsprotobuffeature registers this codec;xmachines-kafka proto events <bundle>prints a machine’s event.proto.
§License
MIT
Structs§
- Event
Schema - The protobuf form of a machine’s input events.
- Snapshots
- The protobuf codec of snapshots, always of their compact form: its records are not for XState’s own restore anyway.
Enums§
- Error
- Why a message could not be read or written, or a bundle’s event schemas have no protobuf form.
Constants§
- FORMAT
- The format byte of protobuf snapshot records.
- MEDIA_
TYPES - The media types of protobuf event records.
- NAME
- The name protobuf is registered as.
Functions§
- codec
- The protobuf codec: snapshots, and input events when there is an event
schema (a bundle without
schemas.eventshas none). Protobuf has no emitted events. - snapshot_
proto_ file - The
.protofile for snapshots (checked in asproto/xmachines_snapshot.proto).