lmjtfy.git / apps / lmjtfy / src / view

Chapter 4: view/, pages as pure functions

Here is a small idea that makes everything easier to test: a page is a function. It takes what is known (the question, Jev's answers, the calls that were made) and returns HTML. It never fetches anything, never looks at the clock, never touches a database. Give it the same input and you get the same page, every time.

That is what view.rs (one folder up) and this folder are. The Worker does the fetching, then hands a plain value to a view, and the view hands back markup. So every page can be tested on a laptop with cargo test, no Cloudflare involved, and the tests can check things like "the JSON shown is exactly the JSON sent" by parsing it back.

flowchart LR
  W["Worker: fetches, calls, waits"] -->|"plain values"| V["view: pure"]
  V -->|"HTML (maud, escaped)"| B["Browser"]

Aside. The HTML is written with maud, a Rust macro that looks a little like HTML without the angle brackets: div .win { p { (text) } }. Anything in parentheses is escaped, so a question someone typed, or an option an LLM wrote, cannot become markup. That matters here, because both of those are strangers' words shown to other strangers.

Try it. Every page you are reading right now came out of code.rs. Open https://lmjtfy.fun/lmjtfy.git/apps/lmjtfy/src/view/code.rs to see the function that drew it.

For the people who maintain it

FileWhat
code.rsThe 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).
toast.rsThe toasts pushed over /live when someone asks or clones. A question is shown only if the feed may show it.
tests.rsview.rs's tests: what each kind of answer prints, that bodies shown parse back to the bodies sent, and the page's pieces.

← Previous: Chapter 3, the source · Up: src · Next: Chapter 5, packages/ →