#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatementEffect {
None,
Attempted,
Applied,
}
impl StatementEffect {
pub fn from_facts(attempted: bool, applied: bool) -> Self {
match (attempted, applied) {
(false, false) => Self::None,
(true, false) => Self::Attempted,
(false, true) | (true, true) => Self::Applied,
}
}
pub fn from_applied(applied: bool) -> Self {
Self::from_facts(true, applied)
}
pub fn attempted(self) -> bool {
self != Self::None
}
pub fn applied(self) -> bool {
self == Self::Applied
}
pub fn changed_nothing(self) -> bool {
self == Self::Attempted
}
pub fn merge(&mut self, other: Self) {
*self = match (*self, other) {
(Self::None, Self::None) => Self::None,
(Self::None, Self::Attempted)
| (Self::Attempted, Self::None)
| (Self::Attempted, Self::Attempted) => Self::Attempted,
(Self::None, Self::Applied)
| (Self::Applied, Self::None)
| (Self::Attempted, Self::Applied)
| (Self::Applied, Self::Attempted)
| (Self::Applied, Self::Applied) => Self::Applied,
};
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn changed_nothing_is_the_attempted_without_application_state() {
assert!(
!StatementEffect::None.changed_nothing(),
"a read never reports, however the planner classified it"
);
assert!(
StatementEffect::Attempted.changed_nothing(),
"a mutation operator that applied nothing is the note's subject"
);
assert!(
!StatementEffect::Applied.changed_nothing(),
"an applied change — entity, schema or catalog — is not zero-effect"
);
}
#[test]
fn the_accessors_read_the_state() {
for (effect, attempted, applied) in [
(StatementEffect::None, false, false),
(StatementEffect::Attempted, true, false),
(StatementEffect::Applied, true, true),
] {
assert_eq!(effect.attempted(), attempted, "{effect:?}");
assert_eq!(effect.applied(), applied, "{effect:?}");
}
}
#[test]
fn merge_is_the_three_state_join() {
use StatementEffect::{Applied, Attempted, None};
let table = [
(None, None, None),
(None, Attempted, Attempted),
(None, Applied, Applied),
(Attempted, None, Attempted),
(Attempted, Attempted, Attempted),
(Attempted, Applied, Applied),
(Applied, None, Applied),
(Applied, Attempted, Applied),
(Applied, Applied, Applied),
];
for (left, right, expected) in table {
let mut joined = left;
joined.merge(right);
assert_eq!(joined, expected, "{left:?} join {right:?}");
let mut reversed = right;
reversed.merge(left);
assert_eq!(reversed, expected, "{right:?} join {left:?}");
}
}
#[test]
fn from_facts_joins_the_two_facts() {
for (attempted, applied, expected) in [
(false, false, StatementEffect::None),
(true, false, StatementEffect::Attempted),
(true, true, StatementEffect::Applied),
(false, true, StatementEffect::Applied),
] {
assert_eq!(
StatementEffect::from_facts(attempted, applied),
expected,
"attempted={attempted} applied={applied}"
);
}
}
}