Skip to content

Commit

Permalink
fix(debug): Improve traces for wrapped Inputs
Browse files Browse the repository at this point in the history
Part of #482
  • Loading branch information
epage committed Apr 26, 2024
1 parent 73211ad commit 680b41a
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I> {
initial: I,
Expand Down Expand Up @@ -150,12 +150,19 @@ impl<I: crate::lib::std::fmt::Display> crate::lib::std::fmt::Display for Located
}
}

impl<I: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug for Located<I> {
#[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<I, E>
where
I: Stream,
Expand Down Expand Up @@ -228,6 +235,24 @@ where
}
}

#[cfg(feature = "unstable-recover")]
impl<I: Stream + crate::lib::std::fmt::Debug, E: crate::lib::std::fmt::Debug>
crate::lib::std::fmt::Debug for Recoverable<I, E>
{
#[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
Expand Down Expand Up @@ -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<I, S> {
/// Inner input being wrapped in state
Expand Down Expand Up @@ -297,6 +322,22 @@ impl<I: crate::lib::std::fmt::Display, S> crate::lib::std::fmt::Display for Stat
}
}

impl<I: crate::lib::std::fmt::Debug, S: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug
for Stateful<I, S>
{
#[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
Expand Down Expand Up @@ -361,7 +402,7 @@ impl<I: crate::lib::std::fmt::Display, S> 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<I> {
input: I,
partial: bool,
Expand Down Expand Up @@ -412,6 +453,20 @@ impl<I: crate::lib::std::fmt::Display> crate::lib::std::fmt::Display for Partial
}
}

impl<I: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug for Partial<I> {
#[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,
Expand Down

0 comments on commit 680b41a

Please sign in to comment.