Skip to main content

icydb_core/traits/
mod.rs

1#[macro_use]
2mod macros;
3mod atomic;
4mod view;
5mod visitor;
6
7pub use atomic::*;
8pub use view::*;
9pub use visitor::*;
10
11// -----------------------------------------------------------------------------
12// Standard re-exports for `traits::X` ergonomics
13// -----------------------------------------------------------------------------
14
15pub use canic_cdk::structures::storable::Storable;
16pub use num_traits::{FromPrimitive as NumFromPrimitive, NumCast, ToPrimitive as NumToPrimitive};
17pub use serde::{Deserialize, Serialize, de::DeserializeOwned};
18pub use std::{
19    cmp::{Eq, Ordering, PartialEq},
20    convert::From,
21    default::Default,
22    fmt::Debug,
23    hash::Hash,
24    ops::{Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign},
25};
26
27use crate::{prelude::*, types::Id, value::ValueEnum, visitor::VisitorContext};
28
29// ============================================================================
30// FOUNDATIONAL KINDS
31// ============================================================================
32//
33// These traits define *where* something lives in the system,
34// not what data it contains.
35//
36
37///
38/// Path
39/// Fully-qualified schema path.
40///
41
42pub trait Path {
43    const PATH: &'static str;
44}
45
46/// Marker for all schema/runtime nodes.
47pub trait Kind: Path + 'static {}
48impl<T> Kind for T where T: Path + 'static {}
49
50/// Marker for canister namespaces.
51pub trait CanisterKind: Kind {}
52
53/// Marker for data stores bound to a canister.
54pub trait StoreKind: Kind {
55    type Canister: CanisterKind;
56}
57
58// ============================================================================
59// ENTITY IDENTITY & SCHEMA
60// ============================================================================
61//
62// These traits describe *what an entity is*, not how it is stored
63// or manipulated at runtime.
64//
65
66///
67/// EntityKey
68///
69/// Associates an entity with the primitive type used as its primary key.
70///
71/// ## Semantics
72/// - Implemented for entity types
73/// - `Self::Key` is the *storage representation* of the primary key
74/// - Keys are plain values (Ulid, u64, Principal, …)
75/// - Typed identity is provided by `Id<Self>`, not by the key itself
76/// - Keys are public identifiers and are never authority-bearing capabilities
77///
78
79pub trait EntityKey {
80    type Key: Copy + Debug + Eq + Ord + FieldValue + EntityKeyBytes + 'static;
81}
82
83///
84/// EntityKeyBytes
85///
86
87pub trait EntityKeyBytes {
88    /// Exact number of bytes produced.
89    const BYTE_LEN: usize;
90
91    /// Write bytes into the provided buffer.
92    fn write_bytes(&self, out: &mut [u8]);
93}
94
95macro_rules! impl_entity_key_bytes_numeric {
96    ($($ty:ty),* $(,)?) => {
97        $(
98            impl EntityKeyBytes for $ty {
99                const BYTE_LEN: usize = ::core::mem::size_of::<Self>();
100
101                fn write_bytes(&self, out: &mut [u8]) {
102                    assert_eq!(out.len(), Self::BYTE_LEN);
103                    out.copy_from_slice(&self.to_be_bytes());
104                }
105            }
106        )*
107    };
108}
109
110impl_entity_key_bytes_numeric!(i8, i16, i32, i64, u8, u16, u32, u64);
111
112impl EntityKeyBytes for () {
113    const BYTE_LEN: usize = 0;
114
115    fn write_bytes(&self, out: &mut [u8]) {
116        assert_eq!(out.len(), Self::BYTE_LEN);
117    }
118}
119
120///
121/// EntityIdentity
122///
123/// Semantic primary-key metadata about an entity.
124///
125/// These constants name identity metadata only. They do not imply trust, ownership,
126/// authorization, or existence.
127///
128
129pub trait EntityIdentity: EntityKey {
130    const ENTITY_NAME: &'static str;
131    const PRIMARY_KEY: &'static str;
132}
133
134///
135/// EntitySchema
136///
137/// Declared schema facts for an entity.
138///
139
140pub trait EntitySchema: EntityIdentity {
141    const MODEL: &'static EntityModel;
142    const FIELDS: &'static [&'static str];
143    const INDEXES: &'static [&'static IndexModel];
144}
145
146// ============================================================================
147// ENTITY RUNTIME COMPOSITION
148// ============================================================================
149//
150// These traits bind schema-defined entities into runtime placement.
151//
152
153///
154/// EntityPlacement
155///
156/// Runtime placement of an entity
157///
158
159pub trait EntityPlacement {
160    type Store: StoreKind;
161    type Canister: CanisterKind;
162}
163
164///
165/// EntityKind
166///
167/// Fully runtime-bound entity.
168///
169/// This is the *maximum* entity contract and should only be
170/// required by code that actually touches storage or execution.
171///
172
173pub trait EntityKind: EntitySchema + EntityPlacement + Kind + TypeKind {}
174
175// ============================================================================
176// ENTITY VALUES
177// ============================================================================
178//
179// These traits describe *instances* of entities.
180//
181
182///
183/// EntityValue
184///
185/// A concrete entity value that can present a typed identity at boundaries.
186///
187/// Implementors store primitive key material internally.
188/// `id()` constructs a typed `Id<Self>` view on demand.
189/// The returned `Id<Self>` is a public identifier, not proof of authority.
190///
191
192pub trait EntityValue: EntityIdentity + FieldValues + Sized {
193    fn id(&self) -> Id<Self>;
194}
195
196/// Marker for entities with exactly one logical row.
197pub trait SingletonEntity: EntityValue {}
198
199///
200// ============================================================================
201// TYPE SYSTEM CONTRACTS
202// ============================================================================
203//
204// These traits define behavioral expectations for schema-defined types.
205//
206
207///
208/// TypeKind
209///
210/// Any schema-defined data type.
211///
212/// This is a *strong* contract and should only be required
213/// where full lifecycle semantics are needed.
214///
215
216pub trait TypeKind:
217    Kind
218    + AsView
219    + Clone
220    + Default
221    + Serialize
222    + DeserializeOwned
223    + Sanitize
224    + Validate
225    + Visitable
226    + PartialEq
227{
228}
229
230impl<T> TypeKind for T where
231    T: Kind
232        + AsView
233        + Clone
234        + Default
235        + DeserializeOwned
236        + PartialEq
237        + Serialize
238        + Sanitize
239        + Validate
240        + Visitable
241{
242}
243
244/// ============================================================================
245/// QUERY VALUE BOUNDARIES
246/// ============================================================================
247
248///
249/// Collection
250///
251/// Explicit iteration contract for list/set wrapper types.
252/// Avoids implicit deref-based access to inner collections.
253///
254
255pub trait Collection {
256    type Item;
257
258    /// Iterator over the collection's items, tied to the borrow of `self`.
259    type Iter<'a>: Iterator<Item = &'a Self::Item> + 'a
260    where
261        Self: 'a;
262
263    /// Returns an iterator over the collection's items.
264    fn iter(&self) -> Self::Iter<'_>;
265
266    /// Returns the number of items in the collection.
267    fn len(&self) -> usize;
268
269    /// Returns true if the collection contains no items.
270    fn is_empty(&self) -> bool {
271        self.len() == 0
272    }
273}
274
275///
276/// MapCollection
277///
278/// Explicit iteration contract for map wrapper types.
279/// Avoids implicit deref-based access to inner collections.
280///
281
282pub trait MapCollection {
283    type Key;
284    type Value;
285
286    /// Iterator over the map's key/value pairs, tied to the borrow of `self`.
287    type Iter<'a>: Iterator<Item = (&'a Self::Key, &'a Self::Value)> + 'a
288    where
289        Self: 'a;
290
291    /// Returns an iterator over the map's key/value pairs.
292    fn iter(&self) -> Self::Iter<'_>;
293
294    /// Returns the number of entries in the map.
295    fn len(&self) -> usize;
296
297    /// Returns true if the map contains no entries.
298    fn is_empty(&self) -> bool {
299        self.len() == 0
300    }
301}
302
303pub trait EnumValue {
304    fn to_value_enum(&self) -> ValueEnum;
305}
306
307pub trait FieldValues {
308    /// Resolve one field value by field name.
309    fn get_value(&self, field: &str) -> Option<Value>;
310
311    /// Resolve one field value by stable field slot index.
312    fn get_value_by_index(&self, index: usize) -> Option<Value>;
313}
314
315///
316/// FieldValueKind
317///
318/// Schema affordance classification for query planning and validation.
319/// Describes whether a field is planner-addressable and predicate-queryable.
320///
321#[derive(Clone, Copy, Debug, Eq, PartialEq)]
322pub enum FieldValueKind {
323    /// Planner-addressable atomic value.
324    Atomic,
325
326    /// Structured value with known internal fields that the planner
327    /// does not reason about as an addressable query target.
328    Structured {
329        /// Whether predicates may be expressed against this field.
330        queryable: bool,
331    },
332}
333
334impl FieldValueKind {
335    #[must_use]
336    pub const fn is_queryable(self) -> bool {
337        match self {
338            Self::Atomic => true,
339            Self::Structured { queryable } => queryable,
340        }
341    }
342}
343
344///
345/// FieldValue
346///
347/// Conversion boundary for values used in query predicates.
348///
349/// Represents values that can appear on the *right-hand side* of predicates.
350///
351
352pub trait FieldValue {
353    fn kind() -> FieldValueKind
354    where
355        Self: Sized;
356
357    fn to_value(&self) -> Value;
358
359    #[must_use]
360    fn from_value(value: &Value) -> Option<Self>
361    where
362        Self: Sized;
363}
364
365impl FieldValue for &str {
366    fn kind() -> FieldValueKind {
367        FieldValueKind::Atomic
368    }
369
370    fn to_value(&self) -> Value {
371        Value::Text((*self).to_string())
372    }
373
374    fn from_value(_value: &Value) -> Option<Self> {
375        None
376    }
377}
378
379impl FieldValue for String {
380    fn kind() -> FieldValueKind {
381        FieldValueKind::Atomic
382    }
383
384    fn to_value(&self) -> Value {
385        Value::Text(self.clone())
386    }
387
388    fn from_value(value: &Value) -> Option<Self> {
389        match value {
390            Value::Text(v) => Some(v.clone()),
391            _ => None,
392        }
393    }
394}
395
396impl<T: FieldValue> FieldValue for Option<T> {
397    fn kind() -> FieldValueKind {
398        T::kind()
399    }
400
401    fn to_value(&self) -> Value {
402        match self {
403            Some(v) => v.to_value(),
404            None => Value::Null,
405        }
406    }
407
408    fn from_value(value: &Value) -> Option<Self> {
409        if matches!(value, Value::Null) {
410            return Some(None);
411        }
412
413        T::from_value(value).map(Some)
414    }
415}
416
417impl<T: FieldValue> FieldValue for Box<T> {
418    fn kind() -> FieldValueKind {
419        T::kind()
420    }
421
422    fn to_value(&self) -> Value {
423        (**self).to_value()
424    }
425
426    fn from_value(value: &Value) -> Option<Self> {
427        T::from_value(value).map(Self::new)
428    }
429}
430
431impl<T: FieldValue> FieldValue for Vec<Box<T>> {
432    fn kind() -> FieldValueKind {
433        FieldValueKind::Structured { queryable: true }
434    }
435
436    fn to_value(&self) -> Value {
437        Value::List(self.iter().map(FieldValue::to_value).collect())
438    }
439
440    fn from_value(value: &Value) -> Option<Self> {
441        let Value::List(items) = value else {
442            return None;
443        };
444
445        let mut out = Self::with_capacity(items.len());
446        for item in items {
447            out.push(Box::new(T::from_value(item)?));
448        }
449
450        Some(out)
451    }
452}
453
454// impl_field_value
455#[macro_export]
456macro_rules! impl_field_value {
457    ( $( $type:ty => $variant:ident ),* $(,)? ) => {
458        $(
459            impl FieldValue for $type {
460                fn kind() -> FieldValueKind {
461                    FieldValueKind::Atomic
462                }
463
464                fn to_value(&self) -> Value {
465                    Value::$variant((*self).into())
466                }
467
468                fn from_value(value: &Value) -> Option<Self> {
469                    match value {
470                        Value::$variant(v) => (*v).try_into().ok(),
471                        _ => None,
472                    }
473                }
474            }
475        )*
476    };
477}
478
479impl_field_value!(
480    i8 => Int,
481    i16 => Int,
482    i32 => Int,
483    i64 => Int,
484    u8 => Uint,
485    u16 => Uint,
486    u32 => Uint,
487    u64 => Uint,
488    bool => Bool,
489);
490
491/// ============================================================================
492/// MISC HELPERS
493/// ============================================================================
494
495///
496/// Inner
497///
498/// For newtypes to expose their innermost value.
499///
500pub trait Inner<T> {
501    fn inner(&self) -> &T;
502    fn into_inner(self) -> T;
503}
504
505impl<T> Inner<T> for T
506where
507    T: Atomic,
508{
509    fn inner(&self) -> &T {
510        self
511    }
512
513    fn into_inner(self) -> T {
514        self
515    }
516}
517
518/// ============================================================================
519/// SANITIZATION / VALIDATION
520/// ============================================================================
521
522///
523/// Sanitizer
524///
525/// Transforms a value into a sanitized version.
526///
527pub trait Sanitizer<T> {
528    fn sanitize(&self, value: &mut T) -> Result<(), String>;
529}
530
531///
532/// Validator
533///
534/// Allows a node to validate values.
535///
536pub trait Validator<T: ?Sized> {
537    fn validate(&self, value: &T, ctx: &mut dyn VisitorContext);
538}