CodecRegistry¶
Maps PostgreSQL type OIDs to codecs. Immutable — adding a codec, enum, composite, or array type produces a new registry.
The default constructor creates a registry with all built-in text and binary
codecs. Use with_codec to register custom codecs, with_enum_type to
register user-defined enum types, with_composite_type to register
user-defined composite types, and with_array_type to register custom
array type mappings:
let registry = CodecRegistry
.with_codec(600, PointBinaryCodec)? // custom point type
.with_enum_type(12345)? // mood enum
.with_composite_type(16400, // address composite
recover val
[as (String, U32): ("street", 25); ("city", 25); ("zip_code", 23)]
end)?
.with_array_type(12350, 12345)? // mood[]
.with_array_type(16401, 16400)? // address[]
let session = Session(server_info, db_info, notify where registry = registry)
Constructors¶
create¶
Registry with all built-in text and binary codecs.
Returns¶
- CodecRegistry val^
Public Functions¶
with_codec¶
Returns a new registry with the given codec registered for the given OID.
Supports chaining:
CodecRegistry.with_codec(600, A)?.with_codec(790, B)?.
Errors if the OID is already registered (built-in or custom) or collides with a built-in or custom array OID. Use distinct OIDs for each codec.
Parameters¶
Returns¶
- CodecRegistry val ?
with_enum_type¶
Returns a new registry with text-passthrough codecs registered for the
given enum OID in both text and binary formats. PostgreSQL enum types use
raw UTF-8 labels on the wire in both formats, so the driver decodes them
as String. Only use this for enum type OIDs — other dynamically-assigned
types (composites, ranges) have different binary wire formats and would
produce garbage String values.
Supports chaining:
CodecRegistry.with_enum_type(12345)?.with_enum_type(12346)?.
Composes with with_array_type for enum arrays:
CodecRegistry.with_enum_type(12345)?.with_array_type(12350, 12345)?.
Errors if the OID is already registered (built-in or custom) or collides
with a built-in or custom array OID — same validation semantics as
with_codec.
Because this registers the OID in both codec maps atomically, a subsequent
with_codec(oid, ...) will error for either format. This is stricter than
two separate with_codec calls (which allow independent text/binary
registration for the same OID).
Parameters¶
- oid: U32 val
Returns¶
- CodecRegistry val ?
with_composite_type¶
Returns a new registry with the given composite type registered.
field_descriptors are (name, oid) pairs in declaration order.
Supports chaining:
CodecRegistry.with_composite_type(16400, fields)?.with_array_type(16401, 16400)?.
Errors if the OID is already registered (built-in, custom codec, enum, or composite), collides with a built-in or custom array OID, or has empty field descriptors. Self-referential field OIDs (the composite's own OID in its field list) are also rejected.
Does NOT auto-register the corresponding array type — call
with_array_type separately if needed.
fun val with_composite_type(
oid: U32 val,
field_descriptors: Array[(String val , U32 val)] val)
: CodecRegistry val ?
Parameters¶
Returns¶
- CodecRegistry val ?
with_array_type¶
Returns a new registry with a custom array type mapping. This enables
decode of arrays whose element type is a custom codec-registered OID.
Supports chaining with with_codec:
CodecRegistry.with_codec(600, PointCodec)?.with_array_type(1017, 600)?.
Errors if:
- element_oid is itself an array OID (built-in or custom), which would
cause unbounded recursion during decode
- array_oid collides with a registered scalar or built-in array OID
- array_oid is already registered as a custom array OID
- array_oid is already registered as a custom element OID
- array_oid == element_oid
Parameters¶
Returns¶
- CodecRegistry val ?
array_oid_for¶
Return the array OID for a given element OID. Checks built-in mappings first, then custom mappings. Returns 0 if no mapping exists.
Parameters¶
- element_oid: U32 val
Returns¶
- U32 val
decode¶
Decode result column data using the registered codec. Array and composite OIDs are intercepted before falling through to per-OID codec lookup.
Format 0 uses the text codec, format 1 uses the binary codec.
If no codec is registered for the OID, returns a fallback value:
String.from_array(data) for text format, RawBytes(data) for binary.
If a codec IS registered but its decode() errors, the error propagates
to the caller. This surfaces malformed data from the server (built-in
codecs) and broken custom codecs instead of silently returning fallback
values.
For arrays and composites, structural parsing errors (malformed wire format) fall back, but element/field codec errors propagate.
Parameters¶
Returns¶
- FieldData val ?
has_binary_codec¶
Whether a binary codec is registered for this OID. Returns true for known array OIDs (built-in and custom) and composite OIDs in addition to scalar codecs.
Parameters¶
- oid: U32 val
Returns¶
- Bool val