Skip to content

Conversation

@pamburus
Copy link
Owner

@pamburus pamburus commented Jan 5, 2026

This PR fixes a bug in the formatting logic where values containing only whitespace characters (spaces, tabs, newlines) were being incorrectly formatted as empty strings instead of being properly quoted.

Problem

When formatting values that consisted entirely of whitespace characters, the auto-trim functionality would remove all content, resulting in empty output. This was incorrect because:

  1. Whitespace-only values should be preserved to maintain data fidelity
  2. Such values should be quoted to clearly indicate they are non-empty strings

Solution

The fix introduces a flexible auto-trim mechanism with flags:

  • Added AutoTrimFlag enum with a PreserveWhiteSpaceOnly flag to control trimming behavior
  • Modified WithAutoTrim trait to accept flags that determine whether whitespace-only content should be preserved
  • Updated ValueFormatAuto to preserve whitespace-only values by passing the PreserveWhiteSpaceOnly flag
  • Enhanced logic to properly quote whitespace-only values (e.g., " ", "\t\t")

Changes

Core formatting logic (src/formatting.rs):

  • Made WithAutoTrim trait accept an AutoTrimFlags parameter
  • Added AutoTrimFlag enum with PreserveWhiteSpaceOnly variant using EnumSet
  • Updated with_auto_trim implementation to check flags before truncating whitespace-only content
  • Modified ValueFormatAuto to preserve whitespace-only values while still trimming trailing whitespace from mixed content
  • Updated quoting condition to ensure whitespace-only values are quoted

Tests (src/formatting/tests.rs):

  • Updated test expectations for whitespace-only inputs (e.g., " " now formats as " " instead of empty string)
  • Added new error handling test for invalid JSON
  • Updated test comments to reflect correct behavior

This fix ensures that all values, including those containing only whitespace, are formatted correctly and unambiguously, improving data integrity and user experience.

@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.55%. Comparing base (8d766a5) to head (c2f2198).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1237   +/-   ##
=======================================
  Coverage   86.54%   86.55%           
=======================================
  Files          61       61           
  Lines       10498    10502    +4     
=======================================
+ Hits         9086     9090    +4     
  Misses       1412     1412           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pamburus pamburus force-pushed the feature/whitespace branch from 5c45e27 to cdd86b3 Compare January 5, 2026 13:09
@pamburus pamburus force-pushed the feature/whitespace branch from cdd86b3 to c2f2198 Compare January 5, 2026 13:16
@pamburus pamburus marked this pull request as ready for review January 5, 2026 13:25
@pamburus pamburus merged commit c87deca into master Jan 5, 2026
13 checks passed
@pamburus pamburus deleted the feature/whitespace branch January 5, 2026 13:25
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.

2 participants