Cotton code
365 lines · 11.8 KB · RustRaw
//! What the Mac's window contains — the ONE declaration.//!//! The port of `apps/cotton/sections.ts`, which sat at that app's root//! rather than in `src/` because `src/` was the Electron tree and went away//! while this list did not. It has now outlived its second home too: the Swift//! navigator, the View menu and the section registry are generated from HERE//! (`cotton-gen`), so the three cannot hold three opinions about what the//! window contains. Its last TypeScript oddity went with the move — a//! type-only `import type { PrimitiveKind } from "@cottondb/core"` the Mac's//! own manifest never declared, resolving through pnpm's hoisting on the//! critical path of a required check. Here the type is simply a type.//!//! Section order and wording are the browser spec's §D5, verbatim, and the//! machine section is LAST on purpose: the person's own records come before//! the machine that serves them.//!//! The rule the kind map carries is the repo's rule for any map over kinds//! (AGENTS.md, Primitives): **a "no" is never a missing entry, it is a written//! reason.** A kind that is machinery — a rule behind another record, never a//! thing a person browses — is named in [`MACHINERY_KINDS`] with the sentence//! saying where it IS seen.use crate::constants::PrimitiveKind;/// Every section of the window, by key.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pub enum SectionKey {    Today,    Timeline,    Decide,    Projects,    People,    Photos,    Documents,    Messages,    Email,    Money,    Writing,    Calendar,    Assistants,    Passwords,    Records,    Bin,    Machine,}impl SectionKey {    /// In the order the navigator draws them.    pub const ALL: [SectionKey; 17] = [        SectionKey::Today,        SectionKey::Timeline,        SectionKey::Decide,        SectionKey::Projects,        SectionKey::People,        SectionKey::Photos,        SectionKey::Documents,        SectionKey::Messages,        SectionKey::Email,        SectionKey::Money,        SectionKey::Writing,        SectionKey::Calendar,        SectionKey::Assistants,        SectionKey::Passwords,        SectionKey::Records,        SectionKey::Bin,        SectionKey::Machine,    ];    pub const fn as_str(self) -> &'static str {        match self {            SectionKey::Today => "today",            SectionKey::Timeline => "timeline",            SectionKey::Decide => "decide",            SectionKey::Projects => "projects",            SectionKey::People => "people",            SectionKey::Photos => "photos",            SectionKey::Documents => "documents",            SectionKey::Messages => "messages",            SectionKey::Email => "email",            SectionKey::Money => "money",            SectionKey::Writing => "writing",            SectionKey::Calendar => "calendar",            SectionKey::Assistants => "assistants",            SectionKey::Passwords => "passwords",            SectionKey::Records => "records",            SectionKey::Bin => "bin",            SectionKey::Machine => "machine",        }    }    /// Whether a string names a section of the window.    pub fn parse(value: &str) -> Option<SectionKey> {        SectionKey::ALL            .into_iter()            .find(|key| key.as_str() == value)    }}/// Where a row sits in the navigator. `Top` has no heading (the three landing/// views), `Yours` and `More` carry theirs, and `Machine` is the last group and/// carries none — it is one row, and a heading over one row is noise.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pub enum SectionGroup {    Top,    Yours,    More,    Machine,}impl SectionGroup {    /// The groups, in the order the navigator draws them.    pub const ALL: [SectionGroup; 4] = [        SectionGroup::Top,        SectionGroup::Yours,        SectionGroup::More,        SectionGroup::Machine,    ];    pub const fn as_str(self) -> &'static str {        match self {            SectionGroup::Top => "top",            SectionGroup::Yours => "yours",            SectionGroup::More => "more",            SectionGroup::Machine => "machine",        }    }    /// The heading above this group, or nothing where it carries none.    pub const fn heading(self) -> Option<&'static str> {        match self {            SectionGroup::Top => None,            SectionGroup::Yours => Some("Yours"),            SectionGroup::More => Some("More"),            SectionGroup::Machine => None,        }    }}/// One row of the navigator.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct SectionEntry {    pub key: SectionKey,    /// What the navigator shows, and what the View menu shows.    pub label: &'static str,    pub group: SectionGroup,    /// ⌘1…⌘9, or nothing where there is no room left in the first nine.    pub shortcut: Option<u8>,}/// Every section, in the order the navigator draws them.pub const SECTION_LIST: [SectionEntry; 17] = [    SectionEntry {        key: SectionKey::Today,        label: "Today",        group: SectionGroup::Top,        shortcut: Some(1),    },    SectionEntry {        key: SectionKey::Timeline,        label: "Timeline",        group: SectionGroup::Top,        shortcut: Some(2),    },    SectionEntry {        key: SectionKey::Decide,        label: "To decide",        group: SectionGroup::Top,        shortcut: Some(3),    },    SectionEntry {        key: SectionKey::Projects,        label: "Projects",        group: SectionGroup::Yours,        shortcut: Some(4),    },    SectionEntry {        key: SectionKey::People,        label: "People",        group: SectionGroup::Yours,        shortcut: Some(5),    },    SectionEntry {        key: SectionKey::Photos,        label: "Photos & videos",        group: SectionGroup::Yours,        shortcut: Some(6),    },    SectionEntry {        key: SectionKey::Documents,        label: "Documents",        group: SectionGroup::Yours,        shortcut: Some(7),    },    SectionEntry {        key: SectionKey::Messages,        label: "Messages",        group: SectionGroup::Yours,        shortcut: Some(8),    },    SectionEntry {        key: SectionKey::Email,        label: "Email",        group: SectionGroup::Yours,        shortcut: Some(9),    },    SectionEntry {        key: SectionKey::Money,        label: "Money",        group: SectionGroup::Yours,        shortcut: None,    },    SectionEntry {        key: SectionKey::Writing,        label: "Writing",        group: SectionGroup::Yours,        shortcut: None,    },    SectionEntry {        key: SectionKey::Calendar,        label: "Calendar",        group: SectionGroup::Yours,        shortcut: None,    },    SectionEntry {        key: SectionKey::Assistants,        label: "Assistants",        group: SectionGroup::Yours,        shortcut: None,    },    SectionEntry {        key: SectionKey::Passwords,        label: "Passwords",        group: SectionGroup::Yours,        shortcut: None,    },    SectionEntry {        key: SectionKey::Records,        label: "Everything else",        group: SectionGroup::More,        shortcut: None,    },    SectionEntry {        key: SectionKey::Bin,        label: "Bin",        group: SectionGroup::More,        shortcut: None,    },    SectionEntry {        key: SectionKey::Machine,        label: "This Mac",        group: SectionGroup::Machine,        shortcut: None,    },];/// The list entry for a key. Total over [`SectionKey`] by construction — the/// TypeScript searched the array and threw on a miss, which is what a drift/// between the key union and the list sounded like; here the `match` is the/// declaration and there is nothing to drift from.pub const fn section_entry(key: SectionKey) -> SectionEntry {    let mut at = 0;    // A `const fn` cannot iterate, and an index by discriminant would be a    // second declaration of the order. The loop is the search the TypeScript    // wrote, made total by the panic below being unreachable for every variant    // the list names — which `sections_tests.rs` proves for all sixteen.    while at < SECTION_LIST.len() {        if SECTION_LIST[at].key as u8 == key as u8 {            return SECTION_LIST[at];        }        at += 1;    }    panic!("no navigator entry for this section key")}/// The kinds each section owns.////// The three landing sections and the last three own NONE: Today, Timeline and/// To decide are views ACROSS other sections' kinds, "Everything else" reaches/// every kind by definition, the Bin is a filter over the binnable ones, and/// This Mac holds no records at all. Ownership here means "this is the section/// that draws this kind's own view", which is what a palette or an inspector/// needs when it has somewhere to go.pub const fn section_kinds(key: SectionKey) -> &'static [PrimitiveKind] {    match key {        SectionKey::Today | SectionKey::Timeline | SectionKey::Decide => &[],        // Code hosting (v19) is drawn UNDER Projects rather than as a        // section of its own: a repository is reached by opening the project        // that names it, and a navigator entry called Code would be a second        // way in to the same rows. The four kinds that follow the repository        // are read inside it — a pull request, an issue, its timeline and a        // release each have a page, and every one of those pages is a page of        // the repository.        SectionKey::Projects => &[            PrimitiveKind::Project,            PrimitiveKind::Repository,            PrimitiveKind::Pull,            PrimitiveKind::Issue,            PrimitiveKind::RepoComment,            PrimitiveKind::Release,            PrimitiveKind::Run,        ],        SectionKey::People => &[PrimitiveKind::Person],        SectionKey::Photos => &[            PrimitiveKind::Photo,            PrimitiveKind::Video,            PrimitiveKind::Album,        ],        SectionKey::Documents => &[PrimitiveKind::Document],        SectionKey::Messages => &[            PrimitiveKind::Thread,            PrimitiveKind::Message,            PrimitiveKind::Call,        ],        SectionKey::Email => &[PrimitiveKind::EmailThread, PrimitiveKind::Email],        SectionKey::Money => &[            PrimitiveKind::Transaction,            PrimitiveKind::Statement,            PrimitiveKind::MoneyAccount,            PrimitiveKind::StatementLine,            PrimitiveKind::Evidence,        ],        SectionKey::Writing => &[            PrimitiveKind::Note,            PrimitiveKind::Journal,            PrimitiveKind::Memory,        ],        SectionKey::Calendar => &[            PrimitiveKind::Event,            PrimitiveKind::Todo,        ],        SectionKey::Assistants => &[            PrimitiveKind::Session,            PrimitiveKind::SessionMessage,            PrimitiveKind::Prompt,        ],        SectionKey::Passwords => &[PrimitiveKind::Secret],        SectionKey::Records | SectionKey::Bin | SectionKey::Machine => &[],    }}/// Kinds that are machinery and never a section — each with the reason.pub const MACHINERY_KINDS: [(PrimitiveKind, &str); 2] = [    (        PrimitiveKind::TodoRecurrence,        "the repeat rule behind a todo; shown on the todo it serves",    ),    (        PrimitiveKind::Device,        "a phone or Mac admitted to this database; shown whole on the Your devices screen under \         Settings, which is where a person goes to add one or cut one off — never a section of \         the navigator, because nobody browses their devices looking for something",    ),    // gen-kind: machinery-kinds];/// The section that draws a kind, or nothing when the kind is machinery.pub fn section_for_kind(kind: PrimitiveKind) -> Option<SectionEntry> {    SECTION_LIST        .into_iter()        .find(|entry| section_kinds(entry.key).contains(&kind))}#[cfg(test)]#[path = "sections_tests.rs"]mod tests;