icydb_core/model/entity.rs
1//! Module: model::entity
2//! Responsibility: module-local ownership and contracts for model::entity.
3//! Does not own: cross-module orchestration outside this module.
4//! Boundary: exposes this module API while keeping implementation details internal.
5
6//! Runtime-only entity schema surface generated by macros.
7//!
8//! This model intentionally includes only:
9//! - entity metadata (path + stable external name)
10//! - ordered fields
11//! - primary key field
12//! - index definitions
13//!
14//! It intentionally excludes:
15//! - validators/sanitizers/defaults
16//! - relations or full schema graphs
17//! - JSON schema ingestion or global registries
18//!
19//! Stability: this is the authoritative runtime contract for planning and
20//! execution. Additive changes are expected; breaking changes require a
21//! coordinated version bump across the engine.
22//!
23//! Field names are entity-scoped. Callers that combine entities must
24//! namespace by entity at the call site.
25
26use crate::model::{field::FieldModel, index::IndexModel};
27
28///
29/// EntityModel
30///
31/// Macro-generated runtime schema snapshot for a single entity.
32/// The planner and predicate validator consume this model directly.
33///
34
35#[derive(Debug)]
36pub struct EntityModel {
37 /// Fully-qualified Rust type path (for diagnostics).
38 pub(crate) path: &'static str,
39
40 /// Stable external name used in keys and routing.
41 pub(crate) entity_name: &'static str,
42
43 /// Primary key field (points at an entry in `fields`).
44 pub(crate) primary_key: &'static FieldModel,
45
46 /// Ordered field list (authoritative for runtime planning).
47 pub(crate) fields: &'static [FieldModel],
48
49 /// Index definitions (field order is significant).
50 pub(crate) indexes: &'static [&'static IndexModel],
51}
52
53impl EntityModel {
54 /// Build one runtime entity schema descriptor.
55 #[must_use]
56 pub const fn new(
57 path: &'static str,
58 entity_name: &'static str,
59 primary_key: &'static FieldModel,
60 fields: &'static [FieldModel],
61 indexes: &'static [&'static IndexModel],
62 ) -> Self {
63 Self {
64 path,
65 entity_name,
66 primary_key,
67 fields,
68 indexes,
69 }
70 }
71
72 /// Return the fully-qualified Rust path for this entity.
73 #[must_use]
74 pub const fn path(&self) -> &'static str {
75 self.path
76 }
77
78 /// Return the stable external entity name.
79 #[must_use]
80 pub const fn entity_name(&self) -> &'static str {
81 self.entity_name
82 }
83
84 /// Return the primary-key field descriptor.
85 #[must_use]
86 pub const fn primary_key(&self) -> &'static FieldModel {
87 self.primary_key
88 }
89
90 /// Return the ordered runtime field descriptors.
91 #[must_use]
92 pub const fn fields(&self) -> &'static [FieldModel] {
93 self.fields
94 }
95
96 /// Return the runtime index descriptors.
97 #[must_use]
98 pub const fn indexes(&self) -> &'static [&'static IndexModel] {
99 self.indexes
100 }
101}
102
103/// Resolve one schema field name into its stable slot index.
104#[must_use]
105pub(crate) fn resolve_field_slot(model: &EntityModel, field_name: &str) -> Option<usize> {
106 model
107 .fields
108 .iter()
109 .position(|field| field.name == field_name)
110}
111
112/// Resolve the primary-key field into its stable slot index.
113#[must_use]
114pub(crate) fn resolve_primary_key_slot(model: &EntityModel) -> Option<usize> {
115 model
116 .fields
117 .iter()
118 .position(|field| std::ptr::eq(field, model.primary_key))
119}