Skip to main content

Module codec

Module codec 

Source
Expand description

Turning bytes into Messages, and back.

§Two phases, so that every failure can still be answered

S2 requires a ReceptionStatus for every message received, and the status has to name the message it is about. That is awkward for a message that failed to parse: the subject_message_id has to come from somewhere.

So decoding happens in two phases. First a Peek pulls out message_type, message_id and subject_message_id, ignoring everything else and tolerating almost any malformation. Then the typed decode runs, refusing unknown properties. Whatever goes wrong, the error carries what the peek learned, and DecodeError::reception_status turns it into exactly the status the standard prescribes — INVALID_DATA for “not understood”, INVALID_MESSAGE for “not according to schema” — naming the right message, or the nil identifier when there was nothing to name.

use s2_kit::codec::{decode, DecodeError};

let err = decode("{ not json").unwrap_err();
let status = err.reception_status();
assert_eq!(status.status, s2_kit::types::common::ReceptionStatusValues::InvalidData);
assert_eq!(status.subject_message_id, s2_kit::types::Id::NIL);

§Bounded before it is parsed

maxItems does not bound a message meaningfully: a schema-legal PPBC.PowerProfileDefinition may hold a thousand containers of 288 sequences of 288 elements. DecodeOptions::max_bytes is checked before the parser runs.

Structs§

DecodeOptions
Everything that shapes a decode.
Decoded
What a successful decode produced.
Peek
What a decode learned before trying to build a typed message.

Enums§

DecodeError
Why a message could not be decoded.
Strictness
How much malformation a decoder tolerates.

Functions§

decode
Decode one S2 message, strictly, against S2 JSON v1.0.0.
decode_with
Decode one S2 message.
encode
Encode one S2 message.
encode_pretty
Encode one S2 message with indentation, for logs and fixtures.
peek
Read the three fields a reception status needs, tolerating everything else.