| 1 | //! Helpers for boolean operations |
| 2 | use serde_jsonrust-analyzer cargo serde_json 1.0.114 crate/::Valuerust-analyzer cargo serde_json 1.0.114 value/Value# as Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#; |
| 3 | |
| 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/::JsonTruthyrust-analyzer cargo handlebars 5.1.2 json/value/JsonTruthy#; |
| 5 | |
| 6 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(eqrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/eq#: |x: Json, y: Json| x == y); |
| 7 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(nerust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/ne#: |x: Json, y: Json| x != y); |
| 8 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(gtrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/gt#: |x: i64, y: i64| x >rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt(). y); |
| 9 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(gterust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/gte#: |x: i64, y: i64| x >= y); |
| 10 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(ltrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/lt#: |x: i64, y: i64| x <rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]lt(). y); |
| 11 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(lterust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/lte#: |x: i64, y: i64| x <= y); |
| 12 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(andrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/and#: |x: Json, y: Json| x.is_truthyrust-analyzer cargo handlebars 5.1.2 json/value/impl#[Json][JsonTruthy]is_truthy().(false) && y.is_truthyrust-analyzer cargo handlebars 5.1.2 json/value/impl#[Json][JsonTruthy]is_truthy().(false)); |
| 13 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(orrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/or#: |x: Json, y: Json| x.is_truthyrust-analyzer cargo handlebars 5.1.2 json/value/impl#[Json][JsonTruthy]is_truthy().(false) || y.is_truthyrust-analyzer cargo handlebars 5.1.2 json/value/impl#[Json][JsonTruthy]is_truthy().(false)); |
| 14 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(notrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/not#: |x: Json| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().x.is_truthyrust-analyzer cargo handlebars 5.1.2 json/value/impl#[Json][JsonTruthy]is_truthy().(false)); |
| 15 | handlebars_helperrust-analyzer cargo handlebars 5.1.2 macros/handlebars_helper!!(lenrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/len#: |x: Json| { |
| 16 | match x { |
| 17 | Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.114 value/Value#Array#(a) => a.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), |
| 18 | Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#::Objectrust-analyzer cargo serde_json 1.0.114 value/Value#Object#(m) => m.lenrust-analyzer cargo serde_json 1.0.114 map/impl#[`Map<String, Value>`]len().(), |
| 19 | Jsonrust-analyzer cargo serde_json 1.0.114 value/Value#::Stringrust-analyzer cargo serde_json 1.0.114 value/Value#String#(s) => s.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]len().(), |
| 20 | _ => 0 |
| 21 | } |
| 22 | }); |
| 23 | |
| 24 | #[cfg(test)] |
| 25 | mod test_conditionsrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/ { |
| 26 | fn test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(condition: &str, expected: bool) { |
| 27 | let handlebars = craterust-analyzer cargo handlebars 5.1.2 crate/::Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 28 | |
| 29 | let result = handlebars |
| 30 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().( |
| 31 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 32 | "{{{{#if {condition}}}}}lorem{{{{else}}}}ipsum{{{{/if}}}}", |
| 33 | condition = condition |
| 34 | ), |
| 35 | &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({}), |
| 36 | ) |
| 37 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 38 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, if expected { "lorem" } else { "ipsum" }); |
| 39 | } |
| 40 | |
| 41 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 42 | fn foorust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/foo().() { |
| 43 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(gt 5 3)", true); |
| 44 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(gt 3 5)", false); |
| 45 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(or (gt 3 5) (gt 5 3))", true); |
| 46 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(not [])", true); |
| 47 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(and null 4)", false); |
| 48 | } |
| 49 | |
| 50 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 51 | fn test_eqrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_eq().() { |
| 52 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(eq 5 5)", true); |
| 53 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(eq 5 6)", false); |
| 54 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(eq "foo" "foo")"#, true); |
| 55 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(eq "foo" "Foo")"#, false); |
| 56 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(eq [5] [5])"#, true); |
| 57 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(eq [5] [4])"#, false); |
| 58 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(eq 5 "5")"#, false); |
| 59 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(eq 5 [5])"#, false); |
| 60 | } |
| 61 | |
| 62 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 63 | fn test_nerust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_ne().() { |
| 64 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(ne 5 6)", true); |
| 65 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().("(ne 5 5)", false); |
| 66 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(ne "foo" "foo")"#, false); |
| 67 | test_conditionrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_condition().(r#"(ne "foo" "Foo")"#, true); |
| 68 | } |
| 69 | |
| 70 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 71 | fn nested_conditionsrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/nested_conditions().() { |
| 72 | let handlebars = craterust-analyzer cargo handlebars 5.1.2 crate/::Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 73 | |
| 74 | let result = handlebars |
| 75 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (gt 5 3)}}lorem{{else}}ipsum{{/if}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({})) |
| 76 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 77 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, "lorem"); |
| 78 | |
| 79 | let result = handlebars |
| 80 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().( |
| 81 | "{{#if (not (gt 5 3))}}lorem{{else}}ipsum{{/if}}", |
| 82 | &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({}), |
| 83 | ) |
| 84 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 85 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, "ipsum"); |
| 86 | } |
| 87 | |
| 88 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 89 | fn test_lenrust-analyzer cargo handlebars 5.1.2 helpers/helper_extras/test_conditions/test_len().() { |
| 90 | let handlebars = craterust-analyzer cargo handlebars 5.1.2 crate/::Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 91 | |
| 92 | let result = handlebars |
| 93 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{len value}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"value": [1,2,3]})) |
| 94 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 95 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, "3"); |
| 96 | |
| 97 | let result = handlebars |
| 98 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{len value}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"value": {"a" :1, "b": 2}})) |
| 99 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 100 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, "2"); |
| 101 | |
| 102 | let result = handlebars |
| 103 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{len value}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"value": "tomcat"})) |
| 104 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 105 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, "6"); |
| 106 | |
| 107 | let result = handlebars |
| 108 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{len value}}", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"value": 3})) |
| 109 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 110 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&result, "0"); |
| 111 | } |
| 112 | } |