icydb_core/
lib.rs

1//! Core runtime for IcyDB: entity traits, values, executors, visitors, and
2//! the ergonomics exported via the `prelude`.
3pub mod db;
4pub mod design;
5pub mod hash;
6pub mod index;
7pub mod interface;
8pub mod key;
9pub mod macros;
10pub mod obs;
11pub mod serialize;
12pub mod traits;
13pub mod types;
14pub mod value;
15pub mod view;
16pub mod visitor;
17
18pub use index::IndexSpec;
19pub use key::Key;
20pub use serialize::{deserialize, serialize};
21pub use value::Value;
22pub use visitor::{sanitize, validate};
23
24/// re-exports
25///
26/// macros can use these, stops the user having to specify all the dependencies
27/// in the Cargo.toml file manually
28///
29/// these have to be in icydb_core because of the base library not being able to import icydb
30pub mod __reexports {
31    pub use canic_cdk;
32    pub use canic_memory;
33    pub use canic_utils;
34    pub use ctor;
35    pub use derive_more;
36    pub use num_traits;
37    pub use remain;
38}
39
40///
41/// CONSTANTS
42///
43
44/// Maximum number of indexed fields allowed on an entity.
45///
46/// This limit keeps hashed index keys within bounded, storable sizes and
47/// simplifies sizing tests in the stores.
48pub const MAX_INDEX_FIELDS: usize = 4;
49
50///
51/// ICYDB ACTOR PRELUDE
52/// using _ brings traits into scope and avoids name conflicts
53///
54
55pub mod prelude {
56    pub use crate::{
57        db,
58        db::{
59            executor::SaveExecutor,
60            primitives::{
61                self, Cmp, FilterClause, FilterDsl, FilterExpr, FilterExt as _, LimitExpr,
62                LimitExt as _, SortExpr, SortExt as _,
63            },
64            query,
65            response::{Response, ResponseExt},
66        },
67        key::Key,
68        traits::{
69            CreateView as _, EntityKind as _, FilterView as _, Inner as _, Path as _,
70            UpdateView as _, View as _,
71        },
72        types::*,
73        value::Value,
74        view::{Create, Filter, Update, View},
75    };
76    pub use candid::CandidType;
77    pub use serde::{Deserialize, Serialize};
78}
79
80use candid::CandidType;
81use serde::{Deserialize, Serialize};
82use thiserror::Error as ThisError;
83
84///
85/// Error
86///
87/// top level error should handle all sub-errors, but not expose the candid types
88/// as that would be a lot of them
89///
90
91#[derive(CandidType, Debug, Deserialize, Serialize, ThisError)]
92pub enum Error {
93    #[error("{0}")]
94    DbError(String),
95
96    #[error("{0}")]
97    InterfaceError(String),
98
99    #[error("{0}")]
100    SerializeError(String),
101
102    #[error("{0}")]
103    VisitorError(String),
104}
105
106macro_rules! from_to_string {
107    ($from:ty, $variant:ident) => {
108        impl From<$from> for Error {
109            fn from(e: $from) -> Self {
110                Error::$variant(e.to_string())
111            }
112        }
113    };
114}
115
116from_to_string!(db::DbError, DbError);
117from_to_string!(interface::InterfaceError, InterfaceError);
118from_to_string!(serialize::SerializeError, SerializeError);
119from_to_string!(visitor::VisitorError, VisitorError);