//! `mint`'s KINDS hook — slice 5a-ii's seam (store-erase): the five kinds of
//! delete (design 6.2), checked on each change before it is stamped.
//!
//! - A retract names its kind of delete ([`Delete`]), and nothing else does.
//! - Fact: a fact table refuses every delete, here as in the fold.
//! - A write at an erased id, or under an erased owning parent, is refused
//! locally with the reason it would park `gone` under on arrival.
//! - No act retracts an owning child together with an owning ancestor of it
//! (3.5 point 3): the ancestor's retract takes the child; a child retract
//! beside it would be dropped below T, or, on a table with no history, be
//! an undo that cannot name the child's columns.
//!
//! - A take-back (Undo of an add) is judged here, and no erase borrows its
//! reason ([`crate::take_back`]).
//!
//! Still to come in 5a-ii: the kind a dynamic sink declares per table.
use rusqlite::types::Value as SqlValue;
use serde_json::Value as JsonValue;
use super::{Act, Change, Delete};
use crate::canonical::Kind;
use crate::driver::Driver;
use crate::error::{Result, StoreError};
use crate::schema::specs::spec_for;
use crate::schema::table_specs::OWNING_REFS;
pub(crate) fn check(db: &Driver, act: &Act, change: &Change) -> Result<()> {
if change.kind == Kind::Retract && change.table == crate::revocation::DEVICES_SUBJECT {
return Err(StoreError::refused("Devices are removed by revoking their key; the approval row remains as the revocation audit"));
}
if change.kind == Kind::Retract && change.delete.is_none() {
return Err(StoreError::refused(format!(
"`{}` deletes {} {} without naming which kind of delete it is — a retract, an erase, a source-erase or a revocation",
act.verb, change.table, change.row_id
)));
}
if change.kind != Kind::Retract && change.delete.is_some() {
return Err(StoreError::refused(format!(
"`{}` names a kind of delete on a {} of {} {} — only a delete has one",
act.verb,
change.kind.as_str(),
change.table,
change.row_id
)));
}
if change.kind == Kind::Retract
&& crate::fact_hooks::is_fact_table(&change.table)
&& !crate::fact_hooks::erases_history(&change.table, change.delete.as_ref().and_then(|d| d.erase_reason()).is_some())
{
return Err(StoreError::refused(format!(
"`{}`: {} holds facts, and a fact is never deleted — {} stays",
act.verb, change.table, change.row_id
)));
}
if crate::erase::is_gone(db, &change.table, &change.row_id)? {
return Err(StoreError::refused(format!(
"{} `{}` writes {} {}, which was erased — a later write at its id is never applied",
crate::erase::GONE_REFUSAL,
act.verb,
change.table,
change.row_id
)));
}
match &change.delete {
Some(Delete::Erase { reason }) if reason == crate::take_back::REASON => {
return Err(StoreError::refused(format!(
"`{}` erases {} {} as taken back without being a take-back — only Undo of an add names that reason, and it is judged",
act.verb, change.table, change.row_id
)));
}
Some(Delete::TakeBack { through }) => {
if let Some(why) = crate::take_back::refusal(db, &act.actor, &change.table, &change.row_id, through)? {
return Err(StoreError::refused(format!(
"`{}` may not take back {} {}: {why}",
act.verb, change.table, change.row_id
)));
}
}
_ => {}
}
if change.kind == Kind::Retract {
if let Some((table, row_id)) = retracted_ancestor(db, act, change, 0)? {
return Err(StoreError::refused(format!(
"`{}` deletes {} {} together with {table} {row_id}, which owns it — the owner's delete takes it, so the act names only the owner",
act.verb, change.table, change.row_id
)));
}
}
Ok(())
}
/// The owning ancestor of the deleted row that this act also deletes, if
/// any. The parent is read from the row, or from its receipts when it is
/// dormant. Whatever kinds the two deletes name.
fn retracted_ancestor(db: &Driver, act: &Act, change: &Change, depth: usize) -> Result