# Architecture

The layers and their dependency rules, the ports, how the map adapter works and how to add an adapter of your own.

This page is built from [ARCHITECTURE.md](https://github.com/MapCairn/mapcairn/blob/main/ARCHITECTURE.md) in the repository, which is where it is edited.

## At a glance

The document is long, so the site gives each section its own page. A section that is still long is cut into numbered parts.

- <span id="layers-and-dependency-rules"></span>[Layers and dependency rules](https://docs.mapcairn.com/architecture/layers-and-dependency-rules.md): Which package may import which, and what each layer must not do.
- <span id="building-the-sdk-packages"></span>[Building the SDK packages](https://docs.mapcairn.com/architecture/building-the-sdk-packages.md): How the six SDK packages are built, and in what order.
- <span id="ports"></span>[Ports](https://docs.mapcairn.com/architecture/ports-part-1.md): The interfaces the app depends on, each with a real adapter, a fake and a contract suite.
  - In 2 parts: [1](https://docs.mapcairn.com/architecture/ports-part-1.md), [2](https://docs.mapcairn.com/architecture/ports-part-2.md)
- <span id="the-map-adapter"></span>[The map adapter](https://docs.mapcairn.com/architecture/the-map-adapter.md): MapLibre GL JS is the only map engine, and the ui never imports it.
- <span id="data-layers"></span>[Data layers](https://docs.mapcairn.com/architecture/data-layers.md): How data layers are defined, loaded and shown on the map.
- <span id="drawing"></span>[Drawing](https://docs.mapcairn.com/architecture/drawing.md): How shapes are drawn, edited, imported, exported and shared.
- <span id="adding-an-adapter"></span>[Adding an adapter](https://docs.mapcairn.com/architecture/adding-an-adapter.md): An adapter reaches the ui through the services.
- <span id="how-tests-substitute-services"></span>[How tests substitute services](https://docs.mapcairn.com/architecture/how-tests-substitute-services.md): Unit tests wrap components in ServicesProvider with fakes.
- <span id="the-reference-hosts-pages"></span>[The reference host's pages](https://docs.mapcairn.com/architecture/the-reference-hosts-pages.md): The routes of the reference site.

## About this document

MapCairn separates what the app does from what it runs on. The interface and the pure logic depend on small interfaces (ports). Concrete code that touches a map engine, a browser service or a network service (adapters) lives in one place each and is wired together once, in the composition root.

```mermaid
flowchart TD
  web["web, the host<br/>createServices(), web adapters"]
  map["map<br/>user interface"]
  maplibre["adapter-maplibre<br/>MapLibre adapter"]
  core["core<br/>ports, logic"]
  theme["theme"]
  react["react"]
  web --> map
  web --> maplibre
  map --> core
  map --> theme
  map --> react
  maplibre --> core
  maplibre --> theme
```

An arrow means "imports". Each box is a package under `packages/`, so `core` is `packages/core`. `core` holds the ports (types only), the pure logic, the fakes and the contract suites, and imports no other package, as do `theme` (the tokens and their contrast maths) and `react` (generic hooks). Only the host imports the MapLibre adapter, and no SDK package imports the host. The host also imports `core` and `theme` directly, which the picture leaves out.
