Cotton code
285 lines · 11.4 KB · RustRaw
//! A project's PAGE — which sections it draws, and in whose order.//!//! Everything a project has is a section, drawn one of two ways: a section//! that holds a LIST (its heading, how many, the newest few, a way to all of//! them) or a section that holds ONE THING (its heading, one line saying what//! it holds, and the thing opens). The list sections are the link groups//! ([`ProjectSectionKey`], grouped by `super::project_sections`) plus the//! project's coding SESSIONS, which bind by `project_id` rather than by a link//! row, and its PROCEDURES — notes whose first line names a trigger, which//! `get_project` groups out of Notes. The two single things are the project's//! BRIEFING and its BRAND kit.//!//! **The order is Home's** (architect, 13 Oct 2026): a section is a tile on//! the database page, so it sits where its tile sits — its own view//! (`project/<section>/<slug>`) or, for a section a tool covers, that tool's//! list ([`tile_order`]). A section with no//! tile on Home keeps its default place after the ones that have one.//!//! Which sections are DRAWN is a second, separate question — the ones that//! hold something ([`present_sections`]). A section with nothing in it appears//! nowhere on the page.//!//! No clock, no database — a function of its arguments, which is what lets//! `project_page_tests.rs` state the rules as arithmetic.use crate::constants::ProjectSectionKey;/// One section of a project's page.#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]pub enum PageSection {    /// A group of linked records — people, todos, notes, the repositories.    Linked(ProjectSectionKey),    /// The project's coding sessions. A list, bound by `project_id`.    Sessions,    /// The project's procedures — the notes whose first line names the trigger    /// a session matches a task against (`memory_briefing::procedure_trigger`).    /// A list, grouped out of the linked notes, read by NAME. The page's only.    Procedures,    /// What an assistant is briefed with for this project. One thing.    Briefing,    /// The project's brand kit — colours, fonts, voice, logo files. One thing.    Brand,}/// Where each section that is not a link group sits by DEFAULT: straight after/// the named group. Written as a placement rather than as a second full list,/// so a link group added to [`ProjectSectionKey`] takes its own place in the/// page with no edit here.////// * **Briefing after Memory** — the memory entries are what a briefing is///   compiled from, and a reader checking what an assistant knows reads the///   two together./// * **Sessions after Code** — sessions are the work done IN the code; the///   repositories and what ran in them read as one run of the page./// * **Brand after Calls, so just above Passwords** — like a password, the///   brand kit is something you reach for to DO the work (a logo, a colour),///   not something you read to understand where the work stands./// * **Procedures after Notes** — a procedure IS a note, the one kind whose///   first line says when to read it, so it reads straight after the notes it///   was grouped out of.pub const PLACED_AFTER: [(PageSection, ProjectSectionKey); 4] = [    (PageSection::Briefing, ProjectSectionKey::Memory),    (PageSection::Procedures, ProjectSectionKey::Notes),    (PageSection::Sessions, ProjectSectionKey::Code),    (PageSection::Brand, ProjectSectionKey::Calls),];impl PageSection {    /// The key a page and a route both spell it by.    pub const fn as_str(self) -> &'static str {        match self {            PageSection::Linked(key) => key.as_str(),            PageSection::Sessions => "sessions",            PageSection::Procedures => "procedures",            PageSection::Briefing => "briefing",            PageSection::Brand => "brand",        }    }    /// A stored key read back, or `None` for one this build does not know.    pub fn parse(key: &str) -> Option<PageSection> {        match key {            "sessions" => Some(PageSection::Sessions),            "procedures" => Some(PageSection::Procedures),            "briefing" => Some(PageSection::Briefing),            "brand" => Some(PageSection::Brand),            other => ProjectSectionKey::parse(other).map(PageSection::Linked),        }    }    /// Whether this section holds ONE thing rather than a list — drawn with    /// no count, opening the thing itself.    pub const fn holds_one(self) -> bool {        matches!(self, PageSection::Briefing | PageSection::Brand)    }}/// Every page section in its DEFAULT order: the link groups in the crate's/// editorial order ([`ProjectSectionKey::ALL`]), with each of/// [`PLACED_AFTER`]'s sections straight after its anchor.pub fn default_order() -> Vec<PageSection> {    let mut out = Vec::with_capacity(ProjectSectionKey::ALL.len() + PLACED_AFTER.len());    for key in ProjectSectionKey::ALL {        out.push(PageSection::Linked(key));        for (placed, anchor) in PLACED_AFTER {            if anchor == key {                out.push(placed);            }        }    }    out}/// The sections in Home's order: each where its tile sits in `views` (Home's/// view ids, the person's order) — its own view, or the list of the tool that/// covers it ([`section_tool`]) — and the ones with no tile after, in/// [`default_order`].pub fn tile_order(views: &[String], slug: &str) -> Vec<PageSection> {    let place = |section: PageSection| {        let own = section_view(section, slug);        let covered = section_tool(section).map(crate::rules::home_arrangement::tool_view);        views.iter().position(|view| *view == own || Some(view) == covered.as_ref())    };    let mut out = default_order();    out.sort_by_key(|section| place(*section).unwrap_or(usize::MAX));    out}/// What a section holds, as far as the page draws it.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum Holding {    /// A list, and how many it holds. Zero is "nothing" and is not drawn.    Count(u64),    /// The one thing is there.    One,    /// Nothing at all — the section is not drawn.    Nothing,}/// One section a page draws, in its place.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct PresentSection {    pub section: PageSection,    pub holding: Holding,}/// The sections a page DRAWS, in `order` ([`default_order`] or/// [`tile_order`]), only the ones that hold something. `holding` answers for/// one section at a time and is asked once per section.////// A list section holding zero is absent, never a "0": a heading over nothing/// reads as a section that failed to load.pub fn present_sections(    order: &[PageSection],    holding: impl Fn(PageSection) -> Holding,) -> Vec<PresentSection> {    order        .iter()        .copied()        .filter_map(|section| match holding(section) {            Holding::Count(0) | Holding::Nothing => None,            held => Some(PresentSection { section, holding: held }),        })        .collect()}/// Whether a project's BRIEFING holds anything: its summary, or at least one/// memory entry. Those are what an assistant is briefed with for a project; a/// project with neither briefs a name and nothing else, and a section opening/// that would be a section about nothing.pub const fn briefing_holds(has_summary: bool, memory_entries: u64) -> bool {    has_summary || memory_entries > 0}#[cfg(test)]#[path = "project_page_tests.rs"]mod tests;// ── a section as a tile on Home ─────────────────────────────────────────────//// The database page is Home (architect, 13 Oct 2026): nothing on it draws a// list of rows, so what a project's sections held are views, each a tile// placed by the board like any other. A section a tool of the database already// covers is that tool's list; every other section that holds something is a// view of its own, `project/<section>/<slug>`, worn with an existing tool's// photograph./// The tool whose list covers a section: the section is that tool's records,/// so a database holding the tool draws no tile of its own for it.pub const SECTION_TOOLS: [(&str, &str); 12] = [    ("todos", "todos"),    ("notes", "notes"),    ("thoughts", "thoughts"),    ("people", "people"),    ("documents", "documents"),    ("chats", "messages"),    ("email", "email"),    ("events", "calendar"),    ("meetings", "meetings"),    ("money", "accounts"),    ("journal", "journal"),    ("sessions", "code"),];/// The tool covering `section`, if any ([`SECTION_TOOLS`]).pub fn section_tool(section: PageSection) -> Option<&'static str> {    let key = section.as_str();    SECTION_TOOLS.iter().find(|(name, _)| *name == key).map(|(_, tool)| *tool)}/// The tool whose photograph a section's tile wears — always a shipped tool's/// picture, never placeholder art: what the section holds is that tool's kind/// of record (a procedure, a decision and a briefing are written notes; a brand/// kit is pictures).pub fn section_wears(section: PageSection) -> &'static str {    use crate::constants::ProjectSectionKey as K;    match section {        PageSection::Sessions => "code",        PageSection::Procedures | PageSection::Briefing => "notes",        PageSection::Brand => "photos",        PageSection::Linked(key) => match key {            K::People => "people",            K::Chats => "messages",            K::Email => "email",            K::Documents => "documents",            K::Todos => "todos",            K::Memory => "memory",            K::Notes => "notes",            K::Journal => "journal",            K::Events => "calendar",            K::Money => "accounts",            K::Code => "code",            K::Photos | K::Albums => "photos",            K::Videos => "videos",            K::Calls => "calls",            K::Passwords => "passwords",        },    }}/// A section's own name, as its tile is called.pub fn section_title(section: PageSection) -> &'static str {    use crate::constants::ProjectSectionKey as K;    match section {        PageSection::Sessions => "Sessions",        PageSection::Procedures => "Procedures",        PageSection::Briefing => "Briefing",        PageSection::Brand => "Brand",        PageSection::Linked(key) => match key {            K::People => "People",            K::Chats => "Chats",            K::Email => "Email",            K::Documents => "Documents",            K::Todos => "Todos",            K::Memory => "Memory",            K::Notes => "Notes",            K::Journal => "Journal",            K::Events => "Events",            K::Money => "Money",            K::Code => "Code",            K::Photos => "Photos",            K::Videos => "Videos",            K::Albums => "Albums",            K::Calls => "Calls",            K::Passwords => "Passwords",        },    }}const SECTION_VIEW: &str = "project/";/// A project section's view id: `project/<section>/<slug>`.pub fn section_view(section: PageSection, slug: &str) -> String {    format!("{SECTION_VIEW}{}/{slug}", section.as_str())}/// The section and project slug a view id names, or `None` for any other view/// (or a section this build does not know).pub fn view_section(view: &str) -> Option<(PageSection, &str)> {    let (section, slug) = view.strip_prefix(SECTION_VIEW)?.split_once('/')?;    if slug.is_empty() || slug.contains('/') {        return None;    }    Some((PageSection::parse(section)?, slug))}