icydb_model/node/
constraint.rs1use crate::prelude::*;
4use icydb_schema::{SchemaContractError, SourceCheckExpr};
5
6use crate::node::Schema;
7
8pub type SourceExpressionResolver = fn(&Schema) -> Result<SourceCheckExpr, SchemaContractError>;
10
11#[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 #[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 #[must_use]
43 pub const fn name(&self) -> &'static str {
44 self.name
45 }
46
47 #[must_use]
49 pub const fn check(&self) -> &'static str {
50 self.check
51 }
52
53 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}