pub struct Decoder<T> { /* private fields */ }Expand description
Reconstructs a JSON value from the snapshot and delta frames of a group.
The track-free core of Consumer, and the mirror of
Encoder. The caller reads frames from wherever it likes and routes each one
by its position in the group: the first frame of every group is a
snapshot, the rest are deltas.
match frame.keyframe {
true => decoder.snapshot(&frame.payload)?,
false => decoder.delta(&frame.payload)?,
}
let value = decoder.decode()?;Applying and materializing are separate on purpose. Frames must be applied in order (the merge
patches and the DEFLATE window are both sequential), but a consumer catching up on a backlog only
wants the value at the head, so it applies every frame and calls decode once.
A caller that wants a value per frame just calls it every time.
Implementations§
Source§impl<T> Decoder<T>
impl<T> Decoder<T>
Sourcepub fn new(config: ConsumerConfig) -> Self
pub fn new(config: ConsumerConfig) -> Self
Create a decoder with no value, awaiting its first snapshot.
Sourcepub fn snapshot(&mut self, payload: &[u8]) -> Result<()>
pub fn snapshot(&mut self, payload: &[u8]) -> Result<()>
Apply a group’s first frame: a full snapshot that replaces the current value.
Also starts the group’s DEFLATE window, so this must be called at every group boundary, not only the first.
Source§impl<T: DeserializeOwned> Decoder<T>
impl<T: DeserializeOwned> Decoder<T>
Sourcepub fn decode(&self) -> Result<Option<T>>
pub fn decode(&self) -> Result<Option<T>>
Materialize the reconstructed value as T, or None before the first snapshot.
Deserializing from the reconstructed Value rather than the frame bytes costs the line and
column a parse error would carry, so the error is prefixed with the JSON path of the offending
field instead. Without it a rejected field deep in a document reports only its own complaint,
with nothing to say where it came from.