Cotton code
58 lines · 2.7 KB · RustRaw
//! A row of this file, read back as the op it was — ONE of the three places a//! [`CottonOp`] is built (`mint` writes this file's own; `wire` reads a//! peer's bytes).//!//! Three kinds of row hold a write whole: a queued push in `_outbox`, a parked//! write in `_parked`, and the file's own//! tables, from which `changes` re-emits an author's rows for a peer. Each is//! read back HERE and nowhere else, so "what a stored op is" has one answer,//! and the op id is always the one the row kept: a retry or a re-send carries//! the same idempotency key, and the fold folds the second arrival to nothing.use crate::actor::Actor;use crate::driver::Row;use crate::protocol::types::CottonOp;use crate::rejected::RejectedOp;/// One `_outbox` row as the op that was queued: its id and its bytes.pub fn outbox_row(row: &Row) -> std::result::Result<CottonOp, String> {    let bytes = match row.get("bytes") {        Some(rusqlite::types::Value::Blob(bytes)) => bytes.as_slice(),        Some(rusqlite::types::Value::Text(text)) => text.as_bytes(),        _ => return Err(format!("queued op {} holds no receipt bytes", row.text("op_id").unwrap_or_default())),    };    let mut op = crate::wire::from_bytes(row.text("op_id").unwrap_or_default().to_string(), bytes)?;    // The signature the row kept travels with its bytes. A reader that did    // not select `sig` gets an unsigned op, so every reader whose op TRAVELS    // selects it (`tests/signed_receipts.rs` pins the three that do).    if let Some(rusqlite::types::Value::Blob(sig)) = row.get("sig") {        op.sig = Some(crate::signed::sig_text(sig));    }    Ok(op)}/// One `_writes` or `_tombstones` row as the op its receipt was.pub fn receipt_row(row: &Row) -> std::result::Result<CottonOp, String> {    outbox_row(row)}/// One parked write as the op it was, or why it cannot be one: a row parked/// with no receipt bytes is a write nobody can fold again.pub fn rejected_row(row: &RejectedOp) -> std::result::Result<CottonOp, String> {    let Some(bytes) = row.bytes.as_deref() else {        return Err("this write was parked with no receipt, so there is nothing to fold".to_string());    };    let mut op = crate::wire::from_bytes(row.op_id.clone(), bytes)?;    op.sig = row.sig.as_deref().map(crate::signed::sig_text);    Ok(op)}/// An actor read back out of a column this file wrote — checked when it was/// minted, so a text that does not parse is a damaged row, named as such.pub fn actor(row: &Row, column: &str) -> std::result::Result<Actor, String> {    let Some(text) = row.text(column) else {        return Err(format!("the `{column}` column holds no actor"));    };    Actor::from_wire(text)}