handlebars/helpers/
helper_log.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::context::Context;
use crate::helpers::{HelperDef, HelperResult};
#[cfg(not(feature = "no_logging"))]
use crate::json::value::JsonRender;
use crate::output::Output;
use crate::registry::Registry;
use crate::render::{Helper, RenderContext};
#[cfg(not(feature = "no_logging"))]
use crate::RenderErrorReason;
#[cfg(not(feature = "no_logging"))]
use log::Level;
#[cfg(not(feature = "no_logging"))]
use std::str::FromStr;

#[derive(Clone, Copy)]
pub struct LogHelper;

#[cfg(not(feature = "no_logging"))]
impl HelperDef for LogHelper {
    fn call<'reg: 'rc, 'rc>(
        &self,
        h: &Helper<'rc>,
        _: &'reg Registry<'reg>,
        _: &'rc Context,
        _: &mut RenderContext<'reg, 'rc>,
        _: &mut dyn Output,
    ) -> HelperResult {
        let param_to_log = h
            .params()
            .iter()
            .map(|p| {
                if let Some(relative_path) = p.relative_path() {
                    format!("{}: {}", &relative_path, p.value().render())
                } else {
                    p.value().render()
                }
            })
            .collect::<Vec<String>>()
            .join(", ");

        let level = h
            .hash_get("level")
            .and_then(|v| v.value().as_str())
            .unwrap_or("info");

        if let Ok(log_level) = Level::from_str(level) {
            log!(log_level, "{}", param_to_log);
        } else {
            return Err(RenderErrorReason::InvalidLoggingLevel(level.to_string()).into());
        }
        Ok(())
    }
}

#[cfg(feature = "no_logging")]
impl HelperDef for LogHelper {
    fn call<'reg: 'rc, 'rc>(
        &self,
        _: &Helper<'rc>,
        _: &Registry<'reg>,
        _: &Context,
        _: &mut RenderContext<'reg, 'rc>,
        _: &mut dyn Output,
    ) -> HelperResult {
        Ok(())
    }
}

pub static LOG_HELPER: LogHelper = LogHelper;

#[cfg(test)]
mod test {
    use crate::registry::Registry;

    #[test]
    #[cfg(not(feature = "no_logging"))]
    fn test_log_helper() {
        let mut handlebars = Registry::new();
        assert!(handlebars
            .register_template_string("t0", "{{log this level=\"warn\"}}")
            .is_ok());
        assert!(handlebars
            .register_template_string("t1", "{{log this level=\"hello\"}}")
            .is_ok());
        assert!(handlebars
            .register_template_string("t2", "{{log this}}")
            .is_ok());

        let r0 = handlebars.render("t0", &true);
        assert!(r0.is_ok());

        let r1 = handlebars.render("t1", &true);
        assert!(r1.is_err());

        let r2 = handlebars.render("t2", &true);
        assert!(r2.is_ok());
    }

    #[test]
    #[cfg(feature = "no_logging")]
    fn test_log_helper() {
        let mut handlebars = Registry::new();
        assert!(handlebars
            .register_template_string("t0", "{{log this level=\"warn\"}}")
            .is_ok());
        assert!(handlebars
            .register_template_string("t1", "{{log this level=\"hello\"}}")
            .is_ok());
        assert!(handlebars
            .register_template_string("t2", "{{log this}}")
            .is_ok());

        let r0 = handlebars.render("t0", &true);
        assert!(r0.is_ok());

        let r1 = handlebars.render("t1", &true);
        assert!(r1.is_ok());

        let r2 = handlebars.render("t2", &true);
        assert!(r2.is_ok());
    }
}