1extern crate self as icydb_model;
8
9pub mod application;
10pub mod base;
11pub mod build;
12pub mod error;
13pub mod fragment;
14#[cfg_attr(
17 target_arch = "wasm32",
18 expect(
19 dead_code,
20 reason = "Wasm retains declaration nodes while host code owns whole-graph traversal"
21 )
22)]
23pub mod node;
24pub mod normalize;
25#[cfg_attr(
26 target_arch = "wasm32",
27 expect(dead_code, reason = "whole-graph schema validation is host-owned")
28)]
29mod schema_validate;
30mod typed_adapter;
31pub mod types;
32pub mod validate;
33#[cfg_attr(
34 target_arch = "wasm32",
35 expect(
36 dead_code,
37 reason = "Wasm retains declarations while host code owns graph visitation"
38 )
39)]
40mod visit;
41pub mod visitor;
42
43pub const MAX_ENTITY_NAME_LEN: usize = 64;
45
46pub const MAX_FIELD_NAME_LEN: usize = 64;
48
49pub const MAX_INDEX_FIELDS: usize = 4;
51
52pub const MAX_INDEX_NAME_LEN: usize =
54 MAX_ENTITY_NAME_LEN + (MAX_INDEX_FIELDS * (1 + MAX_FIELD_NAME_LEN));
55
56use crate::{build::BuildError, node::NodeError};
57use thiserror::Error as ThisError;
58
59pub mod prelude {
61 pub(crate) use crate::build::schema_read;
62 pub use crate::{
63 EntitySource as _, Inner as _, NormalizeAndValidate as _, Path as _, base, canister,
64 entity, enum_, err,
65 error::ErrorTree,
66 list, map, newtype,
67 node::*,
68 normalizer, record,
69 schema::*,
70 set, store, tuple,
71 types::{Cardinality, Primitive},
72 validator,
73 visitor::{
74 Issue, Normalize as _, NormalizeAuto, NormalizeCustom, Normalizer as _, Validate as _,
75 ValidateAuto, ValidateCustom, Validator as _, Visitable as _, VisitorContext,
76 },
77 };
78 pub(crate) use crate::{
79 node::{MacroNode, ValidateNode, VisitableNode},
80 visit::Visitor,
81 };
82 pub use candid::CandidType;
83 pub use serde::{Deserialize, Serialize};
84}
85
86pub use application::NormalizeAndValidate;
87pub use icydb_model_macros::{
88 Add, AddAssign, Deref, DerefMut, Display, Div, DivAssign, Inner, Mul, MulAssign, Neg, Product,
89 Rem, RemAssign, Sub, SubAssign, Sum, canister, entity, enum_, list, map, newtype, normalizer,
90 record, set, store, tuple, validator,
91};
92pub use normalize::normalize;
93#[doc(hidden)]
94pub use typed_adapter::{
95 TypedAdapterContext, TypedEnumDescriptor, TypedEnumSelection, TypedInputValue, TypedNamedType,
96 TypedOutputValue, TypedScalarValue, TypedValueError,
97};
98pub use validate::validate;
99
100pub trait Path {
102 const PATH: &'static str;
104}
105
106pub trait EntitySource {
112 const ENTITY: &'static str;
114}
115
116pub trait Inner<T> {
118 fn inner(&self) -> &T;
120
121 fn into_inner(self) -> T;
123}
124
125pub mod schema {
127 pub use icydb_schema::*;
128}
129
130#[doc(hidden)]
132pub mod __reexports {
133 pub use candid;
134 #[cfg(not(target_arch = "wasm32"))]
135 pub use ctor;
136 pub use icydb_model_macros;
137 pub use remain;
138 pub use serde;
139}
140
141#[derive(Debug, ThisError)]
148pub enum Error {
149 #[error(transparent)]
150 BuildError(#[from] BuildError),
151
152 #[error(transparent)]
153 NodeError(#[from] NodeError),
154}
155
156#[cfg(test)]
161mod tests {
162 use super::{Error, build::BuildError, error::ErrorTree, node::NodeError};
163
164 #[test]
165 fn build_errors_remain_in_build_boundary() {
166 let schema_error = Error::from(BuildError::Validation(ErrorTree::from(
167 "missing schema relation target",
168 )));
169
170 match schema_error {
171 Error::BuildError(BuildError::Validation(tree)) => {
172 assert!(
173 tree.messages()
174 .iter()
175 .any(|message| message == "missing schema relation target"),
176 "build validation errors must remain wrapped as build-boundary failures",
177 );
178 }
179 Error::BuildError(BuildError::Graph(error)) => {
180 panic!("unexpected graph error: {error}");
181 }
182 Error::NodeError(_) => {
183 panic!("build validation failures must not be remapped into node-boundary errors");
184 }
185 }
186 }
187
188 #[test]
189 fn node_errors_remain_in_node_boundary() {
190 let schema_error = Error::from(NodeError::PathNotFound("entity.user_id".to_string()));
191
192 match schema_error {
193 Error::NodeError(NodeError::PathNotFound(path)) => {
194 assert_eq!(path, "entity.user_id");
195 }
196 Error::NodeError(NodeError::IncorrectNodeType(path)) => {
197 panic!("unexpected node error kind after conversion for path {path}");
198 }
199 Error::BuildError(_) => {
200 panic!("node errors must not be remapped into build-boundary failures");
201 }
202 }
203 }
204}