1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Accepted-check declarations retained by the host authoring graph.
use crate::prelude::*;
use icydb_schema::{SchemaContractError, SourceCheckExpr};
use crate::node::Schema;
/// Compiler-produced source-expression projection for one accepted check.
pub type SourceExpressionResolver = fn(&Schema) -> Result<SourceCheckExpr, SchemaContractError>;
///
/// CheckConstraint
///
/// Source-keyed accepted-check declaration. The SQL spelling remains authored
/// input until the compiler projection lowers it into the public source AST.
///
#[derive(Clone, Debug, Serialize)]
pub struct CheckConstraint {
source_key: &'static str,
name: &'static str,
check: &'static str,
#[serde(skip)]
expression: SourceExpressionResolver,
}
impl CheckConstraint {
/// Construct one source-keyed accepted-check declaration.
#[must_use]
pub const fn new(
source_key: &'static str,
name: &'static str,
check: &'static str,
expression: SourceExpressionResolver,
) -> Self {
Self {
source_key,
name,
check,
expression,
}
}
/// Borrow the immutable constraint source key.
#[must_use]
pub const fn source_key(&self) -> &'static str {
self.source_key
}
/// Borrow the editable accepted-check name.
#[must_use]
pub const fn name(&self) -> &'static str {
self.name
}
/// Borrow the authored check expression.
#[must_use]
pub const fn check(&self) -> &'static str {
self.check
}
/// Lower the compiler-validated expression into the public source AST.
///
/// # Errors
///
/// Returns a typed proposal error when an enum literal no longer resolves
/// through the sealed graph or the expression violates public bounds.
pub fn source_expression(
&self,
schema: &Schema,
) -> Result<SourceCheckExpr, SchemaContractError> {
(self.expression)(schema)
}
}
impl ValidateNode for CheckConstraint {
fn validate(&self) -> Result<(), ErrorTree> {
let mut errs = ErrorTree::new();
validate_source_key(
&mut errs,
"constraint",
self.source_key(),
icydb_schema::ConstraintSourceKey::try_new,
);
errs.result()
}
}
impl VisitableNode for CheckConstraint {
fn route_key(&self) -> String {
self.name().to_string()
}
}