PureYAML/docs/rules-swift/README.md
Mihaela M 5f3892f2c1 chore(rules): vendor the rules-swift canon into docs/rules-swift
Mirror the shared Swift rules canon (rules-swift, commit 4ebb892) into
docs/rules-swift/ as a read-only reference alongside the repo's tailored
docs/rules/. PROVENANCE.md records the source, commit, and what was
included or excluded; the upstream repo's own VCS, CI, and lint tooling
is omitted as machinery rather than rule content.

Exclude the vendored tree from the style gate (scripts/check-style.sh)
and the pre-commit hook: the canon's enforcement docs quote the
forbidden attribution phrases to define them, the same reason the
repo's own enforcement files are already excluded.
2026-06-26 05:36:12 +02:00

8.6 KiB

Swift coding rules (canonical)

The canonical, scrubbed Swift coding rules. This is the Swift domain: the language- and platform-specific craft of writing Swift well. It sits on top of a vendored core spine of cross-cutting engineering discipline (see core/).

Each file here is one Swift rule area. CONVENTIONS.md is the short overview; this repository is the full set, with the cross-cutting discipline vendored under core/. Examples use a sample tile-based static site generator named Tiledown (the TileKit library plus the tile-down executable); replace the example names with your project's when you adopt these.

The core spine

The cross-cutting engineering discipline that governs how work is done and reported, independent of language or domain, lives in core/. The Swift rules here do not duplicate it; they build on it. Read the core spine first:

  • core/no-shortcuts-first-principles.md - the core ethic: no shortcuts, derive from first principles, hold to Knuth's standard of care.
  • core/proof-discipline.md - framing a correctness claim as separately provable layers, each with an explicit epistemic status.
  • core/round-trip-transformation.md - any two-way transform (parse/print, encode/decode) is one invertible description, proven by the round-trip law.
  • core/first-principles-analysis.md - depth target for analysis, plus measurement discipline (every number traceable to a command).
  • core/verification.md - no "done" without fresh command output.
  • core/testing-discipline.md - real tests on every change; a build is not a test.
  • core/systematic-debugging.md - reproduce, isolate, explain, then fix.
  • core/brainstorming.md and core/writing-plans.md - design and planning gates before non-trivial work.
  • core/commits.md, core/git-discipline.md - Conventional Commits; issues, labels, branches, PRs, remotes.
  • core/file-naming.md, core/folder-grouping.md - filename conventions and when to flatten one-file folders.
  • core/rules.md, core/self-improve.md - authoring rules for an agent, and when a recurring correction should become one.

Where an app starts

Before any UI framework is named, an app starts at its business rules and the domain that enforces them.

  • business-rules-and-constraints.md - MANDATORY, the first artifact: the business rules written as explicit checkable statements, plus the constraints that bound them (which external REST services and their contracts, what compliance, which auth model), defined up front.
  • domain-first.md - MANDATORY: an app starts at the domain and business rules, framework-free and headless-provable; the UI framework is the last, reversible, and plural choice, never the first decision.

Language and platform craft (engine, today)

  • code-style.md - namespacing discipline, file naming, one-type-per-file.
  • namespacing.md - caseless enum vs struct vs class for namespace anchors.
  • dependency-injection.md - no singletons, inject every collaborator through init, protocol seams.
  • concurrency.md - Swift 6 strict concurrency: Sendable, actors, @MainActor.
  • cross-platform.md - the same sources build on Apple platforms and Linux (and Windows where noted); guard platform-divergent code behind a protocol seam.
  • linux-server.md - server-side operational rules for the serve command and any networking.
  • testing.md - Swift Testing, @Test / #expect, test isolation.
  • formatting-and-linting.md - SwiftFormat and SwiftLint, enforced by a pre-commit hook and again in CI.
  • documentation.md - DocC catalogs and /// requirements.
  • documentation-search.md - verify API facts against an authoritative documentation index, not from memory: query it with keywords and read the results for Apple-API facts; it is a search engine, not an agent.
  • framework-policy.md - stay inside Apple's own SDKs and the Swift-native ecosystem; Linux falls back to C or C++ only.

Packages

ExtremePackaging

The granular packaging doctrine lives under exp/: a monorepo split into many single-responsibility SPM packages with explicit, unidirectional dependencies, for isolated compilation, parallel builds, and per-package testability. The two core files are loaded on every Swift session; the rest load on demand.

Parsing and validation

  • parsing-rules.md - parse first, validate second; the OpenAPIKit parsing idiom over any structured input.
  • validation-rules.md - composable Validation<Subject> values, never imperative if-trees; every public type is validated or excluded with a reason, enforced mechanically.

UI

The UI rules live under ui/. Any code with UI keeps the renderer thin over a framework-free model, and tests the flows from a shared scenario corpus.

  • ui/pre-ui-layer.md - MANDATORY: all display state and behavior in a framework-free, value-typed model (Domain + Surface); one perform(Intent) write channel; renderers are the only UI import, gated mechanically.
  • ui/three-renderers.md - build the same UI in all three Apple SDKs (SwiftUI, UIKit, AppKit) over the one model: the architecture's payoff and a comparison instrument for which framework behaves best in a scenario.
  • ui/swiftui-views.md - the SwiftUI renderer: view architecture and identity.
  • ui/uikit-views.md - the UIKit renderer: UIViewController/UIView adapters, diffable data sources, re-pull on the change signal.
  • ui/appkit-views.md - the AppKit renderer: NSViewController/NSView adapters, withObservationTracking, programmatic menus.
  • ui/view-models.md - view-model responsibilities and patterns.
  • ui/components.md - the component system.
  • ui/colors.md - the color system.
  • ui/fonts.md - font registration in SPM packages.
  • ui/pom.md - Page Object Model for UI tests: screens behind page objects, identifiers from one shared source.
  • ui/flowspec.md - declarative UI scenarios driven by FlowSpec: one flow, many renderers, replayed headless and on-device.