Skip to main content

icydb_model/node/
constraint.rs

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