toasty_core/schema/mapping.rs
1mod field;
2pub use field::{EnumVariant, Field, FieldEnum, FieldPrimitive, FieldRelation, FieldStruct};
3
4mod model;
5pub use model::{Model, TableToModel};
6
7use super::app::ModelId;
8use indexmap::IndexMap;
9
10/// Defines the correspondence between app-level models and database-level
11/// tables.
12///
13/// The mapping is constructed during schema building and remains immutable at
14/// runtime. It provides the translation layer that enables the query engine to
15/// convert model-oriented statements into table-oriented statements during the
16/// lowering phase.
17#[derive(Debug, Clone)]
18pub struct Mapping {
19 /// Per-model mappings indexed by model identifier.
20 pub models: IndexMap<ModelId, Model>,
21}
22
23impl Mapping {
24 /// Returns the mapping for the specified model.
25 ///
26 /// # Panics
27 ///
28 /// Panics if the model ID does not exist in the mapping.
29 pub fn model(&self, id: impl Into<ModelId>) -> &Model {
30 self.models.get(&id.into()).expect("invalid model ID")
31 }
32
33 /// Returns a mutable reference to the mapping for the specified model.
34 ///
35 /// # Panics
36 ///
37 /// Panics if the model ID does not exist in the mapping.
38 pub fn model_mut(&mut self, id: impl Into<ModelId>) -> &mut Model {
39 self.models.get_mut(&id.into()).expect("invalid model ID")
40 }
41}