Skip to main content

Module from_schema

Module from_schema 

Source
Expand description

Phase 14, commit 5 — bridge from ModelSchema to admin metadata.

This module is the framework’s first real consumer of the Phase 14 schema contract. Given a &ModelSchema produced by #[derive(RustioModel)], it emits the per-column admin metadata required by the existing admin UI without a hand- written AdminModel impl.

§What stays untouched

Existing manual admin paths (the #[derive(RustioAdmin)] macro and projects that hand-build an AdminModel) are not affected. This module is additive — it produces values that consumers can plug into the existing AdminEntry constructor; it never modifies, replaces, or shadows any existing admin type.

§Mapping rules (Phase 14, commit 5 spec)

For each ModelColumn:

Contract fieldBridge output
nameAdminField.name (verbatim)
admin_labelAdminField.label (fallback = name)
admin_widgetBridgedField.widget (preserved through)
flags.searchableBridgedField.searchable
flags.filterableBridgedField.filterable
flags.sortableBridgedField.sortable
flags.readonlyBridgedField.readonly + editable=!ro
primary_keyBridgedField.primary_key

AdminField (the existing type) only models editable. The remaining flag bits and the widget hint live on BridgedField — a side-channel struct so consumers (search indexer, list/sort UI, future renderer changes) can read them without breaking AdminField’s shape.

§Static lifetimes via Box::leak

AdminField requires &'static str and an &'static [AdminField] slice (the existing macro emits compile-time constants). When bridging at runtime, we promote owned data to static via Box::leak. This is a one-time setup cost equivalent to a static: schemas are registered at process startup and live for the program’s lifetime, so leaked memory is never reclaimed but never grows either.

§No DB, no reflection, no new deps

Pure CPU. No async, no database access, no unsafe, no new Cargo.toml entries.

Structs§

BridgedField
One column in its bridge form: the existing AdminField (consumed verbatim by the admin UI) plus the column-level flags AdminField doesn’t model.

Functions§

admin_entry_from_schema
Build a fully-configured AdminEntry from a ModelSchema, without requiring an AdminModel impl. The resulting entry’s CRUD goes through SchemaOps; the field metadata comes from admin_fields_from_schema.
admin_entry_from_type
Same as admin_entry_from_schema but takes the model type rather than a schema value. Convenience wrapper around T::SCHEMA.
admin_fields_from_schema
Static-leaked &'static [AdminField] for direct use as AdminEntry.fields. Equivalent to a static array — the memory is allocated once and lives the program’s lifetime.
bridged_fields_from_schema
Bridge every column in declaration order. Order is preserved 1:1 with schema.columns — the admin UI lists columns in the order the model declared them, and skipping or reordering would silently change rendered forms.
field_type_for
Map a contract column’s (RustType, nullable) pair to the admin’s FieldType vocabulary.
label_for
Resolved admin label.
primary_key_column
The schema’s primary-key column, located by the primary_key = true flag. Returns None when no column is flagged (a malformed schema; the validator in commit 3 surfaces this as WrongPrimaryKey).