Skip to content

Add TOML configuration file support with JSON backward compatibility (Issue #17)#58

Merged
krjordan merged 1 commit intomainfrom
17-add-toml-configuration-file-support
Jul 29, 2025
Merged

Add TOML configuration file support with JSON backward compatibility (Issue #17)#58
krjordan merged 1 commit intomainfrom
17-add-toml-configuration-file-support

Conversation

@krjordan
Copy link
Owner

Summary

  • Implement TOML configuration file support while maintaining full JSON backward compatibility
  • Add automatic format detection and smart config file discovery
  • Default new installations to TOML format for improved readability and maintainability
  • Preserve all existing functionality with CLI argument override support

Key Features

  • Dual Format Support: Both TOML (preferred) and JSON formats supported
  • Automatic Detection: Format determined by file extension (.toml/.json)
  • Smart Discovery: Automatically finds existing configs in either format (TOML preferred)
  • Backward Compatibility: Existing JSON configs continue to work seamlessly
  • Default to TOML: New installations use more readable TOML format

Changes Made

  • Dependency: Added toml = "0.8" for TOML parsing support
  • Config Format Enum: Added ConfigFormat enum to distinguish between JSON/TOML
  • Format Detection: Implemented detect_format() based on file extension
  • Smart Loading: Enhanced config loading to find existing files in either format
  • Dual Serialization: Save configs in appropriate format based on file extension
  • Path Handling: Updated path generation to support both extensions

Implementation Details

Format Detection Logic

pub fn detect_format(path: &Path) -> ConfigFormat {
    match path.extension().and_then(|ext| ext.to_str()) {
        Some("toml") => ConfigFormat::Toml,
        Some("json") => ConfigFormat::Json,
        _ => ConfigFormat::Toml, // Default to TOML for new configs
    }
}

Smart Config Discovery

When loading configs, the system:

  1. Checks if specified file exists
  2. If not, searches for existing config in either format
  3. Prioritizes TOML, falls back to JSON
  4. Creates new config in TOML format by default

Format-Specific Error Handling

  • JSON parsing errors: "Failed to parse JSON config: ..."
  • TOML parsing errors: "Failed to parse TOML config: ..."
  • Clear error messages help users identify format issues

Testing

  • 55 Tests Passing: 39 unit tests + 16 integration tests
  • TOML Format Tests: Creation, loading, format detection
  • JSON Format Tests: Backward compatibility verification
  • Discovery Tests: Smart config file finding logic
  • CLI Integration: Real-world testing with both formats

Configuration Examples

TOML Format (New Default)

default_printer = "my_printer"

[printers.my_printer]
name = "my_printer"
ip = "192.168.1.100"
device_id = "01S00A000000000"
access_code = "12345678"
port = 8883
use_tls = true

[mqtt_settings]
keep_alive_secs = 30
connection_timeout_secs = 10
retry_attempts = 5
retry_delay_secs = 5
queue_size = 10

JSON Format (Legacy Support)

{
  "printers": {
    "my_printer": {
      "name": "my_printer",
      "ip": "192.168.1.100",
      "device_id": "01S00A000000000",
      "access_code": "12345678",
      "port": 8883,
      "use_tls": true
    }
  },
  "default_printer": "my_printer",
  "mqtt_settings": {
    "keep_alive_secs": 30,
    "connection_timeout_secs": 10,
    "retry_attempts": 5,
    "retry_delay_secs": 5,
    "queue_size": 10
  }
}

Acceptance Criteria ✅

  • Creates default config file if not exists - New configs created in TOML format
  • Loads printer settings from config - Both TOML and JSON formats supported
  • CLI arguments override config values - All existing functionality preserved
  • Validates configuration format - Format-specific error messages
  • Clear error messages for invalid config - JSON/TOML specific parsing errors

Documentation Updates

  • README.md: Added dual format examples and configuration section
  • ROADMAP.md: Marked configuration file support as complete with both formats
  • Code Comments: Enhanced documentation for format detection and loading logic

Migration Path

  • Existing Users: No action required - JSON configs continue working
  • New Users: Automatically get TOML configs for better experience
  • Manual Migration: Users can manually convert by changing extension and format

Quality Assurance

  • All tests passing (55 total)
  • Code formatting clean (cargo fmt)
  • Linting clean (cargo clippy)
  • Real-world testing with both config formats

This implementation provides a smooth transition to TOML while maintaining complete backward compatibility with existing JSON configurations.

Closes #17

…ility

- Add toml dependency for dual format support
- Implement automatic format detection based on file extension
- Add smart config file discovery (TOML preferred, JSON fallback)
- Maintain full backward compatibility with existing JSON configs
- Default new installations to TOML format for better readability
- Update config path handling to support both extensions
- Add comprehensive tests for both TOML and JSON formats
- Update documentation with dual format examples and migration info
- Mark issue #17 as complete in ROADMAP

All acceptance criteria fulfilled:
✅ Creates default config file if not exists (TOML format)
✅ Loads printer settings from config (both formats)
✅ CLI arguments override config values (preserved functionality)
✅ Validates configuration format (format-specific error messages)
✅ Clear error messages for invalid config (JSON/TOML specific)

Resolves #17
@krjordan krjordan self-assigned this Jul 29, 2025
@krjordan krjordan merged commit 0dbd609 into main Jul 29, 2025
13 checks passed
@krjordan krjordan deleted the 17-add-toml-configuration-file-support branch July 29, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add TOML configuration file support

1 participant