# nachopal/rust-chess architecture

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

- Interactive diagram: https://gitdiagram.com/nachopal/rust-chess
- Repository: https://github.com/nachopal/rust-chess

## Overview

##### Main Components
- Core Chess Engine (the “chess” crate): Contains the board representation, game logic, pieces, and related tests. It also includes support via procedural macros for generating boilerplate or DSL features.
- Server Application (the “server” crate): This component is responsible for running the chess game on the backend. It hosts the game logic (by invoking functions from the chess core library) and handles incoming player connections and moves.
- Client Application (the “client” crate): Acts as the user interface that clients use to connect and interact with the server. It includes modules handling socket communications and RPC requests.
- JSON-RPC Module (the “json-rpc” crate): Provides a dedicated set of functionalities (including procedural macros) to implement remote procedure calls, which facilitate communication between the client and server.

##### Component Relationships and Interactions
- The Client sends player commands over a network socket that are serialized and communicated via the JSON-RPC protocol.
- The Server receives these RPC calls, parses and validates the input (like passwords and move commands), and then delegates the game operation to the chess engine library.
- The Chess Engine processes the game state and returns outcomes that the Server then communicates back to the Client.

##### Architectural Patterns and Design Principles
- Modularity: The project is divided into distinct Rust crates—each with a well-defined purpose (chess core, server, client, and json-rpc).
- Separation of Concerns: Network communication (in client and server) is cleanly isolated from game logic (in the chess core).
- Use of Procedural Macros: Both the chess and json-rpc crates utilize proc-macros to reduce boilerplate and enforce patterns, suggesting an interest in compile-time code generation and metaprogramming.
- Asynchronous and Socket-Based Communication: The server and client modules handling sockets and RPC calls suggest the use of asynchronous operations for non-blocking I/O.

##### Detailed Step-by-Step for the Principal Software Engineer
- Board Representation (chess/src/board.rs)
- Game Logic (chess/src/game.rs)
- Pieces (chess/src/pieces.rs)
- Procedural Macros (chess/proc-macros/)
- Socket Management (server/src/socket.rs)
- RPC Handling (server/src/rpc.rs)
- Main Execution (server/src/main.rs, server/src/run.rs)
- Socket/RPC Communication (client/src/rpc.rs, client/src/socket.rs)
- Main Runner (client/src/run.rs, client/src/main.rs)
- External interfaces (client and server communication layer) vs. internal logic (chess engine core).

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Application Layer
    subgraph "Client Application"
      direction TB
      Client_Main["Main Runner (client/src/main.rs)"]:::client
      Client_Run["Runner (client/src/run.rs)"]:::client
      Client_Socket["Socket Communication (client/src/socket.rs)"]:::client
      Client_RPC["RPC Communication (client/src/rpc.rs)"]:::client
    end

    %% JSON-RPC Module (Infrastructure Layer)
    subgraph "JSON-RPC Module"
      direction TB
      JSONRPC_Core["Core Library (json-rpc/src/lib.rs)"]:::jsonrpc
      JSONRPC_Proc["Procedural Macros (json-rpc/proc-macros/)"]:::jsonrpc
    end

    %% Server Application Layer
    subgraph "Server Application"
      direction TB
      Server_Main["Main Execution (server/src/main.rs)"]:::server
      Server_Run["Runner (server/src/run.rs)"]:::server
      Server_Socket["Socket Management (server/src/socket.rs)"]:::server
      Server_RPC["RPC Handling (server/src/rpc.rs)"]:::server
      Server_Lib["Core Library Integration (server/src/lib.rs)"]:::server
    end

    %% Chess Engine (Domain Layer)
    subgraph "Chess Engine"
      direction TB
      Chess_Core["Core Library (chess/src/lib.rs)"]:::chess
      Chess_Board["Board Representation (chess/src/board.rs)"]:::chess
      Chess_Game["Game Logic (chess/src/game.rs)"]:::chess
      Chess_Pieces["Pieces (chess/src/pieces.rs)"]:::chess
      Chess_Macros["Procedural Macros (chess/proc-macros/)"]:::chess
    end

    %% Relationships between major components using vertical flow
    Client_RPC -->|"RPC_Request"| JSONRPC_Core
    JSONRPC_Core -->|"RPC_Fwd"| Server_RPC
    Server_RPC -->|"GameLogicInvoke"| Chess_Game
    Chess_Game -->|"StateUpdate"| Server_RPC
    Server_RPC -->|"RPC_Response"| JSONRPC_Core
    JSONRPC_Core -->|"RPC_Response"| Client_RPC

    %% Click Events for Client Application
    click Client_Main "https://github.com/nachopal/rust-chess/blob/master/client/src/main.rs"
    click Client_Run "https://github.com/nachopal/rust-chess/blob/master/client/src/run.rs"
    click Client_Socket "https://github.com/nachopal/rust-chess/blob/master/client/src/socket.rs"
    click Client_RPC "https://github.com/nachopal/rust-chess/blob/master/client/src/rpc.rs"

    %% Click Events for JSON-RPC Module
    click JSONRPC_Core "https://github.com/nachopal/rust-chess/blob/master/json-rpc/src/lib.rs"
    click JSONRPC_Proc "https://github.com/nachopal/rust-chess/tree/master/json-rpc/proc-macros/"

    %% Click Events for Server Application
    click Server_Main "https://github.com/nachopal/rust-chess/blob/master/server/src/main.rs"
    click Server_Run "https://github.com/nachopal/rust-chess/blob/master/server/src/run.rs"
    click Server_Socket "https://github.com/nachopal/rust-chess/blob/master/server/src/socket.rs"
    click Server_RPC "https://github.com/nachopal/rust-chess/blob/master/server/src/rpc.rs"
    click Server_Lib "https://github.com/nachopal/rust-chess/blob/master/server/src/lib.rs"

    %% Click Events for Chess Engine
    click Chess_Core "https://github.com/nachopal/rust-chess/blob/master/chess/src/lib.rs"
    click Chess_Board "https://github.com/nachopal/rust-chess/blob/master/chess/src/board.rs"
    click Chess_Game "https://github.com/nachopal/rust-chess/blob/master/chess/src/game.rs"
    click Chess_Pieces "https://github.com/nachopal/rust-chess/blob/master/chess/src/pieces.rs"
    click Chess_Macros "https://github.com/nachopal/rust-chess/tree/master/chess/proc-macros/"

    %% Styles for different layers
    classDef client fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef server fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef chess fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef jsonrpc fill:#d1c4e9,stroke:#673ab7,stroke-width:2px;
```
