From da1b5015b912ec7ae35940d9c102a0dd44f73141 Mon Sep 17 00:00:00 2001 From: Teolhyn Date: Thu, 29 Jan 2026 10:44:46 +0200 Subject: [PATCH 1/4] fix: changed Display impl for Pair to match as_str() --- pest/src/iterators/pair.rs | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/pest/src/iterators/pair.rs b/pest/src/iterators/pair.rs index 85fe297b..18e9d7d5 100644 --- a/pest/src/iterators/pair.rs +++ b/pest/src/iterators/pair.rs @@ -340,26 +340,7 @@ impl fmt::Debug for Pair<'_, R> { impl fmt::Display for Pair<'_, R> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let rule = self.as_rule(); - let start = self.pos(self.start); - let end = self.pos(self.pair()); - let mut pairs = self.clone().into_inner().peekable(); - - if pairs.peek().is_none() { - write!(f, "{:?}({}, {})", rule, start, end) - } else { - write!( - f, - "{:?}({}, {}, [{}])", - rule, - start, - end, - pairs - .map(|pair| format!("{}", pair)) - .collect::>() - .join(", ") - ) - } + write!(f, "{}", self.as_str()) } } @@ -408,6 +389,7 @@ impl ::serde::Serialize for Pair<'_, R> { #[cfg(test)] mod tests { + use crate::alloc::string::ToString; use crate::macros::tests::*; use crate::parser::Parser; @@ -459,4 +441,10 @@ mod tests { assert_eq!(input, pair.get_input()); } + #[test] + fn pair_to_string_matches_as_str() { + let pair = AbcParser::parse(Rule::a, "abcde").unwrap().next().unwrap(); + + assert_eq!(pair.to_string(), pair.as_str().to_string()); + } } From f6b75ef5ea5ee813144bb15c6ee0f427da7888fa Mon Sep 17 00:00:00 2001 From: Teolhyn Date: Thu, 29 Jan 2026 10:45:16 +0200 Subject: [PATCH 2/4] test: update pairs display test to match the new Display implementation --- pest/src/iterators/pairs.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pest/src/iterators/pairs.rs b/pest/src/iterators/pairs.rs index 74dae4a1..d7cc973a 100644 --- a/pest/src/iterators/pairs.rs +++ b/pest/src/iterators/pairs.rs @@ -621,10 +621,7 @@ mod tests { fn pairs_display() { let pairs = AbcParser::parse(Rule::a, "abcde").unwrap(); - assert_eq!( - format!("{}", pairs), - "[a(0, 3, [b(1, 2)]), c(4, 5)]".to_owned() - ); + assert_eq!(format!("{}", pairs), "[abc, e]".to_owned()); } #[test] From f2949fdfaee525041c0d4969b7e75f74dc0f4cd5 Mon Sep 17 00:00:00 2001 From: Teolhyn Date: Thu, 29 Jan 2026 11:18:08 +0200 Subject: [PATCH 3/4] chore: move alloc::format import under pretty print config --- pest/src/iterators/pair.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pest/src/iterators/pair.rs b/pest/src/iterators/pair.rs index 18e9d7d5..0a1a7180 100644 --- a/pest/src/iterators/pair.rs +++ b/pest/src/iterators/pair.rs @@ -7,7 +7,6 @@ // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. -use alloc::format; use alloc::rc::Rc; #[cfg(feature = "pretty-print")] use alloc::string::String; From a01c639ca2bdb8326f832dacd7b9a115818e264c Mon Sep 17 00:00:00 2001 From: Teolhyn Date: Thu, 29 Jan 2026 13:56:59 +0200 Subject: [PATCH 4/4] feat: add conditional formatting based on alternate flag --- pest/src/iterators/pair.rs | 34 ++++++++++++++++++++++++++++++++-- pest/src/iterators/pairs.rs | 24 ++++++++++++++++-------- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/pest/src/iterators/pair.rs b/pest/src/iterators/pair.rs index 0a1a7180..39ffc795 100644 --- a/pest/src/iterators/pair.rs +++ b/pest/src/iterators/pair.rs @@ -7,6 +7,7 @@ // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. +use alloc::format; use alloc::rc::Rc; #[cfg(feature = "pretty-print")] use alloc::string::String; @@ -339,7 +340,30 @@ impl fmt::Debug for Pair<'_, R> { impl fmt::Display for Pair<'_, R> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.as_str()) + if f.alternate() { + let rule = self.as_rule(); + let start = self.pos(self.start); + let end = self.pos(self.pair()); + let mut pairs = self.clone().into_inner().peekable(); + + if pairs.peek().is_none() { + write!(f, "{:?}({}, {})", rule, start, end) + } else { + write!( + f, + "{:?}({}, {}, [{}])", + rule, + start, + end, + pairs + .map(|pair| format!("{:#}", pair)) + .collect::>() + .join(", ") + ) + } + } else { + write!(f, "{}", self.as_str()) + } } } @@ -388,7 +412,7 @@ impl ::serde::Serialize for Pair<'_, R> { #[cfg(test)] mod tests { - use crate::alloc::string::ToString; + use crate::alloc::{borrow::ToOwned, format, string::ToString}; use crate::macros::tests::*; use crate::parser::Parser; @@ -446,4 +470,10 @@ mod tests { assert_eq!(pair.to_string(), pair.as_str().to_string()); } + #[test] + fn alternate_format() { + let pair = AbcParser::parse(Rule::a, "abcde").unwrap().next().unwrap(); + assert_eq!(format!("{}", pair), "abc".to_owned()); + assert_eq!(format!("{:#}", pair), "a(0, 3, [b(1, 2)])".to_owned()); + } } diff --git a/pest/src/iterators/pairs.rs b/pest/src/iterators/pairs.rs index d7cc973a..b6d1094b 100644 --- a/pest/src/iterators/pairs.rs +++ b/pest/src/iterators/pairs.rs @@ -465,14 +465,18 @@ impl fmt::Debug for Pairs<'_, R> { impl fmt::Display for Pairs<'_, R> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "[{}]", - self.clone() - .map(|pair| format!("{}", pair)) - .collect::>() - .join(", ") - ) + let inner = self + .clone() + .map(|pair| { + if f.alternate() { + format!("{pair:#}") + } else { + format!("{pair}") + } + }) + .collect::>() + .join(", "); + write!(f, "[{inner}]") } } @@ -622,6 +626,10 @@ mod tests { let pairs = AbcParser::parse(Rule::a, "abcde").unwrap(); assert_eq!(format!("{}", pairs), "[abc, e]".to_owned()); + assert_eq!( + format!("{:#}", pairs), + "[a(0, 3, [b(1, 2)]), c(4, 5)]".to_owned() + ); } #[test]