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

Use Display in trace or forward Debug in Located, Stateful and Recoverable #482

Open
2 tasks done
axelkar opened this issue Feb 25, 2024 · 8 comments
Open
2 tasks done
Labels
A-combinator Area: combinators C-enhancement Category: Raise on the bar on expectations M-breaking-change Meta: Implementing or merging this will introduce a breaking change.

Comments

@axelkar
Copy link

axelkar commented Feb 25, 2024

Please complete the following tasks

winnow version

0.6.2

Describe your use case

I want to use winnow/debug but the output looks like this:

> terminated                             | Located {
    initia
 > node Document                         | Located {
    initia
  > separated                            | Located {
    initia
   > alt                                 | Located {
    initia

Also, the separated assert looks like this but it isn't as much of an issue:

assert ``separated` separator parser must always consume` failed at Recoverable {
    input: Located {
        initial: Stateful {
            input: "",
            state: "src",
        },
        input: Stateful {
            input: "",
            state: "src",
        },
    },
    errors: [],
    is_recoverable: true,
}

Describe the solution you'd like

let mut debug_slice = format!("{:#?}", input.raw());

-    let mut debug_slice = format!("{:#?}", input.raw());
+    let mut debug_slice = format!("{}", input.raw());

OR do the following for Located, Stateful and Recoverable

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]

-#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
+#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
+impl<I: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Debug for Located<I> {
+    fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
+        self.input.fmt(f)
+    }
+}

Alternatives, if applicable

Make a new trait to display in trace to avoid breaking Debug for other use cases.

Additional Context

@axelkar axelkar added the C-enhancement Category: Raise on the bar on expectations label Feb 25, 2024
@epage epage added A-combinator Area: combinators M-breaking-change Meta: Implementing or merging this will introduce a breaking change. labels Feb 26, 2024
@epage
Copy link
Collaborator

epage commented Feb 26, 2024

Not all streams implement Display, like &[u8]. We offer BStr but I don't want to limit this feature to someone switching off of &[u8].

Alternatively, we could change the alternate form (#) of debug to be a pass through for these wrapper types

@baoyachi
Copy link
Contributor

Perhaps we could define a custom Format trait within winnow, enabling formatting for any arbitrary type.

@epage
Copy link
Collaborator

epage commented Apr 26, 2024

Since most people don't implement a custom stream, a new trait is likely fine. The main question about how we should handle custom tokens. Right now, you can its pretty minimal to support those (see custom token docs) and this gets into slices vs tokes. Maybe we could just use debug alternate on tokens.

This would be a breaking change still. For now, I've gone forward with the workaround I mentioned

@aDotInTheVoid
Copy link

Since most people don't implement a custom stream, a new trait is likely fine.

I wonder if you could instead have .raw() (or equivalent after renaming) take a f: &mut Formatter, and have it be responsible for writing out the tokens.

This would look something like

trait Stream: core::fmt::Debug {
   ...

  fn fmt_remaining(&self, f: &mut core::fmt::Formatter<'_>) {
    core::fmt::Debug::fmt(self, f)
  }
}

This way, adaptor streams like Stateful, Located and Partial can use there forwarding logic for the debug view, while also not hiding information form format!("{:#?}") (in non-winnow code).

It also means that we can remove the use of {:#?} in the debug view, providing a good output for when stream is &[Token] (cc #516).

@epage epage added this to the 0.7.0 milestone Jan 3, 2025
@epage
Copy link
Collaborator

epage commented Jan 3, 2025

Was looking at making changes for this but I can't reproduce the original problem

#!/usr/bin/env nargo
---
package.edition = "2021"
[dependencies]
winnow.version = "=0.6.2"
winnow.features = ["debug"]
---

use winnow::prelude::*;

fn main() {
    let input = "5";
    let mut input = winnow::stream::Located::new(input);
    winnow::ascii::digit1::<_, ()>.parse_next(&mut input).unwrap();
}

Stream::raw for Located always forwards to the inner type, so its unclear how they would have gotten Located { in the output. This was implemented back in #284. I believe this was released in 0.5.0.

Even if I roll back to 0.4 I don't see it

#!/usr/bin/env nargo
---
package.edition = "2021"
[dependencies]
winnow.version = "0.4"
winnow.features = ["debug"]
---

use winnow::prelude::*;

fn main() {
    let input = "5";
    let input = winnow::stream::Located::new(input);
    winnow::ascii::digit1::<_, ()>.parse_next(input).unwrap();
}

epage added a commit to epage/winnow that referenced this issue Jan 3, 2025
This reverts commit 680b41a.

PR winnow-rs#514 was meant to help winnow-rs#482 but I can't reproduce the problem
@epage
Copy link
Collaborator

epage commented Jan 3, 2025

I have a tracing change posted in #661 but I'm trying to decide if it fills the right needs and will help enough. Feedback would be appreciated!

@epage
Copy link
Collaborator

epage commented Jan 16, 2025

I realized that a good reason to move forward is so that &[u8] has some nicer rendering

@epage
Copy link
Collaborator

epage commented Jan 24, 2025

Except we don't implement Stream for &[u8] but &[T].

@epage epage removed this from the 0.7.0 milestone Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-combinator Area: combinators C-enhancement Category: Raise on the bar on expectations M-breaking-change Meta: Implementing or merging this will introduce a breaking change.
Projects
None yet
Development

No branches or pull requests

4 participants