# anirudh-os/rustytasks architecture

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

- Interactive diagram: https://gitdiagram.com/anirudh-os/rustytasks
- Repository: https://github.com/anirudh-os/rustytasks

## Overview

##### Project type and purpose
- RustyTasks is a peer-to-peer, CRDT-backed command-line To-Do list application written in Rust.
- It is a monolithic CLI application that can run offline or in “interactive” P2P mode over TCP using Tokio, with Automerge as the CRDT engine.

##### File structure
Top-level files:
- Cargo.toml / Cargo.lock: Rust dependencies and metadata
- README.md: project description, usage, features
src/ directory modules:
- main.rs: CLI entry point and orchestration
- cli.rs: definition of command-line interface (clap config)
- tasks.rs: Task data model and local in-memory operations
- crdt.rs: Integration with Automerge document API
- identity.rs: Peer keypair/identity creation
- network.rs: Low-level P2P TCP layer (Tokio)
- peer.rs: Peer management, connection handling
- sync.rs: High-level sync logic (CRDT change exchange)
- .rs: Interactive display/UI logic

##### README for additional insights
- Uses Automerge for conflict-free merges (CRDT)
- Persists state to `autocommit_doc.automerge` on disk
- Async communications via Tokio over TCP
- Modules are explicitly designed to be extensible

##### Components
a. Main components
1. CLI Layer
– cli.rs / main.rs
2. Task Management Layer
– tasks.rs (Task CRUD API)
3. CRDT Layer
– crdt.rs (Automerge document)
4. Persistence
– Local disk file I/O (autocommit_doc.automerge)
5. Networking Layer
– identity.rs (peer identity / keys)
– network.rs (Tokio/TCP sockets)
– peer.rs (connection & peer table)
– sync.rs (CRDT change exchange protocol)
6. / Interactive UI
– display.rs (optional TUI or console rendering)

b. Relationships and interactions
– The CLI Layer receives user commands (add/remove/done/list/interactive).
– For any CRUD, CLI invokes Task Management, which updates the Automerge doc via the CRDT Layer.
– After each local change, Persistence writes the Automerge doc to disk.
– In “interactive” mode, the Networking Layer uses identity.rs to load/create peer identity, network.rs to open a TCP listener, peer.rs to manage connections, and sync.rs to exchange change deltas.
– Incoming remote CRDT changes are applied via the CRDT Layer, then merged into tasks.rs, and persisted.
– display.rs taps into Task Management and CRDT state to render an interactive console UI or prompt.

c. Architectural patterns / design principles
– Modular separation: CLI / Task / CRDT / Network / Persistence / UI
– Layered architecture with clear data flow: User → CLI → Task → CRDT → Persistence / Network
– Asynchronous & event-driven using Tokio for non-blocking I/O
– CRDT pattern for decentralized conflict-free data replication
– Single binary; all modules compiled into one executable

d. Key technologies & libraries
– Rust / Cargo
– clap (for cli.rs)
– Automerge (crdt.rs)
– tokio (network.rs, async runtime)
– TCP sockets (peer.rs)
– Local file I/O (std::fs for persistence)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Input
    User["User"]  

    %% CLI Layer
    subgraph "CLI Layer"
        direction TB
        CLI_main["main.rs\n(CLI Entry)"]:::cliUI
        CLI_cli["cli.rs\n(CLI Config)"]:::cliUI
    end

    %% UI Layer
    subgraph "UI Layer"
        direction TB
        display["display.rs\n(Interactive UI)"]:::cliUI
    end

    %% Task & CRDT Layers
    subgraph "Core Logic"
        direction TB
        Task["tasks.rs\n(Task Manager)"]:::logic
        CRDT["crdt.rs\n(CRDT Layer)"]:::logic
    end

    %% Persistence
    Persist[(autocommit_doc.automerge)]:::persistence

    %% Networking Layer
    subgraph "Networking Layer"
        direction TB
        Identity["identity.rs\n(Peer Identity)"]:::network
        Network["network.rs\n(Tokio/TCP)"]:::network
        Peer["peer.rs\n(Connection Manager)"]:::network
        Sync["sync.rs\n(Sync Protocol)"]:::network
    end

    %% P2P Cloud
    Cloud(("P2P Network")):::network

    %% Connections - Offline Flow
    User --> CLI_main
    CLI_main --> Task
    CLI_cli  --> Task
    Task --> CRDT
    CRDT --> Persist
    display --> Task
    display --> CRDT

    %% Connections - Interactive Flow
    CLI_main --> Identity
    Identity --> Network
    Network --> Cloud
    Cloud --> Peer
    Peer --> Sync
    Sync --> CRDT

    %% Click Events
    click CLI_main "https://github.com/anirudh-os/rustytasks/blob/main/src/main.rs"
    click CLI_cli "https://github.com/anirudh-os/rustytasks/blob/main/src/cli.rs"
    click display "https://github.com/anirudh-os/rustytasks/blob/main/src/display.rs"
    click Task "https://github.com/anirudh-os/rustytasks/blob/main/src/tasks.rs"
    click CRDT "https://github.com/anirudh-os/rustytasks/blob/main/src/crdt.rs"
    click Identity "https://github.com/anirudh-os/rustytasks/blob/main/src/identity.rs"
    click Network "https://github.com/anirudh-os/rustytasks/blob/main/src/network.rs"
    click Peer "https://github.com/anirudh-os/rustytasks/blob/main/src/peer.rs"
    click Sync "https://github.com/anirudh-os/rustytasks/blob/main/src/sync.rs"

    %% Styles
    classDef cliUI fill:lightgreen,stroke:#333,stroke-width:1px
    classDef logic fill:lightblue,stroke:#333,stroke-width:1px
    classDef persistence fill:orange,stroke:#333,stroke-width:1px
    classDef network fill:plum,stroke:#333,stroke-width:1px
```
