winnow-rs / winnow

Making parsing a breeze
https://docs.rs/winnow
Other
525 stars 40 forks source link

Don't split input over multiple lines in debug view. #516

Closed aDotInTheVoid closed 4 months ago

aDotInTheVoid commented 5 months ago

If the Input type is a slice, using {:#?} will insert newlines into the debug view. This messes up the output, which is counting charecters to fill a single line.

Before:

> alt                                                      | [
    Call @ 0..4,
    Ident
 > separated_foldl1                                        | [
    Call @ 0..4,
    Ident
  > separated_foldl1                                       | [
    Call @ 0..4,
    Ident
   > separated_foldl1                                      | [
    Call @ 0..4,
    Ident
    > separated_foldl1                                     | [
    Call @ 0..4,
    Ident
     > separated_foldl1                                    | [
    Call @ 0..4,
    Ident
      > separated_foldl1                                   | [
    Call @ 0..4,
    Ident
       > alt                                               | [
    Call @ 0..4,
    Ident
        > alt                                              | [
    Call @ 0..4,
    Ident
         > one_of                                          | [
    Call @ 0..4,
    Ident
          > any                                            | [
    Call @ 0..4,
    Ident

After:

> alt                                                      | [Call @ 0..4, Ident @ 5..19,
 > separated_foldl1                                        | [Call @ 0..4, Ident @ 5..19,
  > separated_foldl1                                       | [Call @ 0..4, Ident @ 5..19,
   > separated_foldl1                                      | [Call @ 0..4, Ident @ 5..19,
    > separated_foldl1                                     | [Call @ 0..4, Ident @ 5..19,
     > separated_foldl1                                    | [Call @ 0..4, Ident @ 5..19,
      > separated_foldl1                                   | [Call @ 0..4, Ident @ 5..19,
       > alt                                               | [Call @ 0..4, Ident @ 5..19,
        > alt                                              | [Call @ 0..4, Ident @ 5..19,
         > one_of                                          | [Call @ 0..4, Ident @ 5..19,
          > any                                            | [Call @ 0..4, Ident @ 5..19,
coveralls commented 5 months ago

Pull Request Test Coverage Report for Build 8898483497

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
src/combinator/debug/internals.rs 0 1 0.0%
<!-- Total: 0 1 0.0% -->
Totals Coverage Status
Change from base Build 8854777459: 0.03%
Covered Lines: 1277
Relevant Lines: 3069

💛 - Coveralls
aDotInTheVoid commented 4 months ago

Ah, (ab)using f.alterante() to switch input works

impl fmt::Debug for Input<'_, '_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            let toks = self.remaining_input();
            // Don't use Debug::fmt(f, toks) to force non-alternate writing, see winnow#516
            write!(f, "{toks:?}")
        } else {
            f.debug_struct("Input")
                .field("source", &self.source)
                .field("offset", &self.offset)
                .field("acx", &self.acx)
                .finish()
        }
    }
}

(where .remaining_input() returns the slice still to be parsed).

It does unfortunatly mean that dbg!(i) won't show the offset, but I think it's a good tradeoff for me for now.

aDotInTheVoid commented 4 months ago

See also https://github.com/winnow-rs/winnow/issues/482#issuecomment-2094500462, for some thaughts on a potential change to Stream to make this easier