Skip to content

Fix: change display impl to match as str#1151

Merged
tomtau merged 4 commits intopest-parser:masterfrom
Teolhyn:fix/change-display-impl-to-match-as-str
Jan 29, 2026
Merged

Fix: change display impl to match as str#1151
tomtau merged 4 commits intopest-parser:masterfrom
Teolhyn:fix/change-display-impl-to-match-as-str

Conversation

@Teolhyn
Copy link
Contributor

@Teolhyn Teolhyn commented Jan 29, 2026

Fixes issue #1149.

Bug description from the original issue:

When you do pair.to_string() and pair.as_str().to_string() you get different strings - the former gives the pair name and it's range, the latter the actual span content . This is pretty confusing as foo.as_str().to_string() should generally be equivalent to foo.to_string(). Users the library will think it is and it will lead to bugs. I personally did so and it wasn't pleasant to realize what was the cause

What was done

Display implementation for Pair was changed as this is where to_stringcomes from. Rather than giving pair name and it's range it now simply returns the span content using as_str, which quarantees that as_str().to_string() and to_string() will give the same output for Pair, even if for some reason as_str would be changed in future. I also added a simple unit test for this.

Considerations

This changes how displaying of Pairs work. However, there was already a Debug implementation that would give the whole Pair struct.

This is my first ever (code) contribution to OSS, so any feedback would be appreciated.

Summary by CodeRabbit

  • Refactor

    • Rendering of parsed items and collections now defaults to their plain string content; an alternate (pretty-printed) format shows a nested, structured view when requested.
  • Tests

    • Added and updated tests to ensure plain-string rendering and the alternate pretty-printed formatting behave as expected.

✏️ Tip: You can customize this high-level summary in your review settings.

@Teolhyn Teolhyn requested a review from a team as a code owner January 29, 2026 09:04
@Teolhyn Teolhyn requested review from tomtau and removed request for a team January 29, 2026 09:04
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 29, 2026

📝 Walkthrough

Walkthrough

Display implementations for Pair and Pairs were changed to support alternate (“pretty”) formatting: normal formatting renders the pair(s) as their source string(s), while alternate formatting emits a nested, pretty-printed representation including rule, start/end, and inner pairs. Tests were added/updated accordingly.

Changes

Cohort / File(s) Summary
Pair Display & tests
pest/src/iterators/pair.rs
Changed Display for Pair to render self.as_str() for normal formatting and a nested pretty-printed structure when f.alternate() is true; updated imports and added pair_to_string_matches_as_str and alternate_format tests.
Pairs Display & tests
pest/src/iterators/pairs.rs
Changed Display for Pairs to join inner Pair renderings, using normal formatting for non-alternate and pretty/debug-style ({pair:#}) for alternate; updated pairs_display test expectation to match new non-alternate output and preserved alternate formatting expectations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I nibble tokens, tidy and neat,
I fold inner hops into a neat beat.
When asked for pretty, I stretch and show,
Else I whisper the string that I know. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix: change display impl to match as str' directly describes the main change: modifying the Display implementation for Pair to match the output of as_str().

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the old formatting can be preserved with the "alternate" flag on the formatter (or the other way round)?
so one can have both with format!("{pair}") and format!("{pair:#}") ?

// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use alloc::format;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like format is used in cfg("pretty-print") so I added the import under feature flag.

@Teolhyn Teolhyn force-pushed the fix/change-display-impl-to-match-as-str branch from c4ca742 to f2949fd Compare January 29, 2026 11:14
@Teolhyn
Copy link
Contributor Author

Teolhyn commented Jan 29, 2026

maybe the old formatting can be preserved with the "alternate" flag on the formatter (or the other way round)? so one can have both with format!("{pair}") and format!("{pair:#}") ?

I added this now so that format!("{pair}") uses the new formatting that matches as_str and format!("{pair:#}") matches the old formatting. I updated tests for pair and pairs. Let me know what you think.

@Teolhyn Teolhyn requested a review from tomtau January 29, 2026 12:05
@tomtau tomtau merged commit 182ffb4 into pest-parser:master Jan 29, 2026
9 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.

to_string and as_str have different implementations for Pair, which is pretty confusing and misleading

2 participants