Skip to main content

xrust/parser/combinators/
debug.rs

1use crate::item::Node;
2use crate::parser::{ParseError, ParseInput, StaticState};
3use qualname::{NamespacePrefix, NamespaceUri};
4
5/// Emits a message to stderr from within the parser combinator. This can be useful for debugging.
6#[allow(dead_code)]
7pub fn inspect<'a, P1, A, N: Node, L>(
8    msg: &'a str,
9    parser: P1,
10) -> impl Fn(ParseInput<'a, N>, &mut StaticState<L>) -> Result<(ParseInput<'a, N>, A), ParseError> + 'a
11where
12    P1: Fn(ParseInput<'a, N>, &mut StaticState<L>) -> Result<(ParseInput<'a, N>, A), ParseError>
13        + 'a,
14    L: FnMut(&NamespacePrefix) -> Result<NamespaceUri, ParseError>,
15{
16    move |(input, state), ss| {
17        eprintln!(
18            "inspect pre: {} - input: \"{}\"",
19            msg,
20            input.chars().take(80).collect::<String>()
21        );
22        let result = parser((input, state.clone()), ss);
23        let errmsg = format!(
24            "error: {:?}",
25            result
26                .as_ref()
27                .map_or_else(|e| e, |_| &ParseError::Notimplemented)
28        );
29        eprintln!(
30            "inspect post: {} - input is now \"{}\"",
31            msg,
32            result
33                .as_ref()
34                .map_or_else(|_| errmsg, |((r, _), _)| r.to_string())
35                .chars()
36                .take(80)
37                .collect::<String>()
38        );
39        result
40    }
41}