Skip to content
This repository was archived by the owner on Mar 10, 2026. It is now read-only.
This repository was archived by the owner on Mar 10, 2026. It is now read-only.

Refactor Configuration System for Usability and Zero-Config Execution #41

@TKanX

Description

@TKanX

Description:

The current system requires a complete TOML file for operation and has default values scattered and hardcoded within the codebase.

The new design will introduce a layered configuration model with a clear hierarchy of precedence: CLI Arguments > TOML File > Built-in Defaults. This will be achieved by creating a centralized default configuration module and leveraging serde and clap's advanced features. The primary goal is to make the tool more accessible to new users by providing scientifically sound defaults, while retaining full customizability for expert users.

Tasks:

  • Phase 1: Centralize All Default Parameters

    • Create a new module config/defaults.rs.
    • Define all default values for forcefield, sampling, and optimization parameters as constants within this module (e.g., S_FACTOR, MAX_ITERATIONS).
    • Create serde-compatible helper functions (e.g., fn default_s_factor() -> f64) for each default value.
    • Audit: Systematically scan the entire codebase, particularly config.rs and commands/place.rs, to identify and list all existing hardcoded default values (e.g., unwrap_or(100)). Ensure a corresponding default is created in the new module.
  • Phase 2: Architect the Layered Configuration Structs

    • Create a new module config/core.rs.
    • Define the primary Config struct and its sub-structs (ForcefieldConfig, OptimizationConfig, etc.). These structs will not use Option<T> for their fields.
    • Implement the Default trait for Config and all sub-structs, using the functions from the defaults.rs module.
    • Annotate all fields with #[serde(default = "...")] to ensure that serde uses our default functions when a field is missing from a TOML file.
    • Move the existing Partial...Config structs (which use Option<T>) into a separate module, config/partial.rs. Their sole purpose will be to parse TOML files.
  • Phase 3: Refactor CLI Arguments for Override Capability

    • In cli.rs:
      • Create a new ConfigArgs struct that mirrors the fields in PartialConfig (all fields as Option<T>).
      • Annotate ConfigArgs with #[derive(Args)].
      • In the main PlaceArgs struct, replace individual config-related arguments (like --s-factor) with #[command(flatten)] pub config_overrides: ConfigArgs.
      • Change the -c, --config argument from required = true to optional (Option<PathBuf>).
    • Unit Test: Add a test to verify that clap correctly parses the new flattened arguments (e.g., --max-iterations 150).
  • Phase 4: Implement and Integrate the New Loading Logic

    • Create a new module config/loader.rs with a public function load_and_merge_config.
    • Implement the three-layer merge logic within this function:
      1. Start by creating an instance of Config::default().
      2. If a config file path is provided, parse it into PartialConfig and merge its Some(...) values into the Config instance.
      3. Merge the Some(...) values from the CLI's ConfigArgs into the Config instance.
    • In commands/place.rs:
      • Completely remove the old PartialPlacementConfig::merge_with_cli function.
      • Replace the configuration logic with a single call to config::loader::load_and_merge_config.
      • Crucial Cleanup: Search for and remove all remaining hardcoded unwrap_or(...) calls related to configuration, ensuring the PlacementConfig from the loader is the sole source of truth.

Metadata

Metadata

Assignees

Labels

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions