Skip to content

fix: Add per-field serde defaults to Colors for partial config support#6

Merged
oubiwann merged 3 commits into
release/0.6.xfrom
fix/colors-partial-config-defaults
Jan 23, 2026
Merged

fix: Add per-field serde defaults to Colors for partial config support#6
oubiwann merged 3 commits into
release/0.6.xfrom
fix/colors-partial-config-defaults

Conversation

@oubiwann
Copy link
Copy Markdown
Contributor

Problem

When users specify [logging.colors] in their TOML config with only some fields (e.g., just timestamp), serde would deserialize ALL fields of the Colors struct. Unspecified fields would be set to None instead of using the struct's Default implementation values.

This caused twyg to lose colored output for all unspecified color fields.

Example of Broken Behavior

Config file:

[logging.colors]
timestamp = { fg = "HiBlack", bg = "Reset" }

Before this fix:

  • timestamp: Some(HiBlack) ✅ (from config)
  • level_info: None ❌ (should be HiGreen)
  • message: None ❌ (should be Green)
  • All other 11 fields: None

Result: Only timestamps were colored, all other log output was plain white text.

Solution

Add #[serde(default = "default_fn")] attribute to each field in the Colors struct, with corresponding default functions. This makes serde apply per-field defaults when fields are missing from the config.

After this fix:

  • timestamp: Some(HiBlack) ✅ (from config)
  • level_info: Some(HiGreen) ✅ (from default)
  • message: Some(Green) ✅ (from default)
  • All other fields: Some(...) ✅ (from defaults)

Result: Users can now customize individual colors while keeping defaults for everything else.

Changes

  1. Added #[serde(default = "default_*")] to all 13 fields in Colors struct
  2. Added 13 default functions (default_timestamp, default_level_info, etc.) matching values from Colors::default()
  3. Added test test_colors_partial_deserialization_preserves_defaults to verify the fix

Testing

  • ✅ All existing tests pass (147 unit tests + 15 integration tests + 10 doc tests)
  • ✅ New test confirms partial config deserialization works correctly
  • ✅ Verified with burrow project using twyg (real-world usage)

Example Usage

Users can now do this in their config:

[logging.colors]
# Only customize timestamp - all other colors use twyg defaults
timestamp = { fg = "HiBlack", bg = "Reset" }

Or this:

[logging.colors]
# Customize a few colors
timestamp = { fg = "HiBlack", bg = "Reset" }
level_error = { fg = "HiRed", bg = "Reset" }
arrow = { fg = "HiMagenta", bg = "Reset" }
# All other fields automatically use defaults

Breaking Changes

None. This is a pure bug fix that makes partial configuration work as users would naturally expect.

oubiwann and others added 3 commits January 22, 2026 17:27
Add #[serde(default)] attributes to each field in the Colors struct to
support partial color configuration in TOML/JSON files.

Problem:
When users specify [logging.colors] with only some fields (e.g., just
timestamp), serde would deserialize ALL fields, setting unspecified
fields to None instead of using the struct's Default implementation.
This caused twyg to lose its colored output for unspecified fields.

Example of broken behavior:
```toml
[logging.colors]
timestamp = { fg = "HiBlack", bg = "Reset" }
```

Before this fix:
- timestamp: Some(HiBlack) ✓
- level_info: None ✗ (should be HiGreen)
- message: None ✗ (should be Green)
- All other fields: None ✗

Solution:
Add #[serde(default = "default_fn")] to each field with corresponding
default functions. Now each field independently falls back to its
default value when not specified in the config.

After this fix:
- timestamp: Some(HiBlack) ✓ (from config)
- level_info: Some(HiGreen) ✓ (from default)
- message: Some(Green) ✓ (from default)
- All other fields: Some(...) ✓ (from defaults)

Changes:
- Added #[serde(default = "...")] to all 13 fields in Colors struct
- Added 13 default_* functions matching Colors::default() values
- Added test to verify partial deserialization preserves defaults

Testing:
- All existing tests pass
- New test confirms partial config works correctly
- Verified with burrow project using twyg

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Add toml = "0.9" to dev-dependencies
- Change test from JSON to TOML deserialization
- Matches how users actually configure colors in TOML files
@oubiwann oubiwann merged commit 3e6a472 into release/0.6.x Jan 23, 2026
4 checks passed
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.

1 participant