Skip to main content

icydb_schema/
lib.rs

1pub mod build;
2pub mod error;
3pub mod node;
4pub mod types;
5pub mod validate;
6pub mod visit;
7
8/// Maximum length for entity schema identifiers.
9pub const MAX_ENTITY_NAME_LEN: usize = 64;
10
11/// Maximum length for field schema identifiers.
12pub const MAX_FIELD_NAME_LEN: usize = 64;
13
14/// Maximum number of fields allowed in a derived index.
15pub const MAX_INDEX_FIELDS: usize = 4;
16
17/// Maximum length for derived index identifiers.
18pub const MAX_INDEX_NAME_LEN: usize =
19    MAX_ENTITY_NAME_LEN + (MAX_INDEX_FIELDS * (1 + MAX_FIELD_NAME_LEN));
20
21use crate::{build::BuildError, node::NodeError};
22use thiserror::Error as ThisError;
23
24///
25/// Prelude
26///
27
28pub mod prelude {
29    pub(crate) use crate::build::schema_read;
30    pub use crate::{
31        err,
32        error::ErrorTree,
33        node::*,
34        types::{Cardinality, Primitive},
35        visit::Visitor,
36    };
37    pub use candid::CandidType;
38    pub use serde::{Deserialize, Serialize};
39}
40
41///
42/// Error
43///
44
45#[derive(Debug, ThisError)]
46pub enum Error {
47    #[error(transparent)]
48    BuildError(#[from] BuildError),
49
50    #[error(transparent)]
51    NodeError(#[from] NodeError),
52}
53
54///
55/// TESTS
56///
57
58#[cfg(test)]
59mod tests {
60    use super::{Error, build::BuildError, error::ErrorTree, node::NodeError};
61
62    #[test]
63    fn build_errors_remain_in_build_boundary() {
64        let schema_error = Error::from(BuildError::Validation(ErrorTree::from(
65            "missing schema relation target",
66        )));
67
68        match schema_error {
69            Error::BuildError(BuildError::Validation(tree)) => {
70                assert!(
71                    tree.messages()
72                        .iter()
73                        .any(|message| message == "missing schema relation target"),
74                    "build validation errors must remain wrapped as build-boundary failures",
75                );
76            }
77            Error::NodeError(_) => {
78                panic!("build validation failures must not be remapped into node-boundary errors");
79            }
80        }
81    }
82
83    #[test]
84    fn node_errors_remain_in_node_boundary() {
85        let schema_error = Error::from(NodeError::PathNotFound("entity.user_id".to_string()));
86
87        match schema_error {
88            Error::NodeError(NodeError::PathNotFound(path)) => {
89                assert_eq!(path, "entity.user_id");
90            }
91            Error::NodeError(NodeError::IncorrectNodeType(path)) => {
92                panic!("unexpected node error kind after conversion for path {path}");
93            }
94            Error::BuildError(_) => {
95                panic!("node errors must not be remapped into build-boundary failures");
96            }
97        }
98    }
99}