Skip to content

[DEV] Add validation for configuration file values #97

@Firstp1ck

Description

@Firstp1ck

Summary

Implement comprehensive validation for configuration file values to prevent runtime errors from invalid settings. Add validation for timeout values, percentages, URLs, colors, enums, and other configuration parameters with helpful error messages.

Files to modify

  • src/theme/config/settings_ensure.rs (add validation functions)
  • src/theme/config/theme_loader.rs (add color format validation)
  • src/theme/config/keybinds_loader.rs (if exists, add keybind validation)
  • src/theme/types.rs (add validation methods to Settings struct)

Expected behavior

When invalid configuration values are detected:

  1. Show clear, actionable error messages explaining what's wrong
  2. Suggest valid alternatives or ranges where applicable
  3. Fall back to default values for individual invalid settings
  4. Allow the application to start with partially valid configuration
  5. Log warnings for invalid values that were corrected

Implementation approach

  1. Add validation functions:

    • validate_layout_percentages() - ensure left/center/right sum to 100
    • validate_timeout_values() - check updates_refresh_interval is reasonable
    • validate_age_limits() - validate news age limits are positive
    • validate_mirror_count() - ensure mirror_count is reasonable
    • validate_color_formats() - verify hex/RGB color formats
    • validate_enum_values() - check sort_mode, package_marker are valid
    • validate_locale() - verify locale is supported or empty
  2. Integrate validation:

    • Call validation during config loading in settings_ensure.rs
    • Return detailed error messages with suggestions
    • Fall back to defaults for invalid individual values
    • Continue with valid configuration values
  3. Error messaging:

    • Include the invalid value and line number where possible
    • Suggest valid alternatives (e.g., "use 30 instead of -5")
    • Explain the constraint (e.g., "must be between 1-3600 seconds")

Testing

  • cargo check passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • cargo test -- --test-threads=1 passes
  • Test validation of invalid layout percentages (don't sum to 100)
  • Test validation of negative timeout values
  • Test validation of invalid color formats (#GGG, invalid hex)
  • Test validation of unsupported enum values
  • Test validation of invalid locale codes
  • Test fallback behavior when some values are invalid
  • Test error messages contain helpful suggestions

Additional context

Configuration values that need validation:

  • Layout percentages: layout_left_pct, layout_center_pct, layout_right_pct must sum to 100
  • Timeouts: updates_refresh_interval should be 1-3600 seconds
  • Age limits: news_max_age_days, startup_news_max_age_days should be positive or None
  • Mirror count: mirror_count should be 1-100
  • Colors: All theme colors should be valid hex (#RRGGBB) or RGB (R,G,B) format
  • Enums: sort_mode, package_marker, installed_packages_mode should be valid variants
  • Locale: Should be empty, "en-US", "de-DE", or "hu-HU"
  • URLs: Any URL fields should be valid URLs (though none currently exist)

Follow existing error handling patterns in the codebase. Validation should be non-blocking - invalid individual values should fall back to defaults while preserving valid configuration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions