lmjtfy.git / apps / lmjtfy / src / view / README.md
1# Chapter 4: view/, pages as pure functions
2
3Here is a small idea that makes everything easier to test: **a page is a
4function**. It takes what is known (the question, Jev's answers, the
5calls that were made) and returns HTML. It never fetches anything, never
6looks at the clock, never touches a database. Give it the same input and you
7get the same page, every time.
8
9That is what `view.rs` (one folder up) and this folder are. The Worker does
10the fetching, then hands a plain value to a view, and the view hands back
11markup. So every page can be tested on a laptop with `cargo test`, no
12Cloudflare involved, and the tests can check things like "the JSON shown is
13exactly the JSON sent" by parsing it back.
14
15```mermaid
16flowchart LR
17  W["Worker: fetches, calls, waits"] -->|"plain values"| V["view: pure"]
18  V -->|"HTML (maud, escaped)"| B["Browser"]
19```
20
21> **Aside.** The HTML is written with maud, a Rust macro that looks a little
22> like HTML without the angle brackets: `div .win { p { (text) } }`. Anything
23> in parentheses is escaped, so a question someone typed, or an option an
24> LLM wrote, cannot become markup. That matters here, because both of those
25> are strangers' words shown to other strangers.
26
27> **Try it.** Every page you are reading right now came out of `code.rs`.
28> Open <https://lmjtfy.fun/lmjtfy.git/apps/lmjtfy/src/view/code.rs>
29> to see the function that drew it.
30
31## For the people who maintain it
32
33| File | What |
34| --- | --- |
35| [code.rs](code.rs) | The code pages, laid out like an editor: a sidebar (explorer, outline, clone, latest commit) beside the page, a folder's README and CLAUDE.md as tabs, a source file as Docco reads one (its comments rendered on the left, the code they are about on the right, coloured), markdown rendered, and the mermaid drawer (pinned, themed to the site, loaded only where a diagram is). The Worker hands it a `Frame` (where you are, and the whole tree) and a `Shown` (what is there). |
36| [toast.rs](toast.rs) | The toasts pushed over `/live` when someone asks or clones. A question is shown only if the feed may show it. |
37| [tests.rs](tests.rs) | `view.rs`'s tests: what each kind of answer prints, that bodies shown parse back to the bodies sent, and the page's pieces. |
38
39← Previous: [Chapter 3, the source](../) · Up: [src](../) · Next: [Chapter 5, packages/](../../../../packages/) →