Debian Codegraph @

examples/render.rs

raw symbols SCIP
1 extern crate env_loggerrust-analyzer cargo handlebars 5.1.2 env_logger/;
2 extern crate handlebarsrust-analyzer cargo handlebars 5.1.2 handlebars/;
3
4 #[macro_use]
5 extern crate serde_deriverust-analyzer cargo handlebars 5.1.2 serde_derive/;
6 extern crate serde_jsonrust-analyzer cargo handlebars 5.1.2 serde_json/;
7
8 use serde_jsonrust-analyzer cargo serde_json 1.0.114 crate/::valuerust-analyzer cargo serde_json 1.0.114 value/::{Maprust-analyzer cargo serde_json 1.0.114 map/Map#, Valuerust-analyzer cargo serde_json 1.0.114 value/Value# as Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#};
9 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#;
10
11 use handlebarsrust-analyzer cargo handlebars 5.1.2 crate/::{
12 to_jsonrust-analyzer cargo handlebars 5.1.2 json/value/to_json()., Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, JsonRenderrust-analyzer cargo handlebars 5.1.2 json/value/JsonRender#, Outputrust-analyzer cargo handlebars 5.1.2 output/Output#, RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#,
13 RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#,
14 };
15
16 // define a custom helper
17 fn format_helperrust-analyzer cargo handlebars 5.1.2 format_helper().(
18 h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#,
19 _: &Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#,
20 _: &Contextrust-analyzer cargo handlebars 5.1.2 context/Context#,
21 _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#,
22 out: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#,
23 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> {
24 // get parameter from helper or throw an error
25 let param = h
26 .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(0)
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#("format", 0))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
28 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(out, "{} pts", param.valuerust-analyzer cargo handlebars 5.1.2 json/value/impl#[`PathAndJson<'rc>`]value().().renderrust-analyzer cargo handlebars 5.1.2 json/value/impl#[Json][JsonRender]render().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
29 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
30 }
31
32 // another custom helper
33 fn rank_helperrust-analyzer cargo handlebars 5.1.2 rank_helper().(
34 h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#,
35 _: &Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#,
36 _: &Contextrust-analyzer cargo handlebars 5.1.2 context/Context#,
37 _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#,
38 out: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#,
39 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> {
40 let rank = h.paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(0).and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|v| v.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().()).ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(
41 RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::ParamTypeMismatchForNamerust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#ParamTypeMismatchForName#("rank", "0".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "u64".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()),
42 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as usize;
43 let total = h
44 .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(1)
45 .as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()
46 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|v| v.valuerust-analyzer cargo handlebars 5.1.2 json/value/impl#[`PathAndJson<'rc>`]value().().as_arrayrust-analyzer cargo serde_json 1.0.114 value/impl#[Value]as_array().())
47 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|arr| arr.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().())
48 .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#::ParamTypeMismatchForNamerust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#ParamTypeMismatchForName#(
49 "rank",
50 "1".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
51 "array".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
52 ))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
53 if rank rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
54 out.writerust-analyzer cargo handlebars 5.1.2 output/Output#write().("champion")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
55 } else if rank rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().total rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().2 {
56 out.writerust-analyzer cargo handlebars 5.1.2 output/Output#write().("relegation")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
57 } else if rank rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().2 {
58 out.writerust-analyzer cargo handlebars 5.1.2 output/Output#write().("acl")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
59 }
60 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
61 }
62
63 static TYPESrust-analyzer cargo handlebars 5.1.2 TYPES.: &'static str = "serde_json";
64
65 // define some data
66 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:)]
67 pub struct Teamrust-analyzer cargo handlebars 5.1.2 Team# {
68 namerust-analyzer cargo handlebars 5.1.2 Team#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
69 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: u16,
70 }
71
72 // produce some data
73 pub fn make_datarust-analyzer cargo handlebars 5.1.2 make_data().() -> Maprust-analyzer cargo serde_json 1.0.114 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#> {
74 let mut data = Maprust-analyzer cargo serde_json 1.0.114 map/Map#::newrust-analyzer cargo serde_json 1.0.114 map/impl#[`Map<String, Value>`]new().();
75
76 data.insertrust-analyzer cargo serde_json 1.0.114 map/impl#[`Map<String, Value>`]insert().("year".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), to_jsonrust-analyzer cargo handlebars 5.1.2 json/value/to_json().("2015"));
77
78 let teams = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
79 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
80 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Jiangsu Suning".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
81 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 43u16,
82 },
83 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
84 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Shanghai SIPG".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
85 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 39u16,
86 },
87 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
88 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Hebei CFFC".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
89 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 27u16,
90 },
91 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
92 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Guangzhou Evergrand".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
93 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 22u16,
94 },
95 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
96 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Shandong Luneng".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
97 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 12u16,
98 },
99 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
100 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Beijing Guoan".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
101 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 7u16,
102 },
103 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
104 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Hangzhou Greentown".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
105 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 7u16,
106 },
107 Teamrust-analyzer cargo handlebars 5.1.2 Team# {
108 namerust-analyzer cargo handlebars 5.1.2 Team#name.: "Shanghai Shenhua".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
109 ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 4u16,
110 },
111 ];
112
113 data.insertrust-analyzer cargo serde_json 1.0.114 map/impl#[`Map<String, Value>`]insert().("teams".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), to_jsonrust-analyzer cargo handlebars 5.1.2 json/value/to_json().(&teams));
114 data.insertrust-analyzer cargo serde_json 1.0.114 map/impl#[`Map<String, Value>`]insert().("engine".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), to_jsonrust-analyzer cargo handlebars 5.1.2 json/value/to_json().(TYPESrust-analyzer cargo handlebars 5.1.2 TYPES.));
115 data
116 }
117
118 fn mainrust-analyzer cargo handlebars 5.1.2 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#>> {
119 env_loggerrust-analyzer cargo env_logger 0.11.11 crate/::initrust-analyzer cargo env_logger 0.11.11 logger/init().();
120 // create the handlebars registry
121 let mut handlebars = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().();
122
123 // register template from a file and assign a name to it
124 handlebars.register_template_filerust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_file().("table", "./examples/render/template.hbs")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
125
126 // register some custom helpers
127 handlebars.register_helperrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_helper().("format", Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(format_helperrust-analyzer cargo handlebars 5.1.2 format_helper().));
128 handlebars.register_helperrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_helper().("ranking_label", Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(rank_helperrust-analyzer cargo handlebars 5.1.2 rank_helper().));
129
130 // make data and render it
131 let data = make_datarust-analyzer cargo handlebars 5.1.2 make_data().();
132 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", handlebars.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("table", &data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
133 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
134 }