reintroduce dbg_dmp, move it to the error module
@@ -561,6 +561,46 @@ macro_rules! error_node_position(
});
);
+/// Prints a message and the input if the parser fails.
+///
+/// The message prints the `Error` or `Incomplete`
+/// and the parser's calling code.
+///
+/// It also displays the input in hexdump format
+///
+/// ```rust
+/// use nom::{IResult, error::dbg_dmp, bytes::complete::tag};
+///
+/// fn f(i: &[u8]) -> IResult<&[u8], &[u8]> {
+/// dbg_dmp(tag("abcd"), "tag")(i)
+/// }
+///
+/// let a = &b"efghijkl"[..];
+///
+/// // Will print the following message:
+/// // Error(Position(0, [101, 102, 103, 104, 105, 106, 107, 108])) at l.5 by ' tag ! ( "abcd" ) '
+/// // 00000000 65 66 67 68 69 6a 6b 6c efghijkl
+/// f(a);
+/// ```
+#[cfg(feature = "std")]
+#[cfg_attr(feature = "docsrs", doc(cfg(feature = "std")))]
+pub fn dbg_dmp<'a, F, O, E: std::fmt::Debug>(
+ f: F,
+ context: &'static str,
+) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], O, E>
+where
+ F: Fn(&'a [u8]) -> IResult<&'a [u8], O, E>,
+{
+ use crate::HexDisplay;
+ move |i: &'a [u8]| match f(i) {
+ Err(e) => {
+ println!("{}: Error({:?}) at:\n{}", context, e, i.to_hex(8));
+ Err(e)
+ }
+ a => a,
+ }
+}
+
#[cfg(test)]
#[cfg(feature = "alloc")]
mod tests {
