Skip to main content

FromPgRow

Trait FromPgRow 

Source
pub trait FromPgRow: Sized {
    const COLUMNS: &'static [&'static str];
    const COLUMN_LIST: &'static str;

    // Required method
    fn from_pg_row(row: &Row) -> Result<Self, DjogiError>;
}
Expand description

Canonical row-decode trait for #[model]-annotated structs. Do not implement this manually — #[model] emits the impl. Users can still bound generic code on T: FromPgRow (e.g. to accept any model in a helper function), which is the intended public shape.

§Contract

Implementors must guarantee that:

  1. COLUMNS lists fields in the exact order from_pg_row reads them from the row (ordinal position matches slice index).
  2. COLUMN_LIST equals COLUMNS.join(", ") — callers interpolate it into SQL text expecting exactly that shape.
  3. from_pg_row returns Err(DjogiError::Decode) on any column-level type-conversion failure, not panic. (The debug_assert on column-name drift is a separate invariant violation and is allowed to panic.)

Required Associated Constants§

Source

const COLUMNS: &'static [&'static str]

Column names in the canonical SELECT order (framework fields first, then user fields). COLUMNS[i] is the name of the column decoded by from_pg_row at ordinal position i. The macro uses this slice both to emit the per-column debug_assert_eq! name guard and to build COLUMN_LIST at compile time.

Source

const COLUMN_LIST: &'static str

Canonical column list for SQL emission — the same names as COLUMNS, joined with ", ". Baked at macro time so callers never need to allocate. Interpolate directly into SQL text:

let sql = format!("SELECT {} FROM {} WHERE id = $1",
                  <User as FromPgRow>::COLUMN_LIST,
                  User::table_name());

Required Methods§

Source

fn from_pg_row(row: &Row) -> Result<Self, DjogiError>

Decode Self from a tokio_postgres::Row positionally. Column ordinals are fixed at macro time and match COLUMNS index-for-index. Callers must supply a row produced by a SELECT whose projection matches COLUMN_LIST — the CRUD and QuerySet terminals shipped by Djogi guarantee this; hand-rolled SELECTs must either interpolate COLUMN_LIST or supply columns in the same order. Returns DjogiError::Decode with the offending column name on wire-type mismatch. In debug builds, a debug_assert_eq! on each row.columns()[i].name() panics if the wire shape drifts from COLUMNS. Release builds skip the guard.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§