Skip to main content

models

Macro models 

Source
macro_rules! models {
    (@internal $set:ident crate::* $(,$($rest:tt)*)?) => { ... };
    (@internal $set:ident $crate_name:ident::* $(,$($rest:tt)*)?) => { ... };
    (@internal $set:ident $model:ty $(,$($rest:tt)*)?) => { ... };
    (@internal $set:ident) => { ... };
    ($($tokens:tt)*) => { ... };
}
Expand description

Creates a ModelSet containing the specified models.

The resulting ModelSet is typically passed to Builder::models when setting up a database connection:

ⓘ
let db = toasty::Db::builder()
    .models(toasty::models!(User, Todo))
    .connect("sqlite::memory:")
    .await?;

Each registered model automatically discovers any models reachable through its fields — embedded types, BelongsTo, HasMany, and HasOne targets are all registered transitively. You only need to list the entry-point models.

§Syntax

The macro accepts a comma-separated list of any combination of:

  • Individual models — a type path to a struct that derives Model. Module paths are supported (e.g. my_module::MyModel). Embedded types are not listed here; they are discovered through the fields of the models that contain them.
  • crate::* — registers every model discovered in the current crate.
  • some_crate::* — registers every model discovered in the named external crate.

These forms can be freely combined:

ⓘ
toasty::models!(
    // All models from the current crate
    crate::*,
    // All models from an external crate
    third_party_models::*,
    // Individual models, with or without a module path
    MyModel,
    other::SpecificModel,
)