| 1 | #![allow(unused_imports, dead_code)] |
| 2 | extern crate env_loggerrust-analyzer cargo handlebars 5.1.2 env_logger/; |
| 3 | extern crate handlebarsrust-analyzer cargo handlebars 5.1.2 handlebars/; |
| 4 | extern crate serderust-analyzer cargo handlebars 5.1.2 serde/; |
| 5 | #[macro_use] |
| 6 | extern crate serde_deriverust-analyzer cargo handlebars 5.1.2 serde_derive/; |
| 7 | extern crate serde_jsonrust-analyzer cargo handlebars 5.1.2 serde_json/; |
| 8 | use serderust-analyzer cargo serde 1.0.229 crate/::Serializerust-analyzer cargo serde_core 1.0.229 ser/Serialize#; |
| 9 | use serde_jsonrust-analyzer cargo serde_json 1.0.114 crate/::valuerust-analyzer cargo serde_json 1.0.114 value/::{selfrust-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#}; |
| 10 | |
| 11 | 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#; |
| 12 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 13 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 14 | |
| 15 | use handlebarsrust-analyzer cargo handlebars 5.1.2 crate/::{ |
| 16 | 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#, |
| 17 | RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#, |
| 18 | }; |
| 19 | |
| 20 | // define a custom helper |
| 21 | fn format_helperrust-analyzer cargo handlebars 5.1.2 format_helper().( |
| 22 | h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, |
| 23 | _: &Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, |
| 24 | _: &Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, |
| 25 | _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#, |
| 26 | out: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#, |
| 27 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> { |
| 28 | // get parameter from helper or throw an error |
| 29 | let param = h |
| 30 | .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(0) |
| 31 | .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().; |
| 32 | 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().; |
| 33 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 34 | } |
| 35 | |
| 36 | // another custom helper |
| 37 | fn rank_helperrust-analyzer cargo handlebars 5.1.2 rank_helper().( |
| 38 | h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, |
| 39 | _: &Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, |
| 40 | _: &Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, |
| 41 | _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#, |
| 42 | out: &mut dyn Outputrust-analyzer cargo handlebars 5.1.2 output/Output#, |
| 43 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> { |
| 44 | 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().( |
| 45 | 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().()), |
| 46 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as usize; |
| 47 | let total = h |
| 48 | .paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(1) |
| 49 | .as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().() |
| 50 | .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().()) |
| 51 | .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().()) |
| 52 | .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#( |
| 53 | "rank", |
| 54 | "1".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 55 | "array".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 56 | ))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 57 | 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 { |
| 58 | 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().; |
| 59 | } 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 { |
| 60 | 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().; |
| 61 | } 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 { |
| 62 | 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().; |
| 63 | } |
| 64 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 65 | } |
| 66 | |
| 67 | static TYPESrust-analyzer cargo handlebars 5.1.2 TYPES.: &'static str = "serde_json"; |
| 68 | |
| 69 | // define some data |
| 70 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Serializerust-analyzer cargo serde_derive 1.0.229 Serialize:)] |
| 71 | pub struct Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 72 | namerust-analyzer cargo handlebars 5.1.2 Team#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 73 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: u16, |
| 74 | } |
| 75 | |
| 76 | // produce some data |
| 77 | 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#> { |
| 78 | 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().(); |
| 79 | |
| 80 | 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")); |
| 81 | |
| 82 | let teams = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 83 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 84 | 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().(), |
| 85 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 43u16, |
| 86 | }, |
| 87 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 88 | 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().(), |
| 89 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 39u16, |
| 90 | }, |
| 91 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 92 | 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().(), |
| 93 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 27u16, |
| 94 | }, |
| 95 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 96 | 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().(), |
| 97 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 22u16, |
| 98 | }, |
| 99 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 100 | 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().(), |
| 101 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 12u16, |
| 102 | }, |
| 103 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 104 | 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().(), |
| 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.: "Hangzhou Greentown".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.: 7u16, |
| 110 | }, |
| 111 | Teamrust-analyzer cargo handlebars 5.1.2 Team# { |
| 112 | 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().(), |
| 113 | ptsrust-analyzer cargo handlebars 5.1.2 Team#pts.: 4u16, |
| 114 | }, |
| 115 | ]; |
| 116 | |
| 117 | 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)); |
| 118 | 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.)); |
| 119 | data |
| 120 | } |
| 121 | |
| 122 | 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#>> { |
| 123 | env_loggerrust-analyzer cargo env_logger 0.11.11 crate/::initrust-analyzer cargo env_logger 0.11.11 logger/init().(); |
| 124 | let mut handlebars = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 125 | |
| 126 | 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().)); |
| 127 | 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().)); |
| 128 | // handlebars.register_helper("format", Box::new(FORMAT_HELPER)); |
| 129 | |
| 130 | let data = make_datarust-analyzer cargo handlebars 5.1.2 make_data().(); |
| 131 | |
| 132 | handlebars |
| 133 | .register_template_filerust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_file().("template", "./examples/render_file/template.hbs") |
| 134 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 135 | |
| 136 | let mut output_file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().("target/table.html")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 137 | handlebars.render_to_writerust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_to_write().("template", &data, &mut output_file)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 138 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("target/table.html generated"); |
| 139 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 140 | } |