# clearloop/leetcode-cli architecture

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

- Interactive diagram: https://gitdiagram.com/clearloop/leetcode-cli
- Repository: https://github.com/clearloop/leetcode-cli

## Overview

##### Main Components

- CLI Entry Point
– Based on src/bin/lc.rs and src/cli.rs, this is the primary entry where user input is received.

- Commands Layer (Subcommands)
– Under src/cmds, you have distinct subcommands for functionalities like ‘completions’, ‘data’, ‘edit’, ‘exec’, ‘list’, ‘pick’, ‘stat’, and ‘test’. This layer interprets the CLI input and directs the flow accordingly.

- Configuration Module
– Located in src/config, this module handles reading, parsing, and providing configuration (code settings, cookie management, storage paths, etc.) from the user’s config file (i.e., ~/.leetcode/leetcode.toml).

- Caching Subsystem
– Found under src/cache, it is responsible for caching and persisting problem data. This subsystem includes models, parsers, schema definitions, and SQL interactions, which may indicate integration with an SQLite database or similar technology.

- Plugin System
– Located under src/plugins, this subsystem provides integration capabilities, for example with Chrome or directly with LeetCode. It is meant to extend the CLI’s functionality.

- Core Library & Helpers
– The src/lib.rs module collects core functionalities, and additional helper functions (src/helper.rs) as well as error handling (src/err.rs), which are utilized across various parts of the application.

- Testing
– The tests folder shows that a test suite exists to validate the functionality of the components.

##### Relationships and Interactions

- User Interaction Flow:
– The CLI input (via the entry point in src/bin/lc.rs or src/cli.rs) routes the request to the appropriate command module in src/cmds.
– Each command (for example, editing or testing a problem) interacts with the configuration module to read user settings.
– Commands that need to access local problem data will call functions exposed in the cache subsystem.
– For operations that interact with external systems (like LeetCode login via Firefox cookies or using plugins), the command will invoke the plugin system.
– The helper modules and error handling interject as cross-cutting concerns in each component.

- Data Flow:
– Input configuration: Read once from the user’s config file at startup.
– Problem Data: Cached in the cache subsystem, following parsing and SQL interactions.
– Execution Flow: User commands trigger appropriate actions which may involve both config and plugins, with results then fed back to the CLI.

##### Architectural Patterns and Design Principles

- Modular Design:
– The clear separation into directories such as “cmds”, “cache”, “config”, and “plugins” indicates a modular architecture.

- Single Responsibility Principle:
– Each module manages one aspect of the system (commands, caching, configuration, etc.).

- Extensibility:
– The plugins folder and programmable scripting support in the configuration allow for easy extension or customization.

- CLI Design:
– The use of the CLI pattern, where an entry point dispatches to subcommands based on command-line flags, is well represented.

- Testability:
– The existence of tests indicates that components were designed to be independently testable.

##### Additional Elements

– Clearly labeled nodes for each component.
– Optionally, annotate complex interactions (like how the “edit” command reads code templates and injects before and after code snippets) as sub-notes within the relevant node.

## Diagram (Mermaid)

```mermaid
graph TD
    CLIEntry["CLI Entry (src/cli.rs, src/bin/lc.rs)"]:::cli
    Commands["Commands Layer (src/cmds/*)"]:::cmd
    Config["Configuration Management (src/config)"]:::config
    Cache["Caching Subsystem (src/cache)"]:::cache
    Plugin["Plugin System (src/plugins)"]:::plugin
    Core["Core Library & Helpers (src/lib.rs, src/helper.rs, src/err.rs)"]:::core
    Test["Testing Suite (tests)"]:::test
    ConfigFile["Config File (~/.leetcode/leetcode.toml)"]:::external

    CLIEntry -->|"routesTo"| Commands
    Commands -->|"readsSettings"| Config
    ConfigFile -->|"providesConfig"| Config
    Commands -->|"cachesData"| Cache
    Commands -->|"invokesPlugins"| Plugin
    Commands -->|"usesHelpers"| Core

    %% Click Events
    click CLIEntry "https://github.com/clearloop/leetcode-cli/blob/master/src/bin/lc.rs"
    click Commands "https://github.com/clearloop/leetcode-cli/tree/master/src/cmds"
    click Config "https://github.com/clearloop/leetcode-cli/tree/master/src/config"
    click Cache "https://github.com/clearloop/leetcode-cli/tree/master/src/cache"
    click Plugin "https://github.com/clearloop/leetcode-cli/tree/master/src/plugins"
    click Core "https://github.com/clearloop/leetcode-cli/blob/master/src/lib.rs"
    click Test "https://github.com/clearloop/leetcode-cli/tree/master/tests"

    %% Styles
    classDef cli fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef cmd fill:#66ccff,stroke:#333,stroke-width:2px;
    classDef config fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef cache fill:#ffcccc,stroke:#333,stroke-width:2px;
    classDef plugin fill:#ffdd99,stroke:#333,stroke-width:2px;
    classDef core fill:#d6ccff,stroke:#333,stroke-width:2px;
    classDef test fill:#cccccc,stroke:#333,stroke-width:2px;
    classDef external fill:#e0e0e0,stroke:#333,stroke-width:2px;
```
