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