# d0iasm/sababook architecture

> Architecture diagram of the GitHub repository d0iasm/sababook, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/d0iasm/sababook
- Repository: https://github.com/d0iasm/sababook

## Overview

##### Project type and purpose
· This is an educational browser engine called “SaBA” (Sample Browser Application), implemented in Rust and organized as a sequence of chapters (ch0–ch7). Each chapter incrementally adds functionality: from URL parsing (ch2), to HTTP (ch3), HTML parsing (ch4), CSS styling (ch5), GUI (ch6), and JavaScript execution (ch7).
· It is akin to a compiler or interpreter pipeline combined with a rendering engine and minimal UI layer, rather than a typical web-app frontend/backend.

##### File structure
· Top-level directories ch0–ch7 each contain a “saba” Cargo project.
· Within each:
– The “saba_core” sub-crate holds the core browser engine logic (URL handling, HTTP client interface, parsers, DOM, CSSOM, layout, page rendering, JS).
– The “net/wasabi” sub-crate implements a lightweight HTTP client backend.
– In later chapters (ch6+), an additional “ui/wasabi” sub-crate provides a GUI frontend.
· Build scripts: run_on_wasabi.sh, rust-toolchain.toml.
· Tests and sample HTML files exist to illustrate functionality.

##### README
· Confirms the chapter mapping and core purpose: to show how a browser works under the hood.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Runner
    Runner["Runner / CLI\n(main.rs)"]:::core

    %% Network Layer
    subgraph "Network Layer"
        NetLib["HTTP Client\n(lib.rs)"]:::network
        NetHTTP["HTTP Client\n(http.rs)"]:::network
    end

    %% Core Engine
    subgraph "Core Engine (saba_core)"
        CoreRoot["saba_core crate\n(lib.rs)"]:::core

        URL["URL Module"]:::parsing
        CoreHTTP["HTTP Module"]:::parsing

        subgraph "HTML Parsing"
            HTMLParser["HTML Parser"]:::parsing
            HTMLToken["HTML Tokenizer"]:::parsing
            HTMLAttr["HTML Attributes"]:::parsing
        end

        subgraph "DOM Builder"
            DOMApi["DOM API"]:::parsing
            DOMNode["DOM Node"]:::parsing
        end

        subgraph "CSS Parsing"
            CSSOM["CSSOM"]:::parsing
            CSSToken["CSS Tokenizer"]:::parsing
        end

        subgraph "Layout Engine"
            Computed["Computed Style"]:::layout
            LayoutObj["Layout Object"]:::layout
            LayoutView["Layout View"]:::layout
        end

        subgraph "Rendering Pipeline"
            Page["Page Renderer"]:::layout
        end

        subgraph "JavaScript Engine"
            JSAST["JS AST"]:::js
            JSToken["JS Tokenizer"]:::js
            JSRt["JS Runtime"]:::js
        end
    end

    %% UI Layer
    subgraph "UI Layer"
        UILib["UI App"]:::ui
        Cursor["Cursor"]:::ui
    end

    %% External
    External["Remote HTTP Server"]:::external

    %% Connections
    Runner -->|"issues URL"| URL
    Runner -->|"issues URL"| NetLib

    NetLib -->|"raw HTTP response"| CoreHTTP
    NetHTTP --> CoreHTTP

    CoreHTTP -->|"HTML bytes"| HTMLParser
    CoreHTTP -->|"CSS bytes"| CSSOM

    HTMLParser --> HTMLToken
    HTMLParser --> HTMLAttr
    HTMLParser --> DOMApi

    DOMApi -->|"DOM tree"| Computed
    CSSOM --> Computed

    Computed --> LayoutObj
    LayoutObj --> LayoutView
    LayoutView --> Page

    Page --> UILib
    UILib --> Runner

    JSRt --> DOMApi
    JSRt --> CSSOM

    External --> NetLib

    %% Click Events
    click Runner "https://github.com/d0iasm/sababook/blob/main/ch7/saba/src/main.rs"
    click NetLib "https://github.com/d0iasm/sababook/blob/main/ch7/saba/net/wasabi/src/lib.rs"
    click NetHTTP "https://github.com/d0iasm/sababook/blob/main/ch7/saba/net/wasabi/src/http.rs"
    click CoreRoot "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/lib.rs"
    click URL "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/url.rs"
    click CoreHTTP "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/http.rs"
    click HTMLParser "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/html/parser.rs"
    click HTMLToken "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/html/token.rs"
    click HTMLAttr "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/html/attribute.rs"
    click DOMApi "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/dom/api.rs"
    click DOMNode "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/dom/node.rs"
    click CSSOM "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/css/cssom.rs"
    click CSSToken "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/css/token.rs"
    click Computed "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/layout/computed_style.rs"
    click LayoutObj "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/layout/layout_object.rs"
    click LayoutView "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/layout/layout_view.rs"
    click Page "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/page.rs"
    click JSAST "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/js/ast.rs"
    click JSToken "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/js/token.rs"
    click JSRt "https://github.com/d0iasm/sababook/blob/main/ch7/saba/saba_core/src/renderer/js/runtime.rs"
    click UILib "https://github.com/d0iasm/sababook/blob/main/ch7/saba/ui/wasabi/src/lib.rs"
    click Cursor "https://github.com/d0iasm/sababook/blob/main/ch7/saba/ui/wasabi/src/cursor.rs"
    click UILib "https://github.com/d0iasm/sababook/blob/main/ch7/saba/ui/wasabi/src/app.rs"

    %% Styles
    classDef network fill:#a3d5ff,stroke:#000
    classDef parsing fill:#c8facc,stroke:#000
    classDef layout fill:#ffe0b3,stroke:#000
    classDef js fill:#e0c8ff,stroke:#000
    classDef ui fill:#d3d3d3,stroke:#000
    classDef core fill:#ffffff,stroke:#000
    classDef external fill:#ffcccc,stroke:#000
```
