-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor Configuration System for Usability and Zero-Config Execution #41
Description
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.rsandcommands/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.
- Create a new module
-
Phase 2: Architect the Layered Configuration Structs
- Create a new module
config/core.rs. - Define the primary
Configstruct and its sub-structs (ForcefieldConfig,OptimizationConfig, etc.). These structs will not useOption<T>for their fields. - Implement the
Defaulttrait forConfigand all sub-structs, using the functions from thedefaults.rsmodule. - Annotate all fields with
#[serde(default = "...")]to ensure thatserdeuses our default functions when a field is missing from a TOML file. - Move the existing
Partial...Configstructs (which useOption<T>) into a separate module,config/partial.rs. Their sole purpose will be to parse TOML files.
- Create a new module
-
Phase 3: Refactor CLI Arguments for Override Capability
- In
cli.rs:- Create a new
ConfigArgsstruct that mirrors the fields inPartialConfig(all fields asOption<T>). - Annotate
ConfigArgswith#[derive(Args)]. - In the main
PlaceArgsstruct, replace individual config-related arguments (like--s-factor) with#[command(flatten)] pub config_overrides: ConfigArgs. - Change the
-c, --configargument fromrequired = trueto optional (Option<PathBuf>).
- Create a new
- Unit Test: Add a test to verify that
clapcorrectly parses the new flattened arguments (e.g.,--max-iterations 150).
- In
-
Phase 4: Implement and Integrate the New Loading Logic
- Create a new module
config/loader.rswith a public functionload_and_merge_config. - Implement the three-layer merge logic within this function:
- Start by creating an instance of
Config::default(). - If a config file path is provided, parse it into
PartialConfigand merge itsSome(...)values into theConfiginstance. - Merge the
Some(...)values from the CLI'sConfigArgsinto theConfiginstance.
- Start by creating an instance of
- In
commands/place.rs:- Completely remove the old
PartialPlacementConfig::merge_with_clifunction. - 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 thePlacementConfigfrom the loader is the sole source of truth.
- Completely remove the old
- Create a new module