Cotton code
179 lines · 7.6 KB · RustRaw
//! The bin — one place things land softly, glanced at once, emptied in one act//! (schema v33). The port of `packages/core/src/engine/bin.ts`.//!//! KIND-GENERIC FROM DAY ONE, deliberately: see [`crate::constants::BinKind`]//! for why one bin and not one per kind.//!//! The TypeScript holds `BIN_SOURCES` total over `BIN_KINDS` with a static//! assertion that throws at module load. Here the totality is the `match`, so a//! kind added without a source fails the BUILD — the same discipline one step//! earlier, and the reason it matters is unchanged: a kind missing from a read//! registry reads as "there is none of that" to every caller, which in a bin//! means the owner's mail is gone with no trace of where it went.use crate::constants::BinKind;/// A value a restore writes back beside clearing `trashed_at`.#[derive(Debug, Clone, Copy, PartialEq)]pub enum RestoreValue {    Text(&'static str),    Number(f64),    Null,}#[derive(Debug, Clone, Copy, PartialEq)]pub struct BinSource {    /// The synced table holding the kind's rows.    pub table: &'static str,    /// Restoring is normally "clear `trashed_at`". A kind whose restore has to    /// say something more names the extra columns here, applied in the same    /// UPDATE.    pub restore_also: &'static [(&'static str, RestoreValue)],}/// Where each kind's bin rows live, and what a restore has to say.////// Hand path: a binnable kind's table is its engine's compiled storage, which/// the definition does not name (a password's has no compiled shape at all, a/// document's lists its pages first), and `restore_also` is engine behaviour,/// not drawing. Titles come from the definitions ([`title_column`]).pub const fn bin_source(kind: BinKind) -> BinSource {    match kind {        BinKind::Transaction => BinSource {            table: "cotton_money_transactions",            restore_also: &[],        },        BinKind::Email => BinSource {            table: "cotton_emails",            // Coming back out of the bin is a change Gmail has to hear about,            // exactly as going in was: the row goes `pending` and the push loop            // untrashes it.            restore_also: &[("sync_state", RestoreValue::Text("pending"))],        },        BinKind::EmailThread => BinSource {            table: "cotton_email_threads",            // A thread's row carries no `sync_state`: it is not a Gmail object            // at all, and restoring it is restoring its MESSAGES — each of            // which goes `pending` on its own through the `email` arm above.            restore_also: &[],        },        // A password (v6 of the Rust ladder). Nothing to tell anybody about on        // the way in or out — the row is the person's own and travels nowhere —        // so `restore_also` is empty and clearing `trashed_at` is the whole of        // a restore. The SEALED value rides along untouched: a secret in the        // bin is still sealed, and emptying the bin is what erases it.        BinKind::Secret => BinSource {            table: "cotton_secrets",            restore_also: &[],        },        // A deleted note. Restoring is clearing `trashed_at`; an archived        // note binned comes back archived, as it went in. A journal day        // shares the table and never goes in: it is added to, not deleted.        BinKind::Note => BinSource {            table: "cotton_notes",            restore_also: &[],        },        // A deleted event, a whole one (a moved occurrence never waits here:        // its series does). Restoring is clearing `trashed_at`: a row Google        // still has went `pending` on the way in, and one the push already        // cancelled there has no Google identity left, so the push makes it        // anew.        BinKind::Event => BinSource {            table: "cotton_events",            restore_also: &[],        },        // A deleted photo or video, named by its caption as its slip shows it.        // Its file waits with it: restoring is clearing `trashed_at`, and only        // the bin's erase writes the erasure fact that lets the bytes go.        BinKind::Photo => BinSource {            table: "cotton_photos",            restore_also: &[],        },        BinKind::Video => BinSource {            table: "cotton_videos",            restore_also: &[],        },        // A deleted document. Its file and its pages wait with it: restoring        // is clearing `trashed_at`, and only the bin's erase writes the        // erasure facts that let the file go.        BinKind::Document => BinSource {            table: "cotton_documents",            restore_also: &[],        },        // A failed text of the owner's, named by its words. While binned the        // send pass never sends it and the conversation never draws it; a        // restore clears `trashed_at` and leaves it failed, to be sent again        // only by its own Retry.        BinKind::Message => BinSource {            table: "cotton_msg_messages",            restore_also: &[],        },        BinKind::Todo => BinSource {            table: "cotton_todos",            restore_also: &[],        },        // gen-kind: bin-source    }}/// One row waiting in the bin, whatever kind it is.#[derive(Debug, Clone, PartialEq, Eq)]pub struct BinEntry {    pub kind: BinKind,    pub id: String,    pub title: String,    pub trashed_at: String,}/// Kinds whose bin title cannot come from a built-in definition yet, each/// with its reason. Every other kind's title column is its shipped table/// definition's `title` (lane T7, 22 Oct 2026: the bin stops hand-listing/// what the definitions already say).pub const HAND_TITLES: &[(BinKind, &str)] = &[    // No built-in definition ships for these: their tools' definitions live    // in packages a database installs, so the core cannot read them here.    (BinKind::Transaction, "description"),    // The definition titles a message by `preview`, a column its engine    // answers on read; the stored words a binned row is named by are `body`.    (BinKind::Message, "body"),    // gen-kind: bin-hand-title];/// The column whose value the bin shows as a row of `kind`'s title: the/// built-in definition's `title`, or the hand entry for a kind no definition/// can describe yet ([`HAND_TITLES`]).pub fn title_column(kind: BinKind) -> &'static str {    if let Some((_, column)) = HAND_TITLES.iter().find(|(hand, _)| *hand == kind) {        return column;    }    crate::definitions::builtin_table_definitions()        .iter()        .find(|definition| definition.id == kind.as_str())        .map(|definition| definition.title.as_str())        .unwrap_or_else(|| panic!("bin kind {kind} has no built-in definition and no hand title"))}/// The line set the bin's words live in.pub const LINES: &str = "bin";/// The title a bin row shows: what the record is called, or the kind's own/// words for having nothing to call it — the line `untitled.<kind>` of set/// `bin` (`untitled_any` for a kind no line names yet), the database's row/// first. A bin row with no words on it is a row/// nobody can decide about.pub fn bin_title(kind: BinKind, value: Option<&str>, lines: &[super::shell_lines::LineRow]) -> String {    let trimmed = value        .unwrap_or("")        .trim_matches(cotton_store::protocol::js::is_js_whitespace);    if trimmed.is_empty() {        super::shell_lines::resolve(LINES, &format!("untitled.{kind}"), lines, &[])            .or_else(|| super::shell_lines::resolve(LINES, "untitled_any", lines, &[]))            .expect("the bin line set ships untitled_any")    } else {        trimmed.to_string()    }}#[cfg(test)]#[path = "bin_tests.rs"]mod tests;