#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum ConstraintEnforcement {
#[default]
Enforced,
DeclaredOnly(DeclaredOnly),
}
impl ConstraintEnforcement {
#[must_use]
pub const fn keyword(self) -> &'static str {
match self {
ConstraintEnforcement::DeclaredOnly(declared) => declared.keyword,
ConstraintEnforcement::Enforced => "",
}
}
#[must_use]
pub const fn ddl_note(self) -> Option<&'static str> {
match self {
ConstraintEnforcement::DeclaredOnly(declared) if declared.keyword.is_empty() => {
Some(declared.note)
}
_ => None,
}
}
#[must_use]
pub const fn is_declared_only(self) -> bool {
matches!(self, ConstraintEnforcement::DeclaredOnly(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DeclaredOnly {
pub keyword: &'static str,
pub note: &'static str,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_is_an_enforcing_engine() {
assert_eq!(ConstraintEnforcement::default(), ConstraintEnforcement::Enforced);
assert_eq!(ConstraintEnforcement::default().keyword(), "");
assert_eq!(ConstraintEnforcement::default().ddl_note(), None);
assert!(!ConstraintEnforcement::default().is_declared_only());
}
#[test]
fn a_keyword_speaks_on_the_constraint_and_a_note_speaks_above_it() {
let with_keyword = ConstraintEnforcement::DeclaredOnly(DeclaredOnly {
keyword: " NOT ENFORCED",
note: "keys here are optimizer metadata.",
});
assert_eq!(with_keyword.keyword(), " NOT ENFORCED");
assert_eq!(
with_keyword.ddl_note(),
None,
"the keyword is on the constraint line already: a comment saying it again is one \
statement making the same claim twice"
);
assert!(with_keyword.is_declared_only());
let without_keyword = ConstraintEnforcement::DeclaredOnly(DeclaredOnly {
keyword: "",
note: "this engine accepts the declaration and checks nothing.",
});
assert_eq!(without_keyword.keyword(), "");
assert_eq!(
without_keyword.ddl_note(),
Some("this engine accepts the declaration and checks nothing."),
"with no keyword the comment is the only place a reader of this DDL can learn it"
);
assert!(without_keyword.is_declared_only());
}
}