Cotton code
37 lines · 1.7 KB · RustRaw
//! Reservation rows are server facts. Their movement rows, rather than a mutable balance, survive offline edits.use crate::{Driver, Result};use serde_json::Value;pub fn reservation_table(db: &Driver, physical_table: &str) -> Result<bool> {    db.batch_fact(&format!("reservation_table {physical_table}"), || read_reservation_table(db, physical_table))}fn read_reservation_table(db: &Driver, physical_table: &str) -> Result<bool> {    // Definitions are shared data. The fold cannot depend on the core's typed decoder.    if !physical_table.starts_with("cotton_")        || crate::schema::specs::spec_for(physical_table).is_some()        || !db.table_exists("cotton_tool_definitions")?    {        return Ok(false);    }    for row in db.all("SELECT definition FROM cotton_tool_definitions", &[])? {        let value: Value = serde_json::from_str(row.text("definition").unwrap_or("{}"))            .map_err(|e| crate::StoreError::refused(e.to_string()))?;        if value            .get("reserve")            .and_then(|r| r.get("claim_table"))            .and_then(Value::as_str)            .is_some_and(|t| format!("cotton_{t}") == physical_table)        {            return Ok(true);        }    }    Ok(false)}/// Every historical serving author stays valid for replication; a client's newer stamp has no authority over these rows.pub fn refusal(db: &Driver, table: &str, author: &str) -> Result<Option<String>> {    if reservation_table(db, table)? && !crate::serving::is_serving_author(db, author)? {        return Ok(Some("reservation rows are written only by a server action; offline edits cannot change a derived quantity".into()));    }    Ok(None)}