# chmln/sd architecture

> Architecture diagram of the GitHub repository chmln/sd, 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/chmln/sd
- Repository: https://github.com/chmln/sd

## Overview

##### Main components
- Core Application (CLI Entry Point): Represented by src/main.rs. This module orchestrates the entire application.
- CLI Parsing & Input Processing: Implemented in src/cli.rs and src/input.rs. These modules handle command-line argument parsing and reading file/console input.
- Replacer Module: Found in src/replacer (with mod.rs, tests.rs, validate.rs). It encapsulates the core logic of text search and replace, whether in regex mode or in string-literal mode.
- Error Handling: The error.rs module centralizes error creation and propagation throughout the tool.
- Auxiliary Build/Generation Tasks: The xtask directory (with its own Cargo.toml and source files) is used for tasks like generating shell completions and the manual page (found in gen/). This indicates a separation between production code and build tooling.
- Testing Infrastructure: The tests folder, along with snapshot tests and proptest-regressions, ensures the correctness of the replace operations and overall CLI behavior.
- Configuration & Build Files: Cargo.toml, Cargo.lock, rustfmt, and other configuration files define dependencies, formatting, and the Rust build system. They illustrate the project’s integration with Cargo and CI/CD configuration files under .github.

##### Mapping Relationships and Flow
- The CLI module (src/cli.rs) processes user inputs (command-line parameters, flags, and file names) and passes them to the input processor.
- The Input module (src/input.rs) reads data (from STDIN or files) and sends it to the Replacer module.
- The Replacer module (in src/replacer/) executes the core find and replace logic, handling both regex and fixed-string modes. It also manages capture groups and formats output as described.
- Any errors detected in processing are handled by the Error module (src/error.rs) and reported back to the user.
- Optionally, the xtask component (xtask/src) is used in the build process to generate artifacts (e.g., shell completions in gen/completions, the man page in gen/sd.1).
- Testing components ensure that each of these stages works as intended; tests are structured to validate CLI commands and scenario-based outputs.

##### Final Checklist
- All major components are boxed and labeled.
- Annotations or notes on:
- The use of regex vs. string-literal modes.
- How errors are handled.
- Build and test infrastructure supporting the project.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Runtime Components
    subgraph "Runtime Modules"
        A1("CLI Entry Point (main.rs)"):::runtime
        A2("CLI Parser (cli.rs)"):::runtime
        A3("Input Module (input.rs)"):::runtime
        A4("Replacer Module (src/replacer)"):::runtime
        A5("Error Handling (error.rs)"):::runtime
    end

    %% Auxiliary Tools
    subgraph "Auxiliary Tools and Build"
        B1("Build & Generation Tools (xtask & gen)"):::aux
    end

    %% Testing and Configuration
    subgraph "Testing & Configuration"
        C1("Testing Infrastructure (tests & proptest-regressions)"):::test
        C2("Configuration & Build System (Cargo.toml, Cargo.lock, .rustfmt.toml, .github)"):::config
    end

    %% Data Flow Connections (Vertical Pipeline)
    A1 -->|"initialize"| A2
    A2 -->|"parse"| A3
    A3 -->|"input data"| A4
    A4 -->|"result/output"| A1

    %% Error Propagation
    A2 -->|"error_detected"| A5
    A4 -->|"error_detected"| A5

    %% Build & Generation Support Flow
    B1 -->|"generate completions/docs"| A1

    %% Testing and CI Configuration Influence
    C1 -->|"validate modules"| A4
    C2 -->|"configure/trigger"| A1

    %% Click Events
    click A1 "https://github.com/chmln/sd/blob/master/src/main.rs"
    click A2 "https://github.com/chmln/sd/blob/master/src/cli.rs"
    click A3 "https://github.com/chmln/sd/blob/master/src/input.rs"
    click A4 "https://github.com/chmln/sd/tree/master/src/replacer"
    click A5 "https://github.com/chmln/sd/blob/master/src/error.rs"
    click B1 "https://github.com/chmln/sd/tree/master/xtask/src"
    click C1 "https://github.com/chmln/sd/tree/master/tests/"
    click C2 "https://github.com/chmln/sd/blob/master/Cargo.toml"

    %% Styles
    classDef runtime fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef aux fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef test fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef config fill:#F5CBA7,stroke:#7D3C98,stroke-width:2px;
```
