Debian Codegraph @

src/helpers/helper_log.rs

raw symbols SCIP
1 use craterust-analyzer cargo handlebars 5.1.2 crate/::contextrust-analyzer cargo handlebars 5.1.2 context/::Contextrust-analyzer cargo handlebars 5.1.2 context/Context#;
2 use craterust-analyzer cargo handlebars 5.1.2 crate/::helpersrust-analyzer cargo handlebars 5.1.2 helpers/::{HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef#, HelperResultrust-analyzer cargo handlebars 5.1.2 helpers/HelperResult#};
3 #[cfg(not(feature = "no_logging"))]
4 use craterust-analyzer cargo handlebars 5.1.2 crate/::jsonrust-analyzer cargo handlebars 5.1.2 json/::valuerust-analyzer cargo handlebars 5.1.2 json/value/::JsonRenderrust-analyzer cargo handlebars 5.1.2 json/value/JsonRender#;
5 use craterust-analyzer cargo handlebars 5.1.2 crate/::outputrust-analyzer cargo handlebars 5.1.2 output/::Outputrust-analyzer cargo handlebars 5.1.2 output/Output#;
6 use craterust-analyzer cargo handlebars 5.1.2 crate/::registryrust-analyzer cargo handlebars 5.1.2 registry/::Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#;
7 use craterust-analyzer cargo handlebars 5.1.2 crate/::renderrust-analyzer cargo handlebars 5.1.2 render/::{Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#};
8 #[cfg(not(feature = "no_logging"))]
9 use craterust-analyzer cargo handlebars 5.1.2 crate/::RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#;
10 #[cfg(not(feature = "no_logging"))]
11 use logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#;
12 #[cfg(not(feature = "no_logging"))]
13 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#;
14
15 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
16 pub struct LogHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/LogHelper#;
17
18 #[cfg(not(feature = "no_logging"))]
19 impl HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef# for LogHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/LogHelper# {
20 fn call<'reg: 'rc, 'rc>(
21 &self,
22 h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#<'rc>,
23 _: &'reg Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#<'reg>,
24 _: &'rc Contextrust-analyzer cargo handlebars 5.1.2 context/Context#,
25 _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#<'reg, 'rc>,
26 _: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#,
27 ) -> HelperResultrust-analyzer cargo handlebars 5.1.2 helpers/HelperResult# {
28 let param_to_log = h
29 .params()
30 .iter()
31 .map(|p| {
32 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref relative_path) = p.relative_path() {
33 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}: {}", relative_path, p.value().render())
34 } else {
35 p.value().render()
36 }
37 })
38 .collect::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>>()
39 .join(", ");
40
41 let level = h
42 .hash_get("level")
43 .and_then(|v| v.value().as_str())
44 .unwrap_or("info");
45
46 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(log_level) = Level::from_str(level) {
47 logrust-analyzer cargo log 0.4.34 crate/!(log_level, "{}", param_to_log)
48 } else {
49 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(RenderErrorReason::InvalidLoggingLevel(level.to_string()).into());
50 }
51 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
52 }
53 }
54
55 #[cfg(feature = "no_logging")]
56 impl HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef# for LogHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/LogHelper# {
57 fn callrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/impl#[LogHelper][HelperDef]call().<'reg: 'rc, 'rc>(
58 &self,
59 _: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#<'rc>,
60 _: &Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#<'reg>,
61 _: &Contextrust-analyzer cargo handlebars 5.1.2 context/Context#,
62 _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#<'reg, 'rc>,
63 _: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#,
64 ) -> HelperResultrust-analyzer cargo handlebars 5.1.2 helpers/HelperResult# {
65 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
66 }
67 }
68
69 pub static LOG_HELPERrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/LOG_HELPER.: LogHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/LogHelper# = LogHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/LogHelper#;
70
71 #[cfg(test)]
72 mod testrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/test/ {
73 use craterust-analyzer cargo handlebars 5.1.2 crate/::registryrust-analyzer cargo handlebars 5.1.2 registry/::Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#;
74
75 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
76 #[cfg(not(feature = "no_logging"))]
77 fn test_log_helper() {
78 let mut handlebars = Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#::new();
79 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
80 .register_template_string("t0", "{{log this level=\"warn\"}}")
81 .is_ok());
82 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
83 .register_template_string("t1", "{{log this level=\"hello\"}}")
84 .is_ok());
85 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
86 .register_template_string("t2", "{{log this}}")
87 .is_ok());
88
89 let r0 = handlebars.render("t0", &true);
90 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r0.is_ok());
91
92 let r1 = handlebars.render("t1", &true);
93 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r1.is_err());
94
95 let r2 = handlebars.render("t2", &true);
96 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r2.is_ok());
97 }
98
99 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
100 #[cfg(feature = "no_logging")]
101 fn test_log_helperrust-analyzer cargo handlebars 5.1.2 helpers/helper_log/test/test_log_helper().() {
102 let mut handlebars = Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().();
103 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
104 .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t0", "{{log this level=\"warn\"}}")
105 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
106 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
107 .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t1", "{{log this level=\"hello\"}}")
108 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
109 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
110 .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t2", "{{log this}}")
111 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
112
113 let r0 = handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t0", &true);
114 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r0.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
115
116 let r1 = handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t1", &true);
117 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r1.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
118
119 let r2 = handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t2", &true);
120 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(r2.is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
121 }
122 }