toasty_core/schema/app/model.rs
1use super::{Field, FieldId, FieldPrimitive, FieldTy, Index, Name, PrimaryKey};
2use crate::{Result, driver, stmt};
3use indexmap::IndexMap;
4use std::fmt;
5
6/// A model in the application schema.
7///
8/// Models come in three flavors:
9///
10/// - [`Model::Root`] -- a top-level model backed by its own database table.
11/// - [`Model::EmbeddedStruct`] -- a struct whose fields are flattened into a
12/// parent model's table.
13/// - [`Model::EmbeddedEnum`] -- an enum stored via a discriminant column plus
14/// optional per-variant data columns in the parent table.
15///
16/// # Examples
17///
18/// ```ignore
19/// use toasty_core::schema::app::{Model, Schema};
20///
21/// let schema: Schema = /* built from derive macros */;
22/// for model in schema.models() {
23/// if model.is_root() {
24/// println!("Root model: {}", model.name().upper_camel_case());
25/// }
26/// }
27/// ```
28#[derive(Debug, Clone)]
29pub enum Model {
30 /// A root model that maps to its own database table and can be queried
31 /// directly.
32 Root(ModelRoot),
33 /// An embedded struct whose fields are flattened into its parent model's
34 /// table.
35 EmbeddedStruct(EmbeddedStruct),
36 /// An embedded enum stored as a discriminant column (plus optional
37 /// per-variant data columns) in the parent table.
38 EmbeddedEnum(EmbeddedEnum),
39}
40
41/// An ordered collection of [`Model`] definitions.
42///
43/// `ModelSet` is the primary container used to hold all models in a schema.
44/// Models are stored in insertion order and can be iterated over by reference
45/// or by value.
46///
47/// # Examples
48///
49/// ```
50/// use toasty_core::schema::app::{Model, ModelSet};
51///
52/// let mut set = ModelSet::new();
53/// assert_eq!(set.iter().len(), 0);
54/// ```
55#[derive(Debug, Clone, Default)]
56pub struct ModelSet {
57 models: IndexMap<ModelId, Model>,
58}
59
60impl ModelSet {
61 /// Creates an empty `ModelSet`.
62 pub fn new() -> Self {
63 Self::default()
64 }
65
66 /// Returns the number of models in the set.
67 pub fn len(&self) -> usize {
68 self.models.len()
69 }
70
71 /// Returns `true` if the set contains no models.
72 pub fn is_empty(&self) -> bool {
73 self.models.is_empty()
74 }
75
76 /// Returns `true` if the set contains a model with the given ID.
77 pub fn contains(&self, id: ModelId) -> bool {
78 self.models.contains_key(&id)
79 }
80
81 /// Inserts a model into the set, keyed by its [`ModelId`].
82 ///
83 /// If a model with the same ID already exists, it is replaced.
84 pub fn add(&mut self, model: Model) {
85 self.models.insert(model.id(), model);
86 }
87
88 /// Returns an iterator over the models in insertion order.
89 pub fn iter(&self) -> impl ExactSizeIterator<Item = &Model> {
90 self.models.values()
91 }
92}
93
94impl<'a> IntoIterator for &'a ModelSet {
95 type Item = &'a Model;
96 type IntoIter = indexmap::map::Values<'a, ModelId, Model>;
97
98 fn into_iter(self) -> Self::IntoIter {
99 self.models.values()
100 }
101}
102
103impl IntoIterator for ModelSet {
104 type Item = Model;
105 type IntoIter = ModelSetIntoIter;
106
107 fn into_iter(self) -> Self::IntoIter {
108 ModelSetIntoIter {
109 inner: self.models.into_iter(),
110 }
111 }
112}
113
114/// An owning iterator over the models in a [`ModelSet`].
115pub struct ModelSetIntoIter {
116 inner: indexmap::map::IntoIter<ModelId, Model>,
117}
118
119impl Iterator for ModelSetIntoIter {
120 type Item = Model;
121
122 fn next(&mut self) -> Option<Self::Item> {
123 self.inner.next().map(|(_, model)| model)
124 }
125
126 fn size_hint(&self) -> (usize, Option<usize>) {
127 self.inner.size_hint()
128 }
129}
130
131impl ExactSizeIterator for ModelSetIntoIter {}
132
133/// A root model backed by its own database table.
134///
135/// Root models have a primary key, may define indices, and are the only model
136/// kind that can be the target of relations. They are the main entities users
137/// interact with through Toasty's query API.
138///
139/// # Examples
140///
141/// ```ignore
142/// let root = model.as_root_unwrap();
143/// let pk_fields: Vec<_> = root.primary_key_fields().collect();
144/// ```
145#[derive(Debug, Clone)]
146pub struct ModelRoot {
147 /// Uniquely identifies this model within the schema.
148 pub id: ModelId,
149
150 /// The model's name.
151 pub name: Name,
152
153 /// All fields defined on this model.
154 pub fields: Vec<Field>,
155
156 /// The primary key definition. Root models always have a primary key.
157 pub primary_key: PrimaryKey,
158
159 /// Optional explicit table name. When `None`, a name is derived from the
160 /// model name.
161 pub table_name: Option<String>,
162
163 /// Secondary indices defined on this model.
164 pub indices: Vec<Index>,
165
166 /// The versionable field, if any. Points directly into `fields` to avoid scanning.
167 pub version_field: Option<FieldId>,
168}
169
170impl ModelRoot {
171 /// Builds a `SELECT` query that filters by this model's primary key using
172 /// the supplied `input` to resolve argument values.
173 pub fn find_by_id(&self, mut input: impl stmt::Input) -> stmt::Query {
174 let filter = match &self.primary_key.fields[..] {
175 [pk_field] => stmt::Expr::eq(
176 stmt::Expr::ref_self_field(pk_field),
177 input
178 .resolve_arg(&0.into(), &stmt::Projection::identity())
179 .unwrap(),
180 ),
181 pk_fields => stmt::Expr::and_from_vec(
182 pk_fields
183 .iter()
184 .enumerate()
185 .map(|(i, pk_field)| {
186 stmt::Expr::eq(
187 stmt::Expr::ref_self_field(pk_field),
188 input
189 .resolve_arg(&i.into(), &stmt::Projection::identity())
190 .unwrap(),
191 )
192 })
193 .collect(),
194 ),
195 };
196
197 stmt::Query::new_select(self.id, filter)
198 }
199
200 /// Iterate over the fields used for the model's primary key.
201 pub fn primary_key_fields(&self) -> impl ExactSizeIterator<Item = &'_ Field> {
202 self.primary_key
203 .fields
204 .iter()
205 .map(|pk_field| &self.fields[pk_field.index])
206 }
207
208 /// Returns the versionable field, if one is defined on this model.
209 pub fn version_field(&self) -> Option<&Field> {
210 self.version_field.map(|id| &self.fields[id.index])
211 }
212
213 /// Looks up a field by its application-level name.
214 ///
215 /// Returns `None` if no field with that name exists on this model.
216 pub fn field_by_name(&self, name: &str) -> Option<&Field> {
217 self.fields
218 .iter()
219 .find(|field| field.name.app.as_deref() == Some(name))
220 }
221
222 pub(crate) fn verify(&self, db: &driver::Capability) -> Result<()> {
223 for field in &self.fields {
224 field.verify(db)?;
225
226 // Multi-step (`via`) relations lower to nested `IN` subqueries.
227 // Only SQL drivers can evaluate them today; key-value drivers
228 // would need a separate per-step batched fetch strategy that
229 // is not yet implemented.
230 if matches!(&field.ty, FieldTy::Via(_)) && !db.sql {
231 return Err(crate::Error::invalid_schema(format!(
232 "field `{}::{}` declares a multi-step `via` relation, which \
233 requires a SQL-capable driver; the configured driver does not \
234 support SQL",
235 self.name.upper_camel_case(),
236 field.name,
237 )));
238 }
239 }
240 Ok(())
241 }
242}
243
244/// An embedded struct model whose fields are flattened into its parent model's
245/// database table.
246///
247/// Embedded structs do not have their own table or primary key. Their fields
248/// become additional columns in the parent table. Indices declared on an
249/// embedded struct's fields are propagated to physical DB indices on the parent
250/// table.
251///
252/// # Examples
253///
254/// ```ignore
255/// let embedded = model.as_embedded_struct_unwrap();
256/// for field in &embedded.fields {
257/// println!(" embedded field: {}", field.name);
258/// }
259/// ```
260#[derive(Debug, Clone)]
261pub struct EmbeddedStruct {
262 /// Uniquely identifies this model within the schema.
263 pub id: ModelId,
264
265 /// The model's name.
266 pub name: Name,
267
268 /// Fields contained by this embedded struct.
269 pub fields: Vec<Field>,
270
271 /// Indices defined on this embedded struct's fields.
272 ///
273 /// These reference fields within this embedded struct (not the parent
274 /// model). The schema builder propagates them to physical DB indices on
275 /// the parent table's flattened columns.
276 pub indices: Vec<Index>,
277}
278
279impl EmbeddedStruct {
280 pub(crate) fn verify(&self, db: &driver::Capability) -> Result<()> {
281 for field in &self.fields {
282 field.verify(db)?;
283 }
284 Ok(())
285 }
286}
287
288/// An embedded enum model stored in the parent table via a discriminant column
289/// and optional per-variant data columns.
290///
291/// The discriminant column holds a value (integer or string) identifying the active variant.
292/// Variants may optionally carry data fields, which are stored as additional
293/// nullable columns in the parent table.
294///
295/// # Examples
296///
297/// ```ignore
298/// let ee = model.as_embedded_enum_unwrap();
299/// for variant in &ee.variants {
300/// println!("variant {} = {}", variant.name.upper_camel_case(), variant.discriminant);
301/// }
302/// ```
303#[derive(Debug, Clone)]
304pub struct EmbeddedEnum {
305 /// Uniquely identifies this model within the schema.
306 pub id: ModelId,
307
308 /// The model's name.
309 pub name: Name,
310
311 /// The primitive type used for the discriminant column.
312 pub discriminant: FieldPrimitive,
313
314 /// The enum's variants.
315 pub variants: Vec<EnumVariant>,
316
317 /// All fields across all variants, with global indices. Each field's
318 /// [`variant`](Field::variant) identifies which variant it belongs to.
319 pub fields: Vec<Field>,
320
321 /// Indices defined on this embedded enum's variant fields.
322 ///
323 /// These reference fields within this embedded enum (not the parent
324 /// model). The schema builder propagates them to physical DB indices on
325 /// the parent table's flattened columns.
326 pub indices: Vec<Index>,
327}
328
329/// One variant of an [`EmbeddedEnum`].
330///
331/// Each variant has a name and a discriminant value (integer or string) that is
332/// stored in the database to identify which variant is active.
333#[derive(Debug, Clone)]
334pub struct EnumVariant {
335 /// The Rust variant name.
336 pub name: Name,
337
338 /// The discriminant value stored in the database column.
339 /// Typically `Value::I64` for integer discriminants or `Value::String` for
340 /// string discriminants.
341 pub discriminant: stmt::Value,
342}
343
344impl EmbeddedEnum {
345 /// Returns true if at least one variant carries data fields.
346 pub fn has_data_variants(&self) -> bool {
347 !self.fields.is_empty()
348 }
349
350 /// Returns fields belonging to a specific variant.
351 pub fn variant_fields(&self, variant_index: usize) -> impl Iterator<Item = &Field> {
352 let variant_id = VariantId {
353 model: self.id,
354 index: variant_index,
355 };
356 self.fields
357 .iter()
358 .filter(move |f| f.variant == Some(variant_id))
359 }
360
361 pub(crate) fn verify(&self, db: &driver::Capability) -> Result<()> {
362 for field in &self.fields {
363 field.verify(db)?;
364 }
365 Ok(())
366 }
367}
368
369/// Uniquely identifies a [`Model`] within a [`Schema`](super::Schema).
370///
371/// `ModelId` wraps a `usize` index into the schema's model map. It is `Copy`
372/// and can be used as a key for lookups.
373///
374/// # Examples
375///
376/// ```
377/// use toasty_core::schema::app::ModelId;
378///
379/// let id = ModelId(0);
380/// let field_id = id.field(2);
381/// assert_eq!(field_id.model, id);
382/// assert_eq!(field_id.index, 2);
383/// ```
384#[derive(Copy, Clone, Eq, PartialEq, Hash)]
385#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
386pub struct ModelId(pub usize);
387
388impl Model {
389 /// Returns this model's [`ModelId`].
390 pub fn id(&self) -> ModelId {
391 match self {
392 Model::Root(root) => root.id,
393 Model::EmbeddedStruct(embedded) => embedded.id,
394 Model::EmbeddedEnum(e) => e.id,
395 }
396 }
397
398 /// Returns a reference to this model's [`Name`].
399 pub fn name(&self) -> &Name {
400 match self {
401 Model::Root(root) => &root.name,
402 Model::EmbeddedStruct(embedded) => &embedded.name,
403 Model::EmbeddedEnum(e) => &e.name,
404 }
405 }
406
407 /// Returns true if this is a root model (has a table and primary key)
408 pub fn is_root(&self) -> bool {
409 matches!(self, Model::Root(_))
410 }
411
412 /// Returns true if this is an embedded model (flattened into parent)
413 pub fn is_embedded(&self) -> bool {
414 matches!(self, Model::EmbeddedStruct(_) | Model::EmbeddedEnum(_))
415 }
416
417 /// Returns true if this model can be the target of a relation
418 pub fn can_be_relation_target(&self) -> bool {
419 self.is_root()
420 }
421
422 /// Returns the inner [`ModelRoot`] if this is a root model.
423 pub fn as_root(&self) -> Option<&ModelRoot> {
424 match self {
425 Model::Root(root) => Some(root),
426 _ => None,
427 }
428 }
429
430 /// Returns a reference to the root model data.
431 ///
432 /// # Panics
433 ///
434 /// Panics if this is not a [`Model::Root`].
435 pub fn as_root_unwrap(&self) -> &ModelRoot {
436 match self {
437 Model::Root(root) => root,
438 Model::EmbeddedStruct(_) => panic!("expected root model, found embedded struct"),
439 Model::EmbeddedEnum(_) => panic!("expected root model, found embedded enum"),
440 }
441 }
442
443 /// Returns a mutable reference to the root model data.
444 ///
445 /// # Panics
446 ///
447 /// Panics if this is not a [`Model::Root`].
448 pub fn as_root_mut_unwrap(&mut self) -> &mut ModelRoot {
449 match self {
450 Model::Root(root) => root,
451 Model::EmbeddedStruct(_) => panic!("expected root model, found embedded struct"),
452 Model::EmbeddedEnum(_) => panic!("expected root model, found embedded enum"),
453 }
454 }
455
456 /// Returns a reference to the embedded struct data.
457 ///
458 /// # Panics
459 ///
460 /// Panics if this is not a [`Model::EmbeddedStruct`].
461 pub fn as_embedded_struct_unwrap(&self) -> &EmbeddedStruct {
462 match self {
463 Model::EmbeddedStruct(embedded) => embedded,
464 Model::Root(_) => panic!("expected embedded struct, found root model"),
465 Model::EmbeddedEnum(_) => panic!("expected embedded struct, found embedded enum"),
466 }
467 }
468
469 /// Returns a reference to the embedded enum data.
470 ///
471 /// # Panics
472 ///
473 /// Panics if this is not a [`Model::EmbeddedEnum`].
474 pub fn as_embedded_enum_unwrap(&self) -> &EmbeddedEnum {
475 match self {
476 Model::EmbeddedEnum(e) => e,
477 Model::Root(_) => panic!("expected embedded enum, found root model"),
478 Model::EmbeddedStruct(_) => panic!("expected embedded enum, found embedded struct"),
479 }
480 }
481
482 pub(crate) fn verify(&self, db: &driver::Capability) -> Result<()> {
483 match self {
484 Model::Root(root) => root.verify(db),
485 Model::EmbeddedStruct(embedded) => embedded.verify(db),
486 Model::EmbeddedEnum(e) => e.verify(db),
487 }
488 }
489}
490
491/// Identifies a specific variant within an [`EmbeddedEnum`] model.
492///
493/// # Examples
494///
495/// ```
496/// use toasty_core::schema::app::ModelId;
497///
498/// let variant_id = ModelId(1).variant(0);
499/// assert_eq!(variant_id.model, ModelId(1));
500/// assert_eq!(variant_id.index, 0);
501/// ```
502#[derive(Copy, Clone, PartialEq, Eq, Hash)]
503pub struct VariantId {
504 /// The enum model this variant belongs to.
505 pub model: ModelId,
506 /// Index of the variant within `EmbeddedEnum::variants`.
507 pub index: usize,
508}
509
510impl fmt::Debug for VariantId {
511 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
512 write!(fmt, "VariantId({}/{})", self.model.0, self.index)
513 }
514}
515
516impl ModelId {
517 /// Create a `FieldId` representing the current model's field at index
518 /// `index`.
519 pub const fn field(self, index: usize) -> FieldId {
520 FieldId { model: self, index }
521 }
522
523 /// Create a `VariantId` representing the current model's variant at
524 /// `index`.
525 pub const fn variant(self, index: usize) -> VariantId {
526 VariantId { model: self, index }
527 }
528
529 pub(crate) const fn placeholder() -> Self {
530 Self(usize::MAX)
531 }
532}
533
534impl From<&Self> for ModelId {
535 fn from(src: &Self) -> Self {
536 *src
537 }
538}
539
540impl From<&mut Self> for ModelId {
541 fn from(src: &mut Self) -> Self {
542 *src
543 }
544}
545
546impl From<&Model> for ModelId {
547 fn from(value: &Model) -> Self {
548 value.id()
549 }
550}
551
552impl From<&ModelRoot> for ModelId {
553 fn from(value: &ModelRoot) -> Self {
554 value.id
555 }
556}
557
558impl fmt::Debug for ModelId {
559 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
560 write!(fmt, "ModelId({})", self.0)
561 }
562}