diff --git a/src/combinator/debug/internals.rs b/src/combinator/debug/internals.rs index d2103746..02c68b85 100644 --- a/src/combinator/debug/internals.rs +++ b/src/combinator/debug/internals.rs @@ -146,7 +146,7 @@ pub(crate) fn start( // 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() @@ -299,3 +299,29 @@ fn columns_env() -> Option { .ok() .and_then(|c| c.parse::().ok()) } + +fn from_fn) -> core::fmt::Result>(f: F) -> FromFn { + FromFn(f) +} + +struct FromFn(F) +where + F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result; + +impl core::fmt::Debug for FromFn +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 core::fmt::Display for FromFn +where + F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + (self.0)(f) + } +} diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 08879829..c4ec729d 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -693,6 +693,11 @@ pub trait Stream: Offset<::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 { + write!(f, "{:#?}", self.raw()) + } } impl<'i, T> Stream for &'i [T]