toasty_core/schema/app/embedded.rs
1use crate::{
2 schema::app::{Model, ModelId, Schema},
3 stmt,
4};
5
6/// A reference to an embedded model (struct or enum) that is stored inline
7/// within its parent model's table rather than in a separate table.
8///
9/// Embedded fields are flattened into the parent table's columns at the
10/// database level, but appear as nested types at the application level.
11///
12/// # Examples
13///
14/// ```ignore
15/// use toasty_core::schema::app::Embedded;
16///
17/// // Embedded is typically constructed by the schema builder.
18/// let embedded: &Embedded = field.ty.as_embedded_unwrap();
19/// let target_model = embedded.target(&schema);
20/// ```
21#[derive(Debug, Clone)]
22pub struct Embedded {
23 /// The [`ModelId`] of the embedded model being referenced.
24 pub target: ModelId,
25
26 /// The expression type of this embedded field from the application's
27 /// perspective.
28 pub expr_ty: stmt::Type,
29}
30
31impl Embedded {
32 /// Resolves the target [`Model`] from the given schema.
33 pub fn target<'a>(&self, schema: &'a Schema) -> &'a Model {
34 schema.model(self.target)
35 }
36}