Skip to content

Commit a01c639

Browse files
committed
feat: add conditional formatting based on alternate flag
1 parent f2949fd commit a01c639

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

pest/src/iterators/pair.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// option. All files in the project carrying such notice may not be copied,
88
// modified, or distributed except according to those terms.
99

10+
use alloc::format;
1011
use alloc::rc::Rc;
1112
#[cfg(feature = "pretty-print")]
1213
use alloc::string::String;
@@ -339,7 +340,30 @@ impl<R: RuleType> fmt::Debug for Pair<'_, R> {
339340

340341
impl<R: RuleType> fmt::Display for Pair<'_, R> {
341342
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
342-
write!(f, "{}", self.as_str())
343+
if f.alternate() {
344+
let rule = self.as_rule();
345+
let start = self.pos(self.start);
346+
let end = self.pos(self.pair());
347+
let mut pairs = self.clone().into_inner().peekable();
348+
349+
if pairs.peek().is_none() {
350+
write!(f, "{:?}({}, {})", rule, start, end)
351+
} else {
352+
write!(
353+
f,
354+
"{:?}({}, {}, [{}])",
355+
rule,
356+
start,
357+
end,
358+
pairs
359+
.map(|pair| format!("{:#}", pair))
360+
.collect::<Vec<_>>()
361+
.join(", ")
362+
)
363+
}
364+
} else {
365+
write!(f, "{}", self.as_str())
366+
}
343367
}
344368
}
345369

@@ -388,7 +412,7 @@ impl<R: RuleType> ::serde::Serialize for Pair<'_, R> {
388412

389413
#[cfg(test)]
390414
mod tests {
391-
use crate::alloc::string::ToString;
415+
use crate::alloc::{borrow::ToOwned, format, string::ToString};
392416
use crate::macros::tests::*;
393417
use crate::parser::Parser;
394418

@@ -446,4 +470,10 @@ mod tests {
446470

447471
assert_eq!(pair.to_string(), pair.as_str().to_string());
448472
}
473+
#[test]
474+
fn alternate_format() {
475+
let pair = AbcParser::parse(Rule::a, "abcde").unwrap().next().unwrap();
476+
assert_eq!(format!("{}", pair), "abc".to_owned());
477+
assert_eq!(format!("{:#}", pair), "a(0, 3, [b(1, 2)])".to_owned());
478+
}
449479
}

pest/src/iterators/pairs.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,18 @@ impl<R: RuleType> fmt::Debug for Pairs<'_, R> {
465465

466466
impl<R: RuleType> fmt::Display for Pairs<'_, R> {
467467
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
468-
write!(
469-
f,
470-
"[{}]",
471-
self.clone()
472-
.map(|pair| format!("{}", pair))
473-
.collect::<Vec<_>>()
474-
.join(", ")
475-
)
468+
let inner = self
469+
.clone()
470+
.map(|pair| {
471+
if f.alternate() {
472+
format!("{pair:#}")
473+
} else {
474+
format!("{pair}")
475+
}
476+
})
477+
.collect::<Vec<_>>()
478+
.join(", ");
479+
write!(f, "[{inner}]")
476480
}
477481
}
478482

@@ -622,6 +626,10 @@ mod tests {
622626
let pairs = AbcParser::parse(Rule::a, "abcde").unwrap();
623627

624628
assert_eq!(format!("{}", pairs), "[abc, e]".to_owned());
629+
assert_eq!(
630+
format!("{:#}", pairs),
631+
"[a(0, 3, [b(1, 2)]), c(4, 5)]".to_owned()
632+
);
625633
}
626634

627635
#[test]

0 commit comments

Comments
 (0)