ToCreateModel

Derive Macro ToCreateModel 

Source
#[derive(ToCreateModel)]
{
    // Attributes available to this derive:
    #[crudcrate]
    #[active_model]
}
Expand description

§=================== ToCreateModel Macro

This macro:

  1. Generates a struct named <OriginalName>Create that includes only the fields where #[crudcrate(create_model = false)] is NOT specified (default = true). If a field has an on_create expression, its type becomes Option<…> (with #[serde(default)]) so the user can override that default.
  2. Generates an impl From<<OriginalName>Create> for <ActiveModelType>> where:
    • For each field with on_create:
      • If the original type was Option<T>, then create.<field> is Option<Option<T>>. We match on that and do:
        match create.field {
          Some(Some(v)) => Some(v.into()),      // user overrode with T
          Some(None)    => None,                // user explicitly set null
          None          => Some((expr).into()), // fallback to expr
        }
      • If the original type was non‐optional T, then create.<field> is Option<T>. We match on that and do:
        match create.field {
          Some(v) => v.into(),
          None    => (expr).into(),
        }
    • For each field without on_create:
      • If the original type was Option<T>, we do create.<field>.map(|v| v.into()).
      • If it was non‐optional T, we do create.<field>.into().
    • For any field excluded (create_model = false) but having on_create, we do Some((expr).into()) if it was Option<T>, or just (expr).into() otherwise.