//! A record's trail, read from `cotton_changes` — slice 6's one history.//!//! A history row holds before and after of the fields an act MOVED, while the//! pairing in [`super::entries`] reads whole snapshots of the record as it//! stood before each edit. So the snapshots are rebuilt here, newest to//! oldest: start from the live row and lay each row's `before` over it.//!//! Rows come in two dialects, and a record's trail holds both: `mint`'s//! (`table_id` the storage table, `verb` the act's) and the generic verbs'//! (`table_id` the definition id, which for a built-in is its kind, `verb` a//! `records::Verb`). Neither verb is read: a row with no `before` made or put//! back the record and has no earlier state to show, a row with no `after`//! removed it, every other row edited it.use cotton_store::driver::Row;use serde_json::Value;usecrate::constants::{VersionOp, VersionRecordKind};usesuper::diff::RecordRow;usesuper::entries::RecordVersion;usesuper::fields::history_kind;/// A record's history rows, newest first: bind the record's id and the two/// [`trail_tables`]. Two edits inside one millisecond are settled by `rowid`,/// the order `mint` wrote them.pubconst TRAIL_SQL: &str = "SELECT id, before, after, actor, created_at FROM cotton_changes \ WHERE record_id = ? AND table_id IN (?, ?) ORDER BY created_at DESC, rowid DESC";/// The `table_id`s a kind's history rows carry: its storage table (`mint`'s)/// and its kind word (the generic verbs').pubfn trail_tables(kind: VersionRecordKind) -> [&'static str; 2] { [history_kind(kind).table, kind.as_str()]}/// The record's versions, newest first — each the record as it stood BEFORE/// the edit that dated it — from its [`TRAIL_SQL`] rows. `current` is the live/// row (`None` once it is gone).pubfn record_trail( kind: VersionRecordKind, workspace_id: &str, record_id: &str, rows: &[Row], current: Option<&RecordRow>,) -> Vec<RecordVersion> {let object = |raw: Option<&str>| -> Option<RecordRow> {match serde_json::from_str::<Value>(raw?).ok()? {Value::Object(fields) => Some(fields), _ => None, } };letmut running: RecordRow = current.cloned().unwrap_or_default();letmut versions = Vec::new();for row in rows {letSome(before) = object(row.text("before")) else {continue; };let op = if object(row.text("after")).is_none() { VersionOp::Delete } else { VersionOp::Update };for (column, value) in before { running.insert(column, value); } versions.push(RecordVersion { id: row.text("id").unwrap_or_default().to_string(), workspace_id: workspace_id.to_string(), record_kind: kind, record_id: record_id.to_string(), op, data: running.clone(), actor_role: actor_role(row.text("actor").unwrap_or_default()).to_string(), created_at: row.text("created_at").unwrap_or_default().to_string(), }); } versions}/// The history screen's role for an act's actor (`Actor::to_wire`): the owner's/// own device, a member's (a peer), an assistant's grant, or Cotton's own work.pubfn actor_role(actor: &str) -> &'static str {match actor.split_once(':').map_or(actor, |(kind, _)| kind) {"owner" => "owner","member" => "peer","token" | "credential" | "oauth" | "admin" | "session" | "automation" => "agent","job" | "device-job" | "import" | "takeover" => "system", _ => "local", }}#[cfg(test)]#[path = "trail_tests.rs"]mod tests;