# tusharsadhwani/formatjson architecture

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

- Interactive diagram: https://gitdiagram.com/tusharsadhwani/formatjson
- Repository: https://github.com/tusharsadhwani/formatjson

## Overview

##### Main Components
- Command-Line Interface (CLI): Represented by src/main.rs, which ties together reading input (file or STDIN), invoking library functionality, and displaying output.
- Core Library Modules: The library portion located in src/lib.rs and its supporting modules:
- Tokenizer (src/tokenizer.rs): Parses the raw JSON into tokens.
- Nodes (src/nodes.rs): Likely constructs an abstract representation (possibly a parse tree) of the JSON data.
- Validator (src/validator.rs): Ensures the JSON adheres to proper syntax and rules.
- Token Formatter (src/token_formatter.rs): Handles reformatting of tokens or the reconstructed JSON.
- Errors (src/errors.rs): Manages error handling across the modules.
- Testing Layer: Located in the tests directory, showing the separation into end-to-end (e2e_tests.rs) and validator-specific tests, along with JSON test data (in tests/json_data).

##### Their Relationships and Interactions
- The CLI (src/main.rs) serves as an entry point and makes calls to the library (src/lib.rs).
- The library aggregates the functionality of the tokenizer, nodes, validator, and formatter to process JSON strings.
- Data flows begin at input (from the CLI or a function call when used as a library), pass through the tokenizer, move to the node builder (parsing), then are validated and formatted before returning the output.
- Errors are captured and propagated from any layer, ensuring a consistent error-handling strategy.

##### Architectural Patterns and Principles
- Separation of Concerns: Different modules handle distinct tasks (tokenization, parsing, validation, formatting).
- Modular Design: Allows the library to be used both as a CLI tool and as a reusable component in other programs.
- Test-Driven Architecture: The presence of separate E2E and validator tests indicates an emphasis on verifying functionality across layers.

##### Relevant Technologies and Frameworks
- Rust as the programming language.
- Cargo build management and dependency management as per Cargo.toml and Cargo.lock.
- GitHub Actions workflows (in .github/workflows) showing automated CI using the rust.yml workflow.
- The testing framework integrated with Rust (using the tests directory).

##### Elements
- Use clear labels for each component (CLI, Library, Tokenizer, Nodes, Validator, Formatter, Errors, Tests, CI/CD).

##### Final Implementation Notes
- Specifically mention how errors are handled as an integral part of the system.
- Testing and deployment (via CI/CD) as important parts of the overall process.

## Diagram (Mermaid)

```mermaid
graph TD
    CLI["Command-Line Interface"]:::cli
    LIB["Core Library"]:::lib

    CLI -->|"calls"| LIB

    subgraph "Core JSON Processing"
        TKN["Tokenizer"]:::module
        NB["Node Builder"]:::module
        VAL["Validator"]:::module
        TF["Token Formatter"]:::module
        TKN -->|"parses"| NB
        NB -->|"builds"| VAL
        VAL -->|"validates"| TF
    end

    LIB -->|"invokes"| TKN
    TF -->|"returnsOutput"| LIB

    ERR["Error Handling"]:::error
    TKN -.-> ERR
    NB -.-> ERR
    VAL -.-> ERR
    TF -.-> ERR

    TEST["Testing Layer"]:::test
    LIB -->|"testedBy"| TEST

    CICD["CI/CD (GitHubActions)"]:::cicd
    CICD -->|"triggers"| TEST

    click CLI "https://github.com/tusharsadhwani/formatjson/blob/master/src/main.rs"
    click LIB "https://github.com/tusharsadhwani/formatjson/blob/master/src/lib.rs"
    click TKN "https://github.com/tusharsadhwani/formatjson/blob/master/src/tokenizer.rs"
    click NB "https://github.com/tusharsadhwani/formatjson/blob/master/src/nodes.rs"
    click VAL "https://github.com/tusharsadhwani/formatjson/blob/master/src/validator.rs"
    click TF "https://github.com/tusharsadhwani/formatjson/blob/master/src/token_formatter.rs"
    click ERR "https://github.com/tusharsadhwani/formatjson/blob/master/src/errors.rs"
    click TEST "https://github.com/tusharsadhwani/formatjson/tree/master/tests/"
    click CICD "https://github.com/tusharsadhwani/formatjson/blob/master/.github/workflows/rust.yml"

    classDef cli fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef lib fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef module fill:#f9d9a5,stroke:#000,stroke-width:2px;
    classDef error fill:#ffcccb,stroke:#000,stroke-width:2px;
    classDef test fill:#ffeb99,stroke:#000,stroke-width:2px;
    classDef cicd fill:#dcd0ff,stroke:#000,stroke-width:2px;
```
