thrifty/container
Types
List and Set encoding/decoding for Thrift Compact Protocol.
Format:
- Short form (0-14 elements): 1 byte header (sssstttt)
- Long form (15+ elements): 1 byte header (1111tttt) + size varint
Element type values match field types from spec.
Element type codes (same as field types)
Note on boolean representation:
Historically there is a compatibility quirk in the Compact Protocol: boolean
element values inside containers (lists/sets/maps) have been encoded by
different implementations using either
1or2as the element byte. The inline boolean type nibble in field headers follows the spec mapping (1 => TRUE, 2 => FALSE); container element bytes are interpreted using the same mapping by this library (1 -> True, 2 -> False).
Policy in this library:
- The reader accepts element bytes
1and2and maps them to booleans as 1 => True, 2 => False. - The
types.ReaderOptions.bool_element_policyoption controls whether to reject bytes outside the set {1,2}. Seethrifty/types.gleamfor availableBoolElementPolicyvalues. The default policy enforces canonical validation (accept only 1 or 2 and map 1->True, 2->False).
If you require an alternative policy (for example, accept a single numeric
value for legacy interop), add a thin pre-validation wrapper before
calling the reader, or request we add an explicit bool_element_policy
configuration to express that behaviour.
pub type ElementType {
BoolType
I8Type
I16Type
I32Type
I64Type
DoubleType
BinaryType
ListType
SetType
MapType
StructType
}
Constructors
-
BoolType -
I8Type -
I16Type -
I32Type -
I64Type -
DoubleType -
BinaryType -
ListType -
SetType -
MapType -
StructType
Values
pub fn code_to_element_type(
code: Int,
) -> Result(ElementType, types.DecodeError)
Convert a compact protocol 4-bit type code to ElementType.
Inputs
code: integer value extracted from a header nibble (0-15).
Outputs
Ok(ElementType)when the code is recognized.Error(types.UnsupportedType(code))when the code is unknown.
Error modes
- Returns
types.UnsupportedTypefor unknown codes.
pub fn decode_list_header(
data: BitArray,
byte_position: Int,
) -> Result(#(Int, ElementType, Int), types.DecodeError)
Decode a list/set header from a BitArray at byte_position.
Inputs
data: theBitArraycontaining the encoded header and payload.byte_position: byte offset where the header begins.
Outputs
Ok(#(size, element_type, next_byte_position))on success wherenext_byte_positionis the byte index immediately after the header.Error(types.DecodeError)on malformed input or unexpected end.
pub fn decode_map_header(
data: BitArray,
byte_position: Int,
) -> Result(
#(Int, ElementType, ElementType, Int),
types.DecodeError,
)
Decode a map header from data at byte_position.
Inputs
data: theBitArraycontaining the encoded header and payload.byte_position: byte offset where the header begins.
Outputs
Ok(#(size, key_type, value_type, next_byte_position))wherenext_byte_positionpoints after the types byte (or after the varint for empty maps).Error(types.DecodeError)on malformed input or unexpected end.
pub fn element_type_to_code(t: ElementType) -> Int
Convert ElementType to compact protocol type code (4 bits).
Inputs
t: theElementTypeto convert.
Outputs
- Returns the 4-bit integer code used in Compact Protocol headers.
Complexity
- O(1)
pub fn encode_list_header(
size: Int,
elem_type: ElementType,
) -> BitArray
Encode a list/set header consisting of size and element type.
Inputs
size: number of elements in the list/set (non-negative).elem_type: theElementTypeof elements.
Outputs
- Returns a
BitArraycontaining either the short-form header (1 byte) whensize < 15or the long-form header (1 byte + varint) otherwise.
Complexity
- O(1) (plus cost of varint encoding when required).
pub fn encode_map_header(
size: Int,
key_type: ElementType,
value_type: ElementType,
) -> BitArray
Encode a map header (size + key type + value type).
Inputs
size: number of entries in the map (non-negative).key_type: theElementTypeused for keys.value_type: theElementTypeused for values.
Outputs
- For empty maps returns a single zero byte.
- For non-empty maps returns varint(size) followed by a single types byte where the high nibble is key type and the low nibble is value type.