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