winnow/combinator/debug/
mod.rs
1#![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(all(feature = "debug", not(feature = "std")))]
11compile_error!("`debug` requires `std`");
12
13#[cfg_attr(not(feature = "debug"), allow(unused_variables))]
41#[cfg_attr(not(feature = "debug"), allow(unused_mut))]
42#[cfg_attr(not(feature = "debug"), inline(always))]
43pub fn trace<I: Stream, O, E>(
44 name: impl crate::lib::std::fmt::Display,
45 parser: impl Parser<I, O, E>,
46) -> impl Parser<I, O, E> {
47 #[cfg(feature = "debug")]
48 {
49 internals::Trace::new(parser, name)
50 }
51 #[cfg(not(feature = "debug"))]
52 {
53 parser
54 }
55}
56
57#[cfg_attr(not(feature = "debug"), allow(unused_variables))]
58pub(crate) fn trace_result<T, E>(
59 name: impl crate::lib::std::fmt::Display,
60 res: &Result<T, ErrMode<E>>,
61) {
62 #[cfg(feature = "debug")]
63 {
64 let depth = internals::Depth::existing();
65 let severity = internals::Severity::with_result(res);
66 internals::result(*depth, &name, severity);
67 }
68}
69
70#[test]
71#[cfg(feature = "std")]
72#[cfg_attr(miri, ignore)]
73#[cfg(unix)]
74#[cfg(feature = "debug")]
75fn example() {
76 use term_transcript::{test::TestConfig, ShellOptions};
77
78 let path = snapbox::cmd::compile_example("string", ["--features=debug"]).unwrap();
79
80 let current_dir = path.parent().unwrap();
81 let cmd = path.file_name().unwrap();
82 let cmd = format!("./{}", cmd.to_string_lossy());
84
85 TestConfig::new(
86 ShellOptions::default()
87 .with_current_dir(current_dir)
88 .with_env("CLICOLOR_FORCE", "1"),
89 )
90 .test("assets/trace.svg", [cmd.as_str()]);
91}