Cotton code
207 lines · 8.8 KB · RustRaw
//! Project templates — a project seeded for the work somebody actually does//! (roadmap item 14). The port of `packages/core/src/templates.ts`.//!//! The owner's words: "maybe we could also have professions as a list, and for//! each profession we're actually supporting we can have a template — cab//! drivers, delivery people."//!//! ONE declaration, three surfaces, exactly like [`crate::recipes`]: the server//! projects this list onto MCP (`create_project`'s optional `template`, whose//! schema description says what each one seeds), the owner route//! `POST /v1/projects/from-template` seeds from the SAME entry, and the app's//! New project sheet renders the same names and descriptions in its picker. The//! app never seeds locally — it asks the server — because the seeding rules//! live in one place and two copies of "what a cab driver's project starts//! with" would drift the day one changed.//!//! A template is DATA. It generates no behaviour and adds no schema: every seed//! it names is a row one of the existing writers already knows how to write — a//! todo (with an RRULE [`crate::transforms::rrule`] parses), a money account on a//! subtype the money engine already offers, and the project's own description.//!//! Every string here is read twice over — by an assistant through//! `create_project`'s `template` description, and by a person in the picker —//! so all of them//! were compared BYTE FOR BYTE against what Node printed, and the language//! sweep `toolLanguage.test.ts` runs over the templates comes over whole in//! `templates_tests.rs`.use crate::constants::RealAccountSubtype;/// A todo the template seeds. `rrule` is iCal, parsed by/// [`crate::transforms::rrule`].#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct TemplateTodo {    pub title: &'static str,    /// The recurrence, or `None` for a one-off. None is a real answer, not a    /// missing one: a driving licence has no useful cadence, and inventing a    /// yearly rule for it would put a wrong reminder in somebody's list    /// forever.    pub rrule: Option<&'static str>,}/// The one money account a template opens.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct TemplateMoneyAccount {    pub name: &'static str,    /// Constrained to [`RealAccountSubtype`] — the places a person's money    /// actually sits. A template may never mint a `category` or a `receivable`:    /// those are shapes the money engine derives or a person-facing verb    /// creates, and seeding one would put a row in the tree nothing else    /// expects. The TypeScript narrows with a type; so does this.    pub subtype: RealAccountSubtype,}/// What a template puts in a new project. Every field is a row an existing/// writer already writes; nothing here is a new kind of record.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct TemplateSeeds {    pub todos: &'static [TemplateTodo],    /// `None` when the profession's money does not want an account of its own.    pub money_account: Option<TemplateMoneyAccount>,    /// The project's summary seed — what the owner sees as the description.    pub description: &'static str,}#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct ProjectTemplate {    /// kebab-case; the id the wire and the picker both use.    pub id: &'static str,    /// What the project is called when the owner does not name it themselves.    pub name: &'static str,    /// One line, shown next to the name in the picker.    pub description: &'static str,    /// The profession this template serves, in the owner's plain words.    pub profession: &'static str,    /// Reserved, and deliberately UNINHABITED rather than left out.    ///    /// A template will one day carry the look a project wears — a colour, an    /// icon, a cover. None of that exists yet, and a widened type would let a    /// half-built field ship silently. The TypeScript writes `brand: null` and    /// types it `null` for that reason; `Option<Infallible>` is the same    /// sentence in Rust — the only value it can hold is `None`, so opening the    /// field means changing this type on purpose.    pub brand: Option<std::convert::Infallible>,    pub seeds: TemplateSeeds,}/// The three professions, in picker order.pub const PROJECT_TEMPLATES: [ProjectTemplate; 3] = [    ProjectTemplate {        id: "cab-driver",        name: "Driving",        description: "For a cab driver: the papers that expire, the vehicle checks, and the cash that comes in.",        profession: "Cab driver",        brand: None,        seeds: TemplateSeeds {            todos: &[                TemplateTodo {                    title: "Renew the vehicle insurance",                    rrule: Some("FREQ=YEARLY"),                },                TemplateTodo {                    title: "Renew the commercial permit",                    rrule: Some("FREQ=YEARLY"),                },                TemplateTodo {                    title: "Renew the fitness certificate",                    rrule: Some("FREQ=YEARLY;INTERVAL=2"),                },                TemplateTodo {                    title: "Book the pollution check",                    rrule: Some("FREQ=MONTHLY;INTERVAL=6"),                },                TemplateTodo {                    title: "Check the car: tyres, brakes, oil, lights",                    rrule: Some("FREQ=WEEKLY;BYDAY=SU"),                },            ],            money_account: Some(TemplateMoneyAccount {                name: "Fare cash",                subtype: RealAccountSubtype::Cash,            }),            description: "My driving work: the car's papers and renewals, the weekly checks, what I earn and what the car costs me to run.",        },    },    ProjectTemplate {        id: "delivery-partner",        name: "Deliveries",        description: "For a delivery rider: payouts to check, the bike to keep running, and papers to renew.",        profession: "Delivery partner",        brand: None,        seeds: TemplateSeeds {            todos: &[                TemplateTodo {                    title: "Renew the two-wheeler insurance",                    rrule: Some("FREQ=YEARLY"),                },                TemplateTodo {                    title: "Service the bike",                    rrule: Some("FREQ=MONTHLY;INTERVAL=3"),                },                TemplateTodo {                    title: "Check the week's payout against the app's statement",                    rrule: Some("FREQ=WEEKLY;BYDAY=MO"),                },                TemplateTodo {                    title: "Renew the driving licence",                    rrule: None,                },            ],            money_account: Some(TemplateMoneyAccount {                name: "Earnings wallet",                subtype: RealAccountSubtype::Wallet,            }),            description: "My delivery work: what each app pays out, what actually reaches my account, and what the bike costs to keep on the road.",        },    },    ProjectTemplate {        id: "small-seller",        name: "Selling from home",        description: "For someone selling food or goods from home: orders, stock, costs and the licence.",        profession: "Small seller",        brand: None,        seeds: TemplateSeeds {            todos: &[                TemplateTodo {                    title: "Count the cash and match it to the day's orders",                    rrule: Some("FREQ=DAILY"),                },                TemplateTodo {                    title: "Restock what sold out",                    rrule: Some("FREQ=WEEKLY;BYDAY=MO"),                },                TemplateTodo {                    title: "Check my prices against what ingredients now cost",                    rrule: Some("FREQ=MONTHLY;INTERVAL=3"),                },                TemplateTodo {                    title: "Renew the food licence",                    rrule: Some("FREQ=YEARLY"),                },            ],            money_account: Some(TemplateMoneyAccount {                name: "Sales account",                subtype: RealAccountSubtype::Bank,            }),            description: "What I sell from home: the orders, what each item costs me to make, what I need to restock, and the money that comes in.",        },    },];/// The template with this id, or `None` — the ONE lookup, so the server, the/// route and the app never disagree about what an unknown id means.pub fn project_template(id: &str) -> Option<&'static ProjectTemplate> {    PROJECT_TEMPLATES.iter().find(|template| template.id == id)}/// Every template id, for a wire enum and for an error that can name them.pub const PROJECT_TEMPLATE_IDS: [&str; 3] = [    PROJECT_TEMPLATES[0].id,    PROJECT_TEMPLATES[1].id,    PROJECT_TEMPLATES[2].id,];#[cfg(test)]#[path = "templates_tests.rs"]mod tests;