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 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.