tsoracle-codec
The one version-prefixed postcard codec shared across tsoracle — every persisted blob is framed as [version_byte | postcard(value)] so an on-disk layout change fails loudly instead of misdecoding.
Why it exists
Both consensus toolkits (tsoracle-paxos-toolkit, tsoracle-openraft-toolkit) carried a near-identical encode/decode/CodecError copy of this framing. Every persisted struct — a Ballot, a HighWaterCommand log entry, a snapshot, a Raft log record — is stamped with a leading schema-version byte; a node upgraded mid-flight that reads prior-version bytes hits a structured version mismatch rather than silently parsing them against a new struct layout. This crate is the single home for that framing.
What's in the box
encode(version, value)— serializesvaluewithpostcardand prependsversion, yielding[version | postcard(value)].decode(expected_version, bytes)— checks the leading byte againstexpected_version(returningCodecError::Versionon mismatch) before deserializing the body.CodecError—Empty,Version { expected, actual },Encode(postcard::Error),Decode(postcard::Error). Encode and decode failures are kept distinct, and the underlyingpostcard::Erroris carried as the error source (noFromconversion) so a stray?never silently becomes aCodecError.
The schema-version number is deliberately not owned here. version is a parameter, so each consumer keeps its own SCHEMA_VERSION constant and evolves its on-disk format independently of the others.
Quick reference
# consumer Cargo.toml
[]
= { = true }
// Each consumer owns its own schema version.
pub const SCHEMA_VERSION: u8 = 1;
let framed = encode?;
let value: MyType = decode?;
Bump the consumer's SCHEMA_VERSION when a persisted struct's postcard layout changes in a backward-incompatible way (field reorder/insert/remove); a stale reader then fails with CodecError::Version instead of misdecoding.
Related
postcard— the compactserdewire format this frames.tsoracle-failpoint,tsoracle-yieldpoint— the other shared micro-crates that exist so a single behavior is not duplicated across the workspace.