Skip to main content

icydb_core/model/
entity.rs

1//! Runtime-only entity schema surface generated by macros.
2//!
3//! This model intentionally includes only:
4//! - entity metadata (path + stable external name)
5//! - ordered fields
6//! - primary key field
7//! - index definitions
8//!
9//! It intentionally excludes:
10//! - validators/sanitizers/defaults
11//! - relations or full schema graphs
12//! - JSON schema ingestion or global registries
13//!
14//! Stability: this is the authoritative runtime contract for planning and
15//! execution. Additive changes are expected; breaking changes require a
16//! coordinated version bump across the engine.
17//!
18//! Field names are entity-scoped. Callers that combine entities must
19//! namespace by entity at the call site.
20
21use crate::model::{field::FieldModel, index::IndexModel};
22
23///
24/// EntityModel
25///
26/// Macro-generated runtime schema snapshot for a single entity.
27/// The planner and predicate validator consume this model directly.
28///
29
30#[derive(Debug)]
31pub struct EntityModel {
32    /// Fully-qualified Rust type path (for diagnostics).
33    pub path: &'static str,
34
35    /// Stable external name used in keys and routing.
36    pub entity_name: &'static str,
37
38    /// Primary key field (points at an entry in `fields`).
39    pub primary_key: &'static FieldModel,
40
41    /// Ordered field list (authoritative for runtime planning).
42    pub fields: &'static [FieldModel],
43
44    /// Index definitions (field order is significant).
45    pub indexes: &'static [&'static IndexModel],
46}
47
48/// Resolve one schema field name into its stable slot index.
49#[must_use]
50pub(crate) fn resolve_field_slot(model: &EntityModel, field_name: &str) -> Option<usize> {
51    model
52        .fields
53        .iter()
54        .position(|field| field.name == field_name)
55}
56
57/// Resolve the primary-key field into its stable slot index.
58#[must_use]
59pub(crate) fn resolve_primary_key_slot(model: &EntityModel) -> Option<usize> {
60    model
61        .fields
62        .iter()
63        .position(|field| std::ptr::eq(field, model.primary_key))
64}