Skip to content

Refactor: Split main.rs into modular structure #15

@cbaugus

Description

@cbaugus

Summary

The current main.rs file is 794 lines and contains all application logic in a single file. This issue tracks refactoring the codebase into a modular structure for better maintainability, testability, and code organization.

Current State

All code resides in src/main.rs:

  • LoadModel enum and implementation (~100 lines)
  • Utility functions: parse_duration_string(), parse_headers_with_escapes() (~60 lines)
  • Main function with TLS config, load test logic, metrics server (~450 lines)
  • metrics_handler() function (~20 lines)
  • Unit tests (~70 lines)

Proposed Structure

src/
├── main.rs           # Entry point only (~50 lines)
├── lib.rs            # Module exports
├── config.rs         # Config struct, env var parsing, validation
├── load_models.rs    # LoadModel enum + calculate_current_rps()
├── client.rs         # HTTP client builder, TLS configuration
├── metrics.rs        # Prometheus metrics setup, handler, registry
├── worker.rs         # Async worker task logic
└── utils.rs          # parse_duration_string(), parse_headers_with_escapes()

Acceptance Criteria

  • Each module has a single, clear responsibility
  • main.rs contains only entry point logic and orchestration
  • All existing functionality preserved (no behavioral changes)
  • All existing tests pass
  • Each module has appropriate visibility (pub, pub(crate))
  • Modules use explicit imports (no use super::*)
  • Code compiles with no new warnings

Implementation Notes

Module Responsibilities

Module Contents
config.rs Config struct, load_from_env(), validation logic
load_models.rs LoadModel enum, calculate_current_rps(), phase calculations
client.rs build_client(), TLS connector setup, mTLS handling
metrics.rs REGISTRY, metric definitions, metrics_handler()
worker.rs run_worker() async function, request loop logic
utils.rs parse_duration_string(), parse_headers_with_escapes()

Migration Steps

  1. Create module files with // TODO placeholders
  2. Move LoadModel and tests to load_models.rs
  3. Move utility functions and tests to utils.rs
  4. Extract metrics to metrics.rs
  5. Extract client builder to client.rs
  6. Create Config struct in config.rs
  7. Extract worker logic to worker.rs
  8. Update main.rs to orchestrate modules
  9. Run cargo test and cargo clippy

Benefits

  • Easier to navigate codebase
  • Tests co-located with implementation
  • Better separation of concerns
  • Enables parallel development on different modules
  • Improves compile times for incremental builds

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions