Debian Codegraph @

src/helpers/helper_lookup.rs

raw symbols SCIP
1 use serde_jsonrust-analyzer cargo serde_json 1.0.114 crate/::valuerust-analyzer cargo serde_json 1.0.114 value/::Valuerust-analyzer cargo serde_json 1.0.114 value/Value# as Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#;
2
3 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#;
4 use craterust-analyzer cargo handlebars 5.1.2 crate/::errorrust-analyzer cargo handlebars 5.1.2 error/::RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#;
5 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#;
6 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/::ScopedJsonrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#;
7 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#;
8 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#};
9 use craterust-analyzer cargo handlebars 5.1.2 crate/::RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#;
10
11 #[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:)]
12 pub struct LookupHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/LookupHelper#;
13
14 impl HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef# for LookupHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/LookupHelper# {
15 fn call_innerrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/impl#[LookupHelper][HelperDef]call_inner().<'reg: 'rc, 'rc>(
16 &self,
17 h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#<'rc>,
18 r: &'reg Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#<'reg>,
19 _: &'rc Contextrust-analyzer cargo handlebars 5.1.2 context/Context#,
20 _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#<'reg, 'rc>,
21 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ScopedJsonrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#<'rc>, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> {
22 let collection_value = h
23 .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(0)
24 .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::ParamNotFoundForIndexrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#ParamNotFoundForIndex#("lookup", 0))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
25 let index = h
26 .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(1)
27 .ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::ParamNotFoundForIndexrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#ParamNotFoundForIndex#("lookup", 1))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
28
29 let value = match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().collection_value.valuerust-analyzer cargo handlebars 5.1.2 json/value/impl#[`PathAndJson<'rc>`]value().() {
30 Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.114 value/Value#Array#(ref v) => index.valuerust-analyzer cargo handlebars 5.1.2 json/value/impl#[`PathAndJson<'rc>`]value().().as_u64rust-analyzer cargo serde_json 1.0.114 value/impl#[Value]as_u64().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|u| v.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(u as usize)),
31 Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#::Objectrust-analyzer cargo serde_json 1.0.114 value/Value#Object#(ref m) => index.valuerust-analyzer cargo handlebars 5.1.2 json/value/impl#[`PathAndJson<'rc>`]value().().as_strrust-analyzer cargo serde_json 1.0.114 value/impl#[Value]as_str().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|k| m.getrust-analyzer cargo serde_json 1.0.114 map/impl#[`Map<String, Value>`]get().(k)),
32 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
33 };
34 if r.strict_moderust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]strict_mode().() && value.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
35 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#::strict_errorrust-analyzer cargo handlebars 5.1.2 error/impl#[RenderError]strict_error().(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#))
36 } else {
37 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(value.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(&Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#::Nullrust-analyzer cargo serde_json 1.0.114 value/Value#Null#).clonerust-analyzer cargo serde_json 1.0.114 value/impl#[Value][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
38 }
39 }
40 }
41
42 pub static LOOKUP_HELPERrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/LOOKUP_HELPER.: LookupHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/LookupHelper# = LookupHelperrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/LookupHelper#;
43
44 #[cfg(test)]
45 mod testrust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/test/ {
46 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#;
47
48 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
49 fn test_lookuprust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/test/test_lookup().() {
50 let mut handlebars = Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().();
51 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
52 .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t0", "{{#each v1}}{{lookup ../v2 @index}}{{/each}}")
53 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
54 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
55 .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t1", "{{#each v1}}{{lookup ../v2 1}}{{/each}}")
56 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
57 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars
58 .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t2", "{{lookup kk \"a\"}}")
59 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
60
61 let m = jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"v1": [1,2,3], "v2": [9,8,7]});
62
63 let m2 = jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({
64 "kk": {"a": "world"}
65 });
66
67 let r0 = handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t0", &m);
68 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r0.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), "987".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
69
70 let r1 = handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t1", &m);
71 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r1.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), "888".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
72
73 let r2 = handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t2", &m2);
74 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(r2.okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), "world".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().());
75
76 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup}}", &m).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
77 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(handlebars.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup v1}}", &m).is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
78 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
79 handlebars.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup null 1}}", &m).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
80 ""
81 );
82 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
83 handlebars.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup v1 3}}", &m).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
84 ""
85 );
86 }
87
88 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
89 fn test_strict_lookuprust-analyzer cargo handlebars 5.1.2 helpers/helper_lookup/test/test_strict_lookup().() {
90 let mut hbs = Registryrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().();
91
92 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
93 hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup kk 1}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"kk": []}))
94 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
95 ""
96 );
97 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hbs
98 .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup kk 0}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({ "kk": [null] }))
99 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
100
101 hbs.set_strict_moderust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]set_strict_mode().(true);
102
103 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hbs
104 .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup kk 1}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"kk": []}))
105 .is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
106 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hbs
107 .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{lookup kk 0}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({ "kk": [null] }))
108 .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().());
109 }
110 }