From 680b41a341b24bc1719ad560fd06e002e3ea9141 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 26 Apr 2024 09:11:55 -0500 Subject: [PATCH] fix(debug): Improve traces for wrapped Inputs Part of #482 --- src/stream/mod.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 91ce5a48..a5721866 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -106,7 +106,7 @@ impl BStr { /// byte offsets to line numbers. /// /// See [`Parser::span`][crate::Parser::span] and [`Parser::with_span`][crate::Parser::with_span] for more details -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord)] #[doc(alias = "LocatedSpan")] pub struct Located { initial: I, @@ -150,12 +150,19 @@ impl crate::lib::std::fmt::Display for Located } } +impl crate::lib::std::fmt::Debug for Located { + #[inline] + fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result { + self.input.fmt(f) + } +} + /// Allow recovering from parse errors, capturing them as the parser continues /// /// Generally, this will be used indirectly via /// [`RecoverableParser::recoverable_parse`][crate::RecoverableParser::recoverable_parse]. #[cfg(feature = "unstable-recover")] -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct Recoverable where I: Stream, @@ -228,6 +235,24 @@ where } } +#[cfg(feature = "unstable-recover")] +impl + crate::lib::std::fmt::Debug for Recoverable +{ + #[inline] + fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result { + if f.alternate() { + self.input.fmt(f) + } else { + f.debug_struct("Recoverable") + .field("input", &self.input) + .field("errors", &self.errors) + .field("is_recoverable", &self.is_recoverable) + .finish() + } + } +} + /// Thread global state through your parsers /// /// Use cases @@ -266,7 +291,7 @@ where /// let output = word.parse(input).unwrap(); /// assert_eq!(state.get(), 1); /// ``` -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Copy, Eq, PartialEq)] #[doc(alias = "LocatedSpan")] pub struct Stateful { /// Inner input being wrapped in state @@ -297,6 +322,22 @@ impl crate::lib::std::fmt::Display for Stat } } +impl crate::lib::std::fmt::Debug + for Stateful +{ + #[inline] + fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result { + if f.alternate() { + self.input.fmt(f) + } else { + f.debug_struct("Stateful") + .field("input", &self.input) + .field("state", &self.state) + .finish() + } + } +} + /// Mark the input as a partial buffer for streaming input. /// /// Complete input means that we already have all of the data. This will be the common case with @@ -361,7 +402,7 @@ impl crate::lib::std::fmt::Display for Stat /// // while the complete version knows that all of the data is there /// assert_eq!(alpha0_complete.parse_peek("abcd"), Ok(("", "abcd"))); /// ``` -#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Partial { input: I, partial: bool, @@ -412,6 +453,20 @@ impl crate::lib::std::fmt::Display for Partial } } +impl crate::lib::std::fmt::Debug for Partial { + #[inline] + fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result { + if f.alternate() { + self.input.fmt(f) + } else { + f.debug_struct("Partial") + .field("input", &self.input) + .field("partial", &self.partial) + .finish() + } + } +} + /// Abstract method to calculate the input length pub trait SliceLen { /// Calculates the input length, as indicated by its name,