# ncave/fable-raytracer architecture

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

- Interactive diagram: https://gitdiagram.com/ncave/fable-raytracer
- Repository: https://github.com/ncave/fable-raytracer

## Overview

##### Main Components
- F# Source Code: This is the core ray tracing code (files such as Platform.fs, RayTracer.fs, lib.fs, and main.fs) which contains the “business logic” and ray tracer implementation.
- Transpiler/Build Engine: The Fable compiler stands out as a central piece that converts F# source code into JavaScript or Rust. It acts as a bridge between the F# code and the target language outputs.
- Build Pipelines:
- JavaScript Pipeline: Involves compiling the F# code to JavaScript via Fable; used in tests like “test-js-node.”
- WebAssembly Pipeline: Converts F# to Rust code (via Fable), then builds to WebAssembly using Rust’s toolchain and wasm-pack (e.g., tests “test-wasm-web” and “test-wasm-node”).
- Native / Other Language Pipelines: With commands like “test-rust,” “test-python,” “test-dotnet,” “test-native,” etc., additional paths are available for compiling/executing the F# code on different platforms.
- Frontend Interface: The index.html and accompanying main.mjs (and test_wasm.js) provide a web interface and test harness specifically for the browser-based WebAssembly demo.
- Configuration/Dependency Managers: Dotnet’s configurations (.config files, fable-raytracer.fsproj, nuget.config), Node.js’s package.json, and Rust’s Cargo.toml give context to the build environment and dependencies.

##### Relationships and Interactions
- The F# source modules are the starting point. They are fed into the Fable compiler (or equivalent build system) which then sends the code down one of several build pipelines.
- There’s a clear separation of concerns where the source code remains oblivious to the target language, while the transpiler adapts it based on the specific build script or command run.
- The build pipelines (as initiated by npm scripts or .NET tools) then interact with their respective runtimes: Node.js for JavaScript, Rust toolchain for native or wasm outputs, and .NET runtime for managed code.
- The web interface (index.html) consumes the compiled output when using the WebAssembly path, linking it with browser APIs.

##### Architectural Patterns and Design Principles
- Modular and Pipeline-based Design: The separation into distinct pipelines (JS, WASM, Native) demonstrates a modular approach.
- Multi-Target Build: The system is designed to compile a single codebase into multiple artifacts. This emphasizes the use of abstraction in build tooling.
- Loose Coupling: The F# source code is decoupled from the output language; the Fable transpiler encapsulates target-specific details.

##### Relevant Technologies and Tools
- Fable Compiler for F#.
- .NET SDK (for building F# projects).
- Node.js (for running JavaScript outputs and tests).
- Rust and wasm-pack (for compiling to native or WebAssembly targets).
- Configuration files (e.g., package.json, Cargo.toml, dotnet-tools.json) that drive the build processes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["F# Source (src/)"]:::fsharpSource
    B["Fable Transpiler"]:::transpiler
    A --> B

    subgraph "Configuration/Dependency Managers"
        C["Fable Project Config"]:::configuration
        D["Nuget Config"]:::configuration
        E["Dotnet Tools"]:::configuration
    end
    C --> B
    D --> B
    E --> B

    subgraph "Build Pipelines"
        subgraph "JavaScript Pipeline"
            J1["Package Config (package.json)"]:::configuration
            J2["Web Interface (index.html)"]:::buildPipeline
            J3["Main Module (src/main.mjs)"]:::buildPipeline
            J4["Test Harness (src/test_wasm.js)"]:::buildPipeline
            J_Out["JS Output (Node.js)"]:::output
        end
        subgraph "Rust/WASM Pipeline"
            R1["Cargo Config (Cargo.toml)"]:::configuration
            R_Out["WebAssembly Output (Browser)"]:::output
        end
        subgraph "Native/Other Pipelines"
            N_Out["Native Output (.NET/Python/etc)"]:::output
        end
    end

    B --> J1
    B --> R1
    B --> N_Out

    J1 --> J2
    J2 --> J3
    J3 --> J4
    J4 --> J_Out

    R1 --> R_Out

    click A "https://github.com/ncave/fable-raytracer/tree/main/src/"
    click B "https://github.com/ncave/fable-raytracer/blob/main/fable-raytracer.fsproj"
    click J1 "https://github.com/ncave/fable-raytracer/blob/main/package.json"
    click J2 "https://github.com/ncave/fable-raytracer/blob/main/index.html"
    click J3 "https://github.com/ncave/fable-raytracer/blob/main/src/main.mjs"
    click J4 "https://github.com/ncave/fable-raytracer/blob/main/src/test_wasm.js"
    click R1 "https://github.com/ncave/fable-raytracer/blob/main/Cargo.toml"
    click D "https://github.com/ncave/fable-raytracer/blob/main/nuget.config"
    click E "https://github.com/ncave/fable-raytracer/blob/main/.config/dotnet-tools.json"

    classDef fsharpSource fill:#add8e6,stroke:#000,stroke-width:1px;
    classDef transpiler fill:#90ee90,stroke:#000,stroke-width:1px;
    classDef buildPipeline fill:#ffd700,stroke:#000,stroke-width:1px;
    classDef configuration fill:#dda0dd,stroke:#000,stroke-width:1px;
    classDef output fill:#ffa07a,stroke:#000,stroke-width:1px;
```
