# esp-rs/esp-generate architecture

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

- Interactive diagram: https://gitdiagram.com/esp-rs/esp-generate
- Repository: https://github.com/esp-rs/esp-generate

## Overview

##### Project Type & Purpose
- This project is a Rust-based command-line application that serves as a template generation tool. Its purpose is to generate no_std application scaffolding targeted at Espressif SoCs (including ESP32, ESP32-C2/C3/C6, ESP32-H2, ESP32-S2/S3). It supports both a Terminal User Interface (TUI) and a Command Line Interface (CLI), with various build options (e.g., enabling Wi-Fi, BLE, allocations, embedded testing, etc.).
- The project emphasizes modularity to handle both interactive and non-interactive usage. It is also designed to be highly customizable by allowing different “options” that configure the generated template.

##### Analyzing the File Structure
- Top-level Directories & Files:
- The project root contains standard Rust files (Cargo.toml, Cargo.lock, build.rs, rust-toolchain.toml) and directories (.cargo, .github) that indicate CI/CD pipelines and configuration files.
- The src directory holds core Rust modules (main.rs, lib.rs, and supporting modules like config.rs, check.rs, template.rs, tui.rs) that form the primary application logic.
- An xtask directory (another Rust project) is included for helper automation tasks, such as building or additional tooling tasks.

##### Identifying Main Components
- User Input Interfaces:
- CLI (main.rs) – processes parameters and option flags (e.g. chip type and generation options).
- TUI (tui.rs) – provides a terminal-based graphical interface for user interactions.
- Core Engine & Configuration:
- Configuration module (config.rs) – parses and manages various generation options (e.g., enabling alloc, wifi, ble, etc.).
- Business Logic (lib.rs) – centralizes shared functions and library code for template generation.
- Check Module (check.rs) – likely validates environmental settings or compatibility.
- Template Generation:
- Template module (template.rs) – accesses the content defined in the template folder, processes placeholders, and copies/builds the output project structure.
- The “template” directory acts as the “blueprint” for new projects including auxiliary components like Docker and development container configurations.
- Task Automation:
- The xtask directory holds scripts or tooling that could be used for build automation, custom tasks, or extended maintenance operations.

##### Relationships & Data Flow
- The user starts by interacting with the CLI/TUI interface.
- Input options and configuration data are processed by the configuration module.
- A new project is then generated by copying the desired template files with applied customizations.
- Either internal tasks (via xtask commands) or external tools (like CI/CD pipelines defined in GitHub workflows) may call further operations to validate or deploy the templates.

##### Architectural Patterns & Technologies
- Modular design: separation between user interfaces (CLI vs. TUI) and core business logic.
- Template Method: using the template directory as a “blueprint” to instantiate new project scaffolding.
- Option/Flag parsing: integrating various feature toggles that conditionally include additional features (e.g., probe-rs, flashing-espflash options).
- Integration with CI/CD: the presence of GitHub workflows suggests an automated build/test/deploy system.
- Rust’s package management through Cargo is central to dependency management.

##### Final Advice
- Fact that the project is customizable via feature flags, and highlight the two paths for project generation (interactive via TUI vs. non-interactive CLI).
- Detail that the “template” directory itself is a complete Rust project structure, demonstrating the comprehensive nature of the project scaffolding being generated.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface Layer"
        CLI["CLI Interface"]:::ui
        TUI["TUI Interface"]:::ui
    end

    subgraph "Core Processing Layer"
        CONFIG["Configuration Module"]:::core
        CHECK["Validation Module"]:::validate
        CORE["Core Business Logic"]:::core
        TEMPLATE_PROC["Template Processor"]:::core
    end

    subgraph "Template Engine"
        BLUEPRINT["Template Blueprint"]:::template
        OUTPUT["Generated Project Output"]:::output
    end

    subgraph "Automation"
        AUTOMATION["Automation Tasks"]:::automation
    end

    CLI -->|"provides_options"| CONFIG
    TUI -->|"provides_options"| CONFIG
    CONFIG -->|"validate_env"| CHECK
    CONFIG -->|"parse_options"| CORE
    CHECK -->|"env_ok"| CORE
    CORE -->|"process_template"| TEMPLATE_PROC
    TEMPLATE_PROC -->|"reads_blueprint"| BLUEPRINT
    BLUEPRINT -->|"generate_project"| OUTPUT
    AUTOMATION -->|"trigger_build"| CORE

    click CLI "https://github.com/esp-rs/esp-generate/blob/main/src/main.rs"
    click TUI "https://github.com/esp-rs/esp-generate/blob/main/src/tui.rs"
    click CONFIG "https://github.com/esp-rs/esp-generate/blob/main/src/config.rs"
    click CHECK "https://github.com/esp-rs/esp-generate/blob/main/src/check.rs"
    click CORE "https://github.com/esp-rs/esp-generate/blob/main/src/lib.rs"
    click TEMPLATE_PROC "https://github.com/esp-rs/esp-generate/blob/main/src/template.rs"
    click BLUEPRINT "https://github.com/esp-rs/esp-generate/tree/main/template/"
    click AUTOMATION "https://github.com/esp-rs/esp-generate/tree/main/xtask/"

    classDef ui fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef core fill:#8cc6ff,stroke:#333,stroke-width:2px;
    classDef validate fill:#d1ff8c,stroke:#333,stroke-width:2px;
    classDef template fill:#ffadad,stroke:#333,stroke-width:2px;
    classDef automation fill:#c9c8ff,stroke:#333,stroke-width:2px;
    classDef output fill:#baffc9,stroke:#333,stroke-width:2px;
```
