Skip to main content

TypeRegistry

Struct TypeRegistry 

Source
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

Source

pub fn new() -> Self

Create an empty type registry.

Source

pub fn register( &mut self, type_oid: u32, encoder: CustomEncoder, decoder: CustomDecoder, )

Register custom encode and decode functions for a given OID.

Source

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.

Source

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.

Source

pub fn has_decoder(&self, type_oid: u32) -> bool

Check if a decoder is registered for the given OID.

Source

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 (query pg_type to find it).
  • variants — The valid variant labels for this enum.
Source

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.

Trait Implementations§

Source§

impl Default for TypeRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.