Schema

Struct Schema 

Source
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

Source

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.

Source

pub fn field(&self, id: FieldId) -> &Field

Returns a reference to the Field identified by id.

§Panics

Panics if the model or field index is invalid.

Source

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.

Source

pub fn models(&self) -> impl Iterator<Item = &Model>

Returns an iterator over all models in the schema.

Source

pub fn get_model(&self, id: impl Into<ModelId>) -> Option<&Model>

Try to get a model by ID, returning None if not found.

Source

pub fn model(&self, id: impl Into<ModelId>) -> &Model

Returns a reference to the Model identified by id.

§Panics

Panics if no model with the given ID exists in the schema.

Source

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
Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Schema

Source§

fn default() -> Schema

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

Auto Trait Implementations§

§

impl Freeze for Schema

§

impl RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnwindSafe for Schema

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.