Skip to main content

icydb_core/db/store/
entity_ref.rs

1use crate::{
2    db::store::{DataKey, StorageKey},
3    value::Value,
4};
5
6///
7/// EntityRef
8///
9/// Concrete reference extracted from an entity instance.
10/// Carries the target entity path and the referenced key value.
11/// Produced by [`EntityReferences`] during pre-commit planning.
12///
13
14#[derive(Clone, Debug, Eq, PartialEq)]
15pub struct EntityRef {
16    pub target_path: &'static str,
17    key: StorageKey,
18}
19
20impl EntityRef {
21    #[must_use]
22    pub const fn from_storage_key(target_path: &'static str, key: StorageKey) -> Self {
23        Self { target_path, key }
24    }
25
26    /// Returns true if this reference points at the given data key.
27    ///
28    /// Invariant:
29    /// - `target_path` must already have been validated
30    /// - comparison is storage-key–level only
31    #[must_use]
32    pub fn matches_data_key(&self, data_key: &DataKey) -> bool {
33        data_key.storage_key() == self.key
34    }
35
36    /// Expose the referenced storage key (for diagnostics / logging).
37    #[must_use]
38    pub const fn storage_key(&self) -> StorageKey {
39        self.key
40    }
41
42    /// Return the referenced key as a semantic Value.
43    ///
44    /// This is the ONLY semantic accessor.
45    #[must_use]
46    pub const fn value(&self) -> Value {
47        self.key.as_value()
48    }
49}