Skip to main content

icydb_model/
lib.rs

1//! IcyDB application-model authoring and code generation.
2//!
3//! This package owns application declarations, the opt-in host authoring graph,
4//! explicit application validation and normalization, and lowering into the
5//! public [`icydb_schema`] proposal contract. It is not database authority.
6
7extern crate self as icydb_model;
8
9pub mod application;
10pub mod base;
11#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
12pub mod build;
13pub mod error;
14#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
15pub mod fragment;
16#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
17pub mod node;
18pub mod normalize;
19#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
20mod schema_validate;
21mod typed_adapter;
22pub mod types;
23pub mod validate;
24#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
25mod visit;
26pub mod visitor;
27
28#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
29use crate::{build::BuildError, node::NodeError};
30#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
31use thiserror::Error as ThisError;
32
33/// Shared schema-building prelude used by validators, macros, and tests.
34pub mod prelude {
35    #[cfg(all(feature = "build", not(target_arch = "wasm32")))]
36    pub(crate) use crate::build::schema_read;
37    #[cfg(all(feature = "build", not(target_arch = "wasm32")))]
38    pub use crate::node::*;
39    pub use crate::{
40        EntitySource as _, Inner as _, NormalizeAndValidate as _, Path as _, base, canister,
41        entity, enum_, err,
42        error::ErrorTree,
43        list, map, newtype, normalizer, record,
44        schema::*,
45        set, store, tuple,
46        types::{Cardinality, Primitive},
47        validator,
48        visitor::{
49            Issue, Normalize as _, NormalizeAuto, NormalizeCustom, Normalizer as _, Validate as _,
50            ValidateAuto, ValidateCustom, Validator as _, Visitable as _, VisitorContext,
51        },
52    };
53    #[cfg(all(feature = "build", not(target_arch = "wasm32")))]
54    pub(crate) use crate::{
55        node::{MacroNode, ValidateNode, VisitableNode},
56        visit::Visitor,
57    };
58
59    pub use serde::{Deserialize, Serialize};
60}
61
62pub use application::NormalizeAndValidate;
63pub use icydb_model_macros::{
64    Add, AddAssign, Deref, DerefMut, Display, Div, DivAssign, Inner, Mul, MulAssign, Neg, Product,
65    Rem, RemAssign, Sub, SubAssign, Sum, canister, entity, enum_, list, map, newtype, normalizer,
66    record, set, store, tuple, validator,
67};
68pub use icydb_schema::MAX_INDEX_FIELDS;
69pub use normalize::normalize;
70#[doc(hidden)]
71pub use typed_adapter::{
72    TypedAdapterContext, TypedEnumDescriptor, TypedEnumSelection, TypedInputValue, TypedNamedType,
73    TypedOutputValue, TypedScalarValue, TypedValueError,
74};
75pub use validate::validate;
76
77/// Fully-qualified path identity for generated application declarations.
78pub trait Path {
79    /// Stable Rust declaration path.
80    const PATH: &'static str;
81}
82
83/// Schema-authored entity source identity emitted for runtime-enabled models.
84///
85/// This is application vocabulary only. It cannot establish accepted-schema
86/// presence, freshness, or authority; runtime APIs resolve the source against
87/// their pinned accepted snapshot.
88pub trait EntitySource {
89    /// Exact authored entity source name.
90    const ENTITY: &'static str;
91}
92
93/// Borrowed and consuming access to one-field application wrappers.
94pub trait Inner<T> {
95    /// Borrow the wrapped value.
96    fn inner(&self) -> &T;
97
98    /// Consume the wrapper and return its value.
99    fn into_inner(self) -> T;
100}
101
102/// Exact public proposal vocabulary consumed by application-model lowering.
103pub mod schema {
104    pub use icydb_schema::*;
105}
106
107/// Dependencies intentionally exposed to generated model code.
108#[doc(hidden)]
109pub mod __reexports {
110
111    #[cfg(all(feature = "build", not(target_arch = "wasm32")))]
112    pub use ctor;
113    pub use icydb_model_macros;
114    pub use remain;
115    pub use serde;
116}
117
118///
119/// Error
120///
121/// Top-level schema error boundary spanning build-time validation and node
122/// lookup/type errors.
123///
124#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
125#[derive(Debug, ThisError)]
126pub enum Error {
127    #[error(transparent)]
128    BuildError(#[from] BuildError),
129
130    #[error(transparent)]
131    NodeError(#[from] NodeError),
132}
133
134//
135// TESTS
136//
137
138#[cfg(all(test, feature = "build", not(target_arch = "wasm32")))]
139mod tests;
140
141/// Expansion support: collect declarations only in explicitly selected host builds.
142#[doc(hidden)]
143#[cfg(all(feature = "build", not(target_arch = "wasm32")))]
144#[macro_export]
145macro_rules! __icydb_register_schema {
146    ($($item:item)*) => { $($item)* };
147}
148
149#[doc(hidden)]
150#[cfg(not(all(feature = "build", not(target_arch = "wasm32"))))]
151#[macro_export]
152macro_rules! __icydb_register_schema {
153    ($($item:item)*) => {};
154}