Cotton code
252 lines · 9.0 KB · RustRaw
//! THE ROUTE TABLE — every screen the phone has, as the path string it is//! reached by.//!//! Design P6: **the route STRINGS are wire protocol and do not move.** The//! Android keyboard's links carry them,//! `NOTIFICATION_ROUTES` maps to them, and `tinmen-cotton://capture` is a//! documented entry point. So they are declared here, in Rust, once — and 4b's//! Swift `Route` enum is held to this list rather than being a second opinion//! about what the app's screens are.//!//! **Why Rust owns the list even though the enum is Swift's.** The reads//! (`super::reads`) are named PER SCREEN, and the guard that makes that//! declaration mean anything is a totality test between the two tables. A test//! can only compare two tables it can both see, and only one of them is in a//! language that compiles on Linux. Putting the routes here also makes the//! Swift enum's job smaller: it is a parser and a formatter over a list//! somebody else already agreed, which is exactly what P6 asks for.//!//! The table is the app's screens, one path each — including `/activity`, the//! assistant-runs screen, which `read.activity` answers.//!//! The redesign (2026-09-14, phase 15) folded the two `/decide/*` screens//! into ONE `/decide` — every pending decision as a paper slip with a tick//! and a cross — and replaced `/find` with `/search`, the everything-search//! drawn as objects. `/inbox` and `/catalogue` never returned: the owner's//! earlier ruling ("add nothing new; move everything as it is") struck them by//! name, and the paper-kit spec is what settled the two that did change.//!//! **Step 5c deleted that app, and with it the derivation.** This table is now//! the DECLARATION rather than a copy of one, and two guards replace the walk//! that used to check it. Between them they catch the same failure from both//! ends: `RouteTableTests.swift` in the phone lane reads this file and holds//! the Swift `Route` enum to it, and `reads_tests.rs` holds every entry here//! to a read or a written deferral. A screen that exists and is not here is//! red in the first; an entry here that answers nothing is red in the second.//! A Rust test cannot do the first half itself — it cannot parse a Swift enum,//! and `cotton-ffi` compiles on Linux where the Swift package does not./// Every route, sorted. A route added here needs a read beside it in/// [`super::reads`] or a line in that module's deferral list saying which/// sub-step owes one — which is the whole point of declaring both.pub const ROUTES: &[&str] = &[    "/",    "/about",    "/about/help",    "/about/whats-new",    "/activity",    "/alarms",    "/backup",    "/bin",    "/briefing",    "/bring-in",    "/calendar",    "/calendar/event/[id]",    "/calendar/event/edit",    "/calendar/month",    "/calls/call/[id]",    "/calls/history",    "/camera",    "/capture",    "/code",    "/code/defaults",    "/code/new",    "/database",    "/device",    "/device/[page]",    "/devices",    "/devices/[id]",    "/devices/[id]/storage",    "/documents",    "/documents/document/[id]",    "/documents/document/[id]/viewer",    "/documents/import",    "/email",    "/email/[id]",    "/export",    "/goals",    "/goals/goal/[id]",    "/home",    "/journal",    "/journal/[date]",    "/keyboard-take",    "/legal/privacy",    "/legal/terms",    "/lists/[table]",    "/lists/[table]/[id]",    "/locations",    "/locations/location/[id]",    "/me",    "/meetings",    "/meetings/meeting/[id]",    "/meetings/record",    "/members",    "/members/[id]",    "/members/admit",    "/memory/[id]",    "/memory/dropped",    "/messages",    "/messages/[thread]",    "/messages/[thread]/info",    "/messages/[thread]/media",    "/messages/import",    "/messages/new",    "/messages/starred",    "/money",    "/money/accounts/[id]",    "/money/statements/[id]",    "/money/transactions/[id]",    "/mood",    "/news",    "/news/edition/[id]",    "/news/settings",    "/news/topic/[id]",    "/news/topic/new",    "/notes",    "/notes/journal-today",    "/notes/note/[id]",    "/passwords",    "/passwords/[id]",    "/people",    "/people/import",    "/people/person/[id]",    "/people/person/[id]/[section]",    "/photos",    "/photos/album/[id]",    "/photos/album/[id]/add",    "/photos/albums",    "/photos/duplicates",    "/photos/people",    "/photos/photo/[id]",    "/projects",    "/projects/[slug]",    "/projects/[slug]/[section]",    "/projects/[slug]/brand",    "/projects/[slug]/code",    "/projects/[slug]/feedback",    "/projects/[slug]/feedback/[id]",    "/projects/[slug]/memory",    "/projects/[slug]/todos",    "/reader",    "/reader/page/[id]",    "/rejected",    "/search",    "/server",    "/sessions/[id]",    "/sessions/[id]/terminal",    "/settings/join-request/[id]",    "/storage",    "/storage/[id]",    "/take",    "/thoughts",    "/thoughts/thought/[id]",    "/todos",    "/todos/done",    "/todos/dropped",    "/todos/todo/[id]",    "/tools",    "/tools/board/[id]",    "/videos",    "/videos/video/[id]",    "/voice-words",    "/voice-words/[id]",];/// Is this a route this app has?////// Exact, never a prefix match: `/todos/todo/[id]` and `/todos/done` are two/// screens, and a parser that answered "yes" to `/todos/anything` would send a/// notification tap to the wrong one.pub fn is_route(path: &str) -> bool {    ROUTES.contains(&path)}/// The screens a record opens on, as `opens.screen` says them.pub const SCREENS: &[&str] = &["deck", "reader", "viewer", "chat", "group", "whole", "outside"];/// WHERE A RECORD OPENS, answered from its table's definition and never/// from a list of table names: `shell.opened` (made cards), `shell.list`/// rows and `shell.today` cards carry it as `opens`, one shape for Swift/// and Android alike:////// ```json/// { "table", "id", "path", "screen": "deck|reader|viewer|chat|group|whole|outside",///   "opening": "reader|whole|group|conversation|viewer|reading|opened" }/// ```////// - A row standing for another record (the definition's `opens`: a///   sighting's thread, a comment's record) opens THAT record — `table` and///   `id` are the one it stands for, read through `opens.column` and///   `opens.table` (else the table its `table_column` names, else the///   column's `link_to`), its path and///   screen that table's./// - `path` is a route of [`ROUTES`] filled in: the definition's///   `record_route` with each parameter the record's id (`[table]` the///   table's own id), else the generic `/lists/<table>/<id>`./// - `opening` is the deck's own word for a tap on the card///   (`shell_deck::opening`); `screen` the kind of screen it is: the///   reader for a book (`reader`) and a card of its own words (`reading`),///   the viewer, a thread's conversation (`chat`), a group's view, a whole///   into its parts — and for a card that opens at rest, `outside` where the///   table names a `record_route` (a screen of its own, outside the deck),///   else `deck`.pub fn opens(    definition: &cotton_core::definitions::TableDefinition,    id: &str,    record: &serde_json::Map<String, serde_json::Value>,    face: &str,    table: &dyn Fn(&str) -> Option<cotton_core::definitions::TableDefinition>,) -> serde_json::Value {    if let Some(stands) = &definition.opens {        let target = stands.table.clone().or_else(|| stands.table_column.as_ref().and_then(|column| record.get(column)).and_then(serde_json::Value::as_str).map(str::to_string))            .or_else(|| definition.column(&stands.column).and_then(|column| column.link_to.clone()));        let other = record.get(&stands.column).and_then(serde_json::Value::as_str).filter(|other| !other.is_empty());        if let (Some(other), Some(target)) = (other, target.and_then(|target| table(&target))) {            return opens(&target, other, &serde_json::Map::new(), "", &|_| None);        }    }    let opening = crate::methods::shell_deck::opening(definition, face, record);    let routed = definition.record_route.as_deref().filter(|route| is_route(route));    let screen = match opening {        "reader" | "reading" => "reader",        "conversation" => "chat",        "viewer" => "viewer",        "group" => "group",        "whole" => "whole",        _ if routed.is_some() => "outside",        _ => "deck",    };    let path = match routed {        Some(route) => fill(route, &definition.id, id),        None => format!("/lists/{}/{id}", definition.id),    };    serde_json::json!({ "table": definition.id, "id": id, "path": path, "screen": screen, "opening": opening })}/// `route` with each `[parameter]` filled: `[table]` by the table's id,/// any other by the record's.fn fill(route: &str, table: &str, id: &str) -> String {    route        .split('/')        .map(|segment| match segment.strip_prefix('[').and_then(|inner| inner.strip_suffix(']')) {            Some("table") => table.to_string(),            Some(_) => id.to_string(),            None => segment.to_string(),        })        .collect::<Vec<_>>()        .join("/")}#[cfg(test)]#[path = "routes_tests.rs"]mod tests;