Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 1 commit into from

Conversation

aDotInTheVoid
Copy link

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,

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,
```
@@ -146,7 +146,7 @@ pub 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!("{:?}", input.raw());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently use the alternate debug form so our custom streams show a "nicer" view. We used this more in #514. #482 is discussing this further.

The main thing we could do is either

  • Measure to the newline
  • split off and only show the first line

@coveralls
Copy link

Pull Request Test Coverage Report for Build 8898483497

Details

  • 0 of 1 (0.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 41.61%

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

💛 - Coveralls

@aDotInTheVoid
Copy link
Author

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
Copy link
Author

See also #482 (comment), for some thaughts on a potential change to Stream to make this easier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants