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 hash;
5pub mod index;
6pub mod interface;
7pub mod key;
8pub mod macros;
9pub mod obs;
10pub mod serialize;
11pub mod traits;
12pub mod types;
13pub mod value;
14pub mod view;
15pub mod visitor;
16
17pub use index::IndexSpec;
18pub use key::Key;
19pub use serialize::{deserialize, serialize};
20pub use value::Value;
21
22///
23/// CONSTANTS
24///
25
26/// Maximum number of indexed fields allowed on an entity.
27///
28/// This limit keeps hashed index keys within bounded, storable sizes and
29/// simplifies sizing tests in the stores.
30pub const MAX_INDEX_FIELDS: usize = 4;
31
32use candid::CandidType;
33use serde::{Deserialize, Serialize};
34use thiserror::Error as ThisError;
35use traits::Visitable;
36use visitor::VisitorIssues;
37
38///
39/// Error
40///
41/// top level error should handle all sub-errors, but not expose the candid types
42/// as that would be a lot of them
43///
44
45#[derive(CandidType, Debug, Deserialize, Serialize, ThisError)]
46pub enum Error {
47    #[error("{0}")]
48    DbError(String),
49
50    #[error("{0}")]
51    InterfaceError(String),
52
53    #[error("{0}")]
54    SanitizeError(VisitorIssues),
55
56    #[error("{0}")]
57    SerializeError(String),
58
59    #[error("{0}")]
60    ValidateError(VisitorIssues),
61}
62
63macro_rules! from_to_string {
64    ($from:ty, $variant:ident) => {
65        impl From<$from> for Error {
66            fn from(e: $from) -> Self {
67                Error::$variant(e.to_string())
68            }
69        }
70    };
71}
72
73from_to_string!(db::DbError, DbError);
74from_to_string!(interface::InterfaceError, InterfaceError);
75from_to_string!(serialize::SerializeError, SerializeError);
76
77/// sanitize
78pub fn sanitize(node: &mut dyn Visitable) -> Result<(), Error> {
79    visitor::sanitize(node).map_err(Error::SanitizeError)
80}
81
82/// validate
83pub fn validate(node: &dyn Visitable) -> Result<(), Error> {
84    visitor::validate(node).map_err(Error::ValidateError)
85}