Skip to main content

Crate xmachines_codec_protobuf

Crate xmachines_codec_protobuf 

Source
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 a SystemSnapshot message (package xmachines.snapshot.v1, snapshot_proto_file, checked in as proto/xmachines_snapshot.proto): XState’s SystemSnapshot in its compact form, with field numbers instead of names, contexts, events and outputs as generic Value messages, and any key or shape a message does not describe in its extra field (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 .proto generated from a bundle’s schemas.events (package xmachines.events.<machine id>, EventSchema::proto_file): an Event envelope 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, integers sint64 (uint64 when minimum >= 0), records map<string, T>, discriminated unions a oneof, and anything else the generic Value. Absent required fields decode to protobuf’s defaults, absent nullable ones to null.

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.

  • xmachines-codec: the codec traits and their registry.
  • xmachines-kafka: its protobuf feature registers this codec; xmachines-kafka proto events <bundle> prints a machine’s event .proto.

§License

MIT

Structs§

EventSchema
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.events has none). Protobuf has no emitted events.
snapshot_proto_file
The .proto file for snapshots (checked in as proto/xmachines_snapshot.proto).