Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Browse the source code

All of Nifty is open source on GitHub: github.com/istorry/nifty.

Each part below links to two places:

  • Source opens the code on GitHub.
  • API docs opens the generated Rust documentation for that crate: every type and function, with its doc comments, cross-linked, and a source link on each item.

Not sure where to start? Read How Nifty works first; it explains how these pieces fit together.

The repository at a glance

nifty/
├── crates/              the Rust code that makes up Nifty
│   ├── nifty-syntax/      text → syntax tree
│   ├── nifty-check/       syntax tree → checked program
│   ├── nifty-interp/      runs a checked program
│   ├── nifty-codegen/     compiles a program to machine code and links it
│   ├── nifty-rt/          the runtime library compiled apps link against
│   ├── nifty-runtime/     widgets, layout and the engine apps run on, on any OS
│   ├── nifty-download/    `download`, for interpreted and compiled apps
│   ├── nifty-platform/    picks the backend for this OS
│   ├── nifty-macos/       the macOS (AppKit) backend
│   ├── nifty-windows/     the Windows (Win32) backend
│   ├── nifty-lsp/         the language server for editors
│   └── nifty-cli/         the `nf` command
├── editors/             editor extensions and grammars
│   ├── vscode/
│   ├── zed/
│   └── tree-sitter-nifty/
├── fixtures/            .nf programs the tests read
└── docs/                this book

Crates

nifty-syntax: reading source code

Turns text into a syntax tree, with error recovery. Source · API docs

FileWhat’s in it
lexer.rssplits text into tokens; decides which line breaks matter
token.rsthe token kinds and the keyword list
parser/the parser, split into items, statements and expressions
ast.rsthe syntax tree types
diagnostic.rs, span.rserror messages and source positions, each in one file of the project
project.rswhich .nf files make up a project

nifty-check: understanding the program

Resolves names, checks types, and records what state each part of the UI reads. Source · API docs

FileWhat’s in it
elements.rsthe one table of built-in UI elements and their arguments
functions.rsthe one table of built-in functions, such as download
methods.rsthe one table of built-in list and string methods
hir.rsthe checked program that the interpreter runs
checker/the checker: declarations, statements, expressions, UI, arguments, constants
tests/grammars.rschecks the editor grammars, docs highlighter and reference lists against those tables

nifty-interp: running the program

Runs a checked program on the runtime’s engine, evaluating every dynamic part of the UI by walking the checked code. Source · API docs

FileWhat’s in it
app.rsstarts and reloads a program on the engine
lower.rsturns checked windows and views into the engine’s templates
logic.rsthe interpreter as the engine’s Logic: text, bindings, actions and regions
live.rsan app whose program can be swapped while it runs, for nf dev
machine/carry.rswhich state values survive a reload
machine/evaluates statements and expressions; reads and writes places
value.rsruntime values

nifty-codegen: compiling to machine code

Compiles programs to object files with Cranelift, and links them against nifty-rt with the system’s linker: cc from Xcode’s command line tools on macOS, and the MSVC linker on Windows. Source · API docs

FileWhat’s in it
compile/compiles a checked program and emits the descriptor the runtime reads
compile/body.rsstatements, expressions and places, with the reference counting that keeps copies independent
compile/ui.rswindows, views, templates, and the functions behind their dynamic parts
emit.rswrites functions and data into an object file, or into memory for tests
object.rsan app’s object file, with the main that runs it
link.rslinks an object and the runtime library into an executable
tests/compiled.rsruns programs compiled and interpreted side by side, and checks they match
tests/link.rscompiles, links and runs a program for real; build the workspace first

A static library holding everything a compiled app needs at run time. Compiled code calls its nf_* functions and hands nifty_rt_main a descriptor of its states, windows and views, which it shows through the same engine the interpreter uses. Source · API docs

FileWhat’s in it
program.rsthe descriptor compiled programs emit, whose layout is the contract with nifty-codegen
value.rsvalues in 64-bit slots; reference-counted strings, lists and structs
abi/the nf_* calls, and who owns the values passed to them
context.rsstate, call depth, and why code failed
logic.rs, lower.rs, show.rscompiled code as the engine’s Logic
place.rsplaces in state that later writes go to, as compiled code names them
download.rs, dialog.rsdownload and the file dialogs in compiled apps
symbols.rsthe names compiled code links to

nifty-download: downloads

Runs each download on its own thread and hands its reports back to the UI thread, for both the interpreter and compiled apps. Source · API docs

nifty-runtime: widgets, layout and the engine

The OS-independent widget tree and flexbox layout, the Backend trait each OS implements, and the engine that keeps an app’s windows in step with its state. Source · API docs

FileWhat’s in it
reactive/the engine: mounts templates, handles events, updates only what changed, and the Logic trait that gives templates their values
state.rs, failure.rswhich state UI reads; why running code fails, the same however it runs
ui.rsthe retained UI: create, arrange and update widgets
backend.rsthe Backend trait a platform implements, and the Waker other threads use to reach the UI thread
dialog.rs, paths.rsfile dialogs: what an app asks for, and the queue that shows them between events and hands back the answers; ~ in paths
layout.rsflexbox layout with Taffy
style.rs, geometry.rstext/button/stack styles; points, sizes, frames
testing.rsan in-memory fake backend for tests

nifty-macos, nifty-windows and nifty-platform: real native widgets

nifty-macos implements Backend with AppKit, and nifty-windows implements it with Win32 common controls. Each backend crate compiles to nothing on other systems, so the workspace builds on both macOS and Windows; CI runs every test on each. nifty-platform picks the backend for the OS you build on. Windows executables need the application manifest that nifty-cli’s build.rs embeds, which turns on Common Controls 6 and per-monitor DPI awareness. macOS source · macOS API docs · Windows source · platform source · platform API docs

nifty-lsp: editor support

The language server behind nf lsp. Source · API docs

FileWhat’s in it
server.rsthe request loop
workspace.rsopen files and the projects they belong to
project.rsa project’s files, analysed together
document.rsone file’s text, syntax tree and position conversion
index/an index of every declaration and use, for navigation
features/diagnostics, completion, semantic tokens, outline, navigation

nifty-cli: the nf command

nf, the tool you use. Source · API docs

FileWhat’s in it
lib.rsthe usage text; parses the command line and dispatches
main.rsthe entry point
create.rs, run.rs, dev.rs, check.rs, build.rs, lsp.rsone file per command
build/macos.rswrites the .app bundle around the compiled executable
build/windows.rswrites the Windows app folder around the compiled executable
frontend.rsreads a project, checks it, and says where spans are; shared by every command
report.rsprints errors with the source line underneath
templates/main.nfthe starter app nf create writes

Editors

FolderWhat’s in it
editors/vscodeVS Code extension: TextMate grammar + language client
editors/zedZed extension: Tree-sitter queries + language server setup
editors/tree-sitter-niftythe Tree-sitter grammar

This book

The docs you’re reading are in docs/, written in Markdown and built with mdBook. Every page has an edit button at the top right.