oni_comb_parser_rs/extension/parsers/
logging_parsers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::core::{ParseResult, Parsers};
use std::fmt::{Debug, Display};

#[derive(Debug, Clone)]
pub enum LogLevel {
  Debug,
  Info,
  Warn,
  Err,
}

pub trait LoggingParsers: Parsers {
  fn log<'a, I, A>(parser: Self::P<'a, I, A>, name: &'a str, log_level: LogLevel) -> Self::P<'a, I, A>
  where
    I: Debug,
    A: Debug + 'a, {
    Self::log_map(parser, name, log_level, move |a| format!("{:?}", a))
  }

  fn log_map<'a, I, A, B, F>(parser: Self::P<'a, I, A>, name: &'a str, log_level: LogLevel, f: F) -> Self::P<'a, I, A>
  where
    F: Fn(&ParseResult<'a, I, A>) -> B + 'a,
    I: Debug,
    A: Debug + 'a,
    B: Display + 'a;

  fn name<'a, I, A>(parser: Self::P<'a, I, A>, name: &'a str) -> Self::P<'a, I, A>
  where
    I: Debug,
    A: Debug + 'a;

  fn expect<'a, I, A>(parser: Self::P<'a, I, A>, name: &'a str) -> Self::P<'a, I, A>
  where
    I: Debug,
    A: Debug + 'a;
}