pub struct TypeRegistry { /* private fields */ }Expand description
A registry mapping PostgreSQL OIDs to custom encode/decode functions.
This allows extensions, custom enums, and composite types to be handled
transparently by PgValue::from_text and PgValue::to_text_bytes.
§Example
use chopin_pg::types::{TypeRegistry, PgValue};
let mut registry = TypeRegistry::new();
// Register a custom enum type at OID 12345
registry.register_enum(12345, &["active", "inactive", "pending"]);
let val = registry.decode(12345, b"active").unwrap();Implementations§
Source§impl TypeRegistry
impl TypeRegistry
Sourcepub fn register(
&mut self,
type_oid: u32,
encoder: CustomEncoder,
decoder: CustomDecoder,
)
pub fn register( &mut self, type_oid: u32, encoder: CustomEncoder, decoder: CustomDecoder, )
Register custom encode and decode functions for a given OID.
Sourcepub fn decode(&self, type_oid: u32, data: &[u8]) -> Option<PgResult<PgValue>>
pub fn decode(&self, type_oid: u32, data: &[u8]) -> Option<PgResult<PgValue>>
Decode a value using the registered decoder for the given OID.
Returns None if no decoder is registered for this OID.
Sourcepub fn encode(&self, type_oid: u32, value: &PgValue) -> Option<Option<Vec<u8>>>
pub fn encode(&self, type_oid: u32, value: &PgValue) -> Option<Option<Vec<u8>>>
Encode a value using the registered encoder for the given OID.
Returns None if no encoder is registered for this OID.
Sourcepub fn has_decoder(&self, type_oid: u32) -> bool
pub fn has_decoder(&self, type_oid: u32) -> bool
Check if a decoder is registered for the given OID.
Sourcepub fn register_enum(&mut self, type_oid: u32, variants: &[&str])
pub fn register_enum(&mut self, type_oid: u32, variants: &[&str])
Register a PostgreSQL enum type.
Enum values are stored as PgValue::Text and validated against the
provided variant list on decode. Encoding just returns the text bytes.
§Arguments
type_oid— The OID of the enum type (querypg_typeto find it).variants— The valid variant labels for this enum.
Sourcepub fn register_composite(&mut self, type_oid: u32, field_oids: &[u32])
pub fn register_composite(&mut self, type_oid: u32, field_oids: &[u32])
Register a composite type with named fields and their OIDs.
Composite values are decoded from PostgreSQL’s text representation
(parenthesized, comma-separated fields) and stored as PgValue::Array
(one element per field). Fields are decoded using their respective OIDs
via PgValue::from_text.
§Arguments
type_oid— The OID of the composite type.field_oids— The OIDs of each field in order.