# bykof/hoard_chunker architecture

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

- Interactive diagram: https://gitdiagram.com/bykof/hoard_chunker
- Repository: https://github.com/bykof/hoard_chunker

## Overview

##### Main Components
- Command Line Interface (CLI) Layer: Represented by src/main.rs. This is the entry point where commands (backup, restore, help, etc.) are parsed.
- Core Library: Represented by src/lib.rs that potentially wires up the internal modules and configures the primary functions of the application.
- Backup Module: Under src/backup, this module encapsulates the domain-specific logic.
- Models: Found under src/backup/models. These files define the data structures such as backup_config, backup_metadata, file and chunk representations, and symbolic links.
- Services: Found under src/backup/services. These files implement the operational behaviors, e.g., backup_service, restore_service, file_chunker, chunk_reader_writer, and chunk_storage. They manage how data is segmented, stored, and later reassembled.

##### Relationships and Interactions
- The CLI layer receives commands and options which trigger calls to the backup module.
- The backup module divides the responsibilities into two primary flows:
a. Backup Flow: Reads from the input directory, splits a file into chunks via file_chunker, uses backup_service to coordinate the process, and interacts with chunk_reader_writer and chunk_storage for persisting the chunks.
b. Restore Flow: Reads chunks from a specified directory, then trigger restore_service to reconstruct the original file.
- Communication between models and services: Models represent the structured data (configuration, metadata, file chunks) that services consume and update during backup or restore operations.

##### Architectural Patterns and Design Principles
- Modular Design: The directory structure promotes separation of concerns (models vs. services). This makes testing, maintenance, and adding features easier.
- Command Pattern: The application accepts commands (backup, restore) via the CLI and routes them to appropriate service layers.
- Layered Architecture: With a clear separation between the interfaces (CLI), business logic (services), and data representations (models).

##### Relevant Technologies and Libraries
- Rust Language and Cargo Build System: Emphasize that this is a Rust application using Cargo (Cargo.toml, Cargo.lock).
- Integration Tests: The tests directory outlines integration tests (tests/integration_test.rs), which support the reliability of the backup and restore flows.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Top Level Interface
    CLI["Command Line Interface (main.rs)"]:::cli
    Core["Core Library (lib.rs)"]:::core

    %% Backup Services Subgraph
    subgraph "Backup Services"
        BackupService["Backup Service (backup_service.rs)"]:::service
        FileChunker["File Chunker (FastCDC) (file_chunker.rs)"]:::service
        ChunkRW["Chunk Reader/Writer (chunk_reader_writer.rs)"]:::service
        ChunkStorage["Chunk Storage (chunk_storage.rs)"]:::service
        RestoreService["Restore Service (restore_service.rs)"]:::service
    end

    %% Backup Models Subgraph
    subgraph "Backup Models"
        BackupConfig["Backup Config (backup_config.rs)"]:::model
        BackupMetadata["Backup Metadata (backup_metadata.rs)"]:::model
        FileChunk["File Chunk (file_chunk.rs)"]:::model
        ChunkDef["Chunk Definition (chunk.rs)"]:::model
        FileMetadata["File Metadata (file_metadata.rs)"]:::model
        Symlink["Symbolic Links Model (symlink.rs)"]:::model
    end

    %% Integration Testing
    IntegrationTest["Integration Tests (integration_test.rs)"]:::test

    %% Data Flow Connections
    CLI -->|"backup"| BackupService
    CLI -->|"restore"| RestoreService
    Core -->|"wires"| BackupService
    Core -->|"wires"| RestoreService

    BackupService -->|"splits"| FileChunker
    BackupService -->|"reads/writes"| ChunkRW
    BackupService -->|"persists"| ChunkStorage
    BackupService -->|"updates"| BackupMetadata

    RestoreService -->|"retrieves"| ChunkStorage
    RestoreService -->|"reads"| BackupMetadata

    IntegrationTest -->|"tests"| CLI

    %% Click Events
    click CLI "https://github.com/bykof/hoard_chunker/blob/main/src/main.rs"
    click Core "https://github.com/bykof/hoard_chunker/blob/main/src/lib.rs"
    click BackupService "https://github.com/bykof/hoard_chunker/blob/main/src/backup/services/backup_service.rs"
    click FileChunker "https://github.com/bykof/hoard_chunker/blob/main/src/backup/services/file_chunker.rs"
    click ChunkRW "https://github.com/bykof/hoard_chunker/blob/main/src/backup/services/chunk_reader_writer.rs"
    click ChunkStorage "https://github.com/bykof/hoard_chunker/blob/main/src/backup/services/chunk_storage.rs"
    click RestoreService "https://github.com/bykof/hoard_chunker/blob/main/src/backup/services/restore_service.rs"
    click BackupConfig "https://github.com/bykof/hoard_chunker/blob/main/src/backup/models/backup_config.rs"
    click BackupMetadata "https://github.com/bykof/hoard_chunker/blob/main/src/backup/models/backup_metadata.rs"
    click FileChunk "https://github.com/bykof/hoard_chunker/blob/main/src/backup/models/file_chunk.rs"
    click ChunkDef "https://github.com/bykof/hoard_chunker/blob/main/src/backup/models/chunk.rs"
    click FileMetadata "https://github.com/bykof/hoard_chunker/blob/main/src/backup/models/file_metadata.rs"
    click Symlink "https://github.com/bykof/hoard_chunker/blob/main/src/backup/models/symlink.rs"
    click IntegrationTest "https://github.com/bykof/hoard_chunker/blob/main/tests/integration_test.rs"

    %% Styles
    classDef cli fill:#fde2e4,stroke:#d11141,stroke-width:2px;
    classDef core fill:#e0fbfc,stroke:#005f73,stroke-width:2px;
    classDef service fill:#cce5ff,stroke:#1d3557,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef model fill:#d4f4dd,stroke:#2d6a4f,stroke-width:2px;
    classDef test fill:#fff3b0,stroke:#e29578,stroke-width:2px;
```
