winnow/combinator/debug/
mod.rs1#![cfg_attr(feature = "debug", allow(clippy::std_instead_of_core))]
2
3#[cfg(feature = "debug")]
4mod internals;
5
6use crate::error::ErrMode;
7use crate::stream::Stream;
8use crate::Parser;
9
10#[cfg_attr(not(feature = "debug"), allow(unused_variables))]
38#[cfg_attr(not(feature = "debug"), allow(unused_mut))]
39#[cfg_attr(not(feature = "debug"), inline(always))]
40pub fn trace<I: Stream, O, E>(
41 name: impl crate::lib::std::fmt::Display,
42 parser: impl Parser<I, O, E>,
43) -> impl Parser<I, O, E> {
44 #[cfg(feature = "debug")]
45 {
46 internals::Trace::new(parser, name)
47 }
48 #[cfg(not(feature = "debug"))]
49 {
50 parser
51 }
52}
53
54#[cfg_attr(not(feature = "debug"), allow(unused_variables))]
55pub(crate) fn trace_result<T, E>(
56 name: impl crate::lib::std::fmt::Display,
57 res: &Result<T, ErrMode<E>>,
58) {
59 #[cfg(feature = "debug")]
60 {
61 let depth = internals::Depth::existing();
62 let severity = internals::Severity::with_result(res);
63 internals::result(*depth, &name, severity);
64 }
65}
66
67pub(crate) struct DisplayDebug<D>(pub(crate) D);
68
69impl<D: crate::lib::std::fmt::Debug> crate::lib::std::fmt::Display for DisplayDebug<D> {
70 fn fmt(&self, f: &mut crate::lib::std::fmt::Formatter<'_>) -> crate::lib::std::fmt::Result {
71 write!(f, "{:?}", self.0)
72 }
73}
74
75#[test]
76#[cfg(feature = "std")]
77#[cfg_attr(miri, ignore)]
78#[cfg(unix)]
79#[cfg(feature = "debug")]
80fn example() {
81 use term_transcript::{test::TestConfig, ShellOptions};
82
83 let path = snapbox::cmd::compile_example("string", ["--features=debug"]).unwrap();
84
85 let current_dir = path.parent().unwrap();
86 let cmd = path.file_name().unwrap();
87 let cmd = format!("./{}", cmd.to_string_lossy());
89
90 TestConfig::new(
91 ShellOptions::default()
92 .with_current_dir(current_dir)
93 .with_env("CLICOLOR_FORCE", "1"),
94 )
95 .test("assets/trace.svg", [cmd.as_str()]);
96}