Skip to content

Commit

Permalink
feat(stream): Give streams direct control over 'trace' output
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jan 3, 2025
1 parent d1baf10 commit 3c3dd56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/combinator/debug/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub(crate) fn start<I: Stream>(

// The debug version of `slice` might be wider, either due to rendering one byte as two nibbles or
// escaping in strings.
let mut debug_slice = format!("{:#?}", input.raw());
let mut debug_slice = format!("{:?}", from_fn(|f| input.trace(f)));
let (debug_slice, eof) = if let Some(debug_offset) = debug_slice
.char_indices()
.enumerate()
Expand Down Expand Up @@ -299,3 +299,29 @@ fn columns_env() -> Option<usize> {
.ok()
.and_then(|c| c.parse::<usize>().ok())
}

fn from_fn<F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result>(f: F) -> FromFn<F> {
FromFn(f)
}

struct FromFn<F>(F)
where
F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result;

impl<F> core::fmt::Debug for FromFn<F>
where
F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.0)(f)
}
}

impl<F> core::fmt::Display for FromFn<F>
where
F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.0)(f)
}
}
5 changes: 5 additions & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,11 @@ pub trait Stream: Offset<<Self as Stream>::Checkpoint> + crate::lib::std::fmt::D

/// Return the inner-most stream
fn raw(&self) -> &dyn crate::lib::std::fmt::Debug;

/// Write out a single-line summary of the current parse location
fn trace(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {

Check warning

Code scanning / clippy

used import from std instead of core Warning

used import from std instead of core

Check warning

Code scanning / clippy

used import from std instead of core Warning

used import from std instead of core
write!(f, "{:#?}", self.raw())
}
}

impl<'i, T> Stream for &'i [T]
Expand Down

0 comments on commit 3c3dd56

Please sign in to comment.