pub struct Schema {
pub models: IndexMap<ModelId, Model>,
}Expand description
The top-level application schema, containing all registered models.
Schema is the entry point for looking up models, fields, and variants by
their IDs, and for resolving projections through the model graph.
Schemas are typically constructed via Schema::from_macro (called by the
#[derive(Model)] proc macro) or built manually for testing.
§Examples
use toasty_core::schema::app::Schema;
let schema = Schema::default();
assert_eq!(schema.models().count(), 0);Fields§
§models: IndexMap<ModelId, Model>All models in the schema, keyed by ModelId.
Implementations§
Source§impl Schema
impl Schema
Sourcepub fn from_macro(models: impl IntoIterator<Item = Model>) -> Result<Self>
pub fn from_macro(models: impl IntoIterator<Item = Model>) -> Result<Self>
Builds a Schema from a slice of models, linking relations and
validating consistency.
This is the primary constructor used by the derive macro infrastructure.
Sourcepub fn variant(&self, id: VariantId) -> &EnumVariant
pub fn variant(&self, id: VariantId) -> &EnumVariant
Returns a reference to the EnumVariant identified by id.
§Panics
Panics if the model is not an EmbeddedEnum or
the variant index is out of bounds.
Sourcepub fn models(&self) -> impl Iterator<Item = &Model>
pub fn models(&self) -> impl Iterator<Item = &Model>
Returns an iterator over all models in the schema.
Sourcepub fn get_model(&self, id: impl Into<ModelId>) -> Option<&Model>
pub fn get_model(&self, id: impl Into<ModelId>) -> Option<&Model>
Try to get a model by ID, returning None if not found.
Sourcepub fn fields(&self, id: impl Into<ModelId>) -> &[Field]
pub fn fields(&self, id: impl Into<ModelId>) -> &[Field]
The fields of the model id, in declaration order.
An embedded struct backs a #[document] column, so this doubles as a
document column’s field layout — the embed is the single source of
truth for its shape. Callers map each Field to what they need (its
name, its expr_ty); a field typed
Type::Model (or List(Model)) signals a nested document to recurse
into.
Panics if no model has the given ID.
Sourcepub fn project_fields<'s>(
&'s self,
model: ModelId,
projection: &[usize],
) -> impl Iterator<Item = &'s Field>
pub fn project_fields<'s>( &'s self, model: ModelId, projection: &[usize], ) -> impl Iterator<Item = &'s Field>
Walks a positional projection through model’s fields, descending
into the nested model whenever a step lands on a model-typed field
(Type::Model). Yields the Field at each step, in order — the last
one is the projection’s leaf. The caller takes whatever it needs from
each field (its name, its type).
Iteration stops short of projection.len() when a step cannot be
taken: its index is out of range, or it descends past a field that is
not model-typed. A projection therefore resolves fully iff the iterator
yields exactly projection.len() fields. Callers that read the leaf
(the engine’s JSON-path lowering, the DynamoDB driver’s document-path
rendering) work from projections already validated by
resolve.
Nothing constrains model to a #[document] embed — the walk follows
any model-typed field — though document paths are its only use today.
Sourcepub fn resolve<'a>(
&'a self,
root: &'a Model,
projection: &Projection,
) -> Option<Resolved<'a>>
pub fn resolve<'a>( &'a self, root: &'a Model, projection: &Projection, ) -> Option<Resolved<'a>>
Resolve a projection through the schema, returning either a field or an enum variant.
Starting from the root model, walks through each step of the projection, resolving fields, following relations/embedded types, and recognizing enum variant discriminant access.
Returns None if:
- The projection is empty
- Any step references an invalid field/variant index
- A step tries to project through a primitive type
Sourcepub fn resolve_field<'a>(
&'a self,
root: &'a Model,
projection: &Projection,
) -> Option<&'a Field>
pub fn resolve_field<'a>( &'a self, root: &'a Model, projection: &Projection, ) -> Option<&'a Field>
Resolve a projection to a field, walking through the schema.
Returns None if the projection is empty, invalid, or resolves to an
enum variant rather than a field.
Sourcepub fn resolve_field_path<'a>(&'a self, path: &Path) -> Option<&'a Field>
pub fn resolve_field_path<'a>(&'a self, path: &Path) -> Option<&'a Field>
Resolves a stmt::Path to a Field by extracting the root model
from the path and delegating to resolve_field.