0

我正在尝试为车把注册一个助手,最终目标是能够拥有一个计算/打印长度的助手Vec。但是我已经无法将文档中的示例放入我的程序中。我设法做了一个“最小”示例,显示与我的火箭页面相同的错误。

#[macro_use]
extern crate handlebars;

extern crate rocket;
extern crate rocket_contrib;

use handlebars::{Context, Handlebars, Helper, HelperResult, JsonRender, Output, RenderContext};
use rocket_contrib::templates::Template;

handlebars_helper!(hex: |v: i64| format!("0x{:x}", v));

fn wow_helper(
    h: &Helper,
    _: &Handlebars,
    _: &Context,
    _: &mut RenderContext,
    out: &mut Output,
) -> HelperResult {
    if let Some(param) = h.param(0) {
        out.write("<b><i>")?;
        out.write(&param.value().render())?;
        out.write("</b></i>")?;
    }

    Ok(())
}

fn main() {
    rocket::ignite()
        .attach(Template::custom(|engines| {
            engines
                .handlebars
                .register_helper("mlenb", Box::new(wow_helper));
        }))
        .launch();
    println!("Hello, world!");
}

具有以下依赖项cargo.toml

[dependencies]
handlebars="3.3.0"
rocket="0.4.5"

[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["diesel_sqlite_pool", "handlebars_templates", "serve"]

错误:

error[E0631]: type mismatch in function arguments
   --> src/main.rs:184:43
    |
162 | / fn wow_helper(
163 | |     h: &handlebars::Helper,
164 | |     _: &handlebars::Handlebars,
165 | |     _: &handlebars::Context,
...   |
175 | |     Ok(())
176 | | }
    | |_- found signature of `for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7> fn(&'r handlebars::Helper<'s, 't0>, &'t1 handlebars::Handlebars<'t2>, &'t3 handlebars::Context, &'t4 mut handlebars::RenderContext<'t5, 't6>, &'t7 mut (dyn handlebars::Output + 't7)) -> _`
...
184 |                   .register_helper("mlenb", Box::new(wow_helper));
    |                                             ^^^^^^^^^^^^^^^^^^^^ expected signature of `for<'r, 'reg, 'rc, 's, 't0> fn(&'r rocket_contrib::templates::handlebars::Helper<'reg, 'rc>, &'reg rocket_contrib::templates::handlebars::Handlebars, &'rc rocket_contrib::templates::handlebars::Context, &'s mut rocket_contrib::templates::handlebars::RenderContext<'reg>, &'t0 mut (dyn rocket_contrib::templates::handlebars::Output + 't0)) -> _`
    |
    = note: required because of the requirements on the impl of `rocket_contrib::templates::handlebars::HelperDef` for `for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7> fn(&'r handlebars::Helper<'s, 't0>, &'t1 handlebars::Handlebars<'t2>, &'t3 handlebars::Context, &'t4 mut handlebars::RenderContext<'t5, 't6>, &'t7 mut (dyn handlebars::Output + 't7)) -> std::result::Result<(), handlebars::RenderError> {wow_helper}`
    = note: required for the cast to the object type `dyn rocket_contrib::templates::handlebars::HelperDef`
4

2 回答 2

1

不需要在 cargo.toml 中使用板条箱把手和火箭板条箱。它的工作原理是这样的:仅在 cargo.toml 中

[dependencies]
rocket = "0.4.5"
[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["handlebars_templates", "serves"]

并在 main.rs 中使用:

use rocket_contrib :: templates :: {Template, handlebars};
use handlebars :: {Helper, Handlebars, Context, RenderContext, Output, HelperResult, RenderError};

因此,对车把进行版本控制不会有任何问题!

于 2020-09-24T15:06:55.263 回答
0

感谢火箭矩阵频道中的@jebrosen:mozilla.org,问题得到了解决。

问题:

我使用了不兼容的车把版本。火箭库使用 1.0 版,而我在Cargo.toml.

解决方案:

更改Cargo.toml为:

[dependencies]
handlebars="1.0"
rocket="0.4.5"

[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["diesel_sqlite_pool", "handlebars_templates", "serve"]

小心重建需要cargo clean否则更改不会生成。

初始问题的解决方案:

我没有设法使用宏handlebars_helper!来创建长度助手,而是使用函数语法创建了它。

fn mlen_helper_fun(
    h: &handlebars::Helper<'_, '_>,
    _: &handlebars::Handlebars,
    _: &handlebars::Context,
    _: &mut handlebars::RenderContext<'_>,
    out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
    if let Some(param) = h.param(0) {
        if let serde_json::value::Value::Array(arr) = param.value() {
            out.write(&format!("{}", arr.len()))?;
            Ok(())
        } else {
            Err(handlebars::RenderError::new("Not a array"))
        }
    } else {
        Err(handlebars::RenderError::new(
            "No parameter given at least one required",
        ))
    }
}

然后可以将其用于:


        .attach(Template::custom(|engines| {
            engines
                .handlebars
                .register_helper("length", Box::new(mlen_helper_fun));
        }))
于 2020-08-04T06:36:13.183 回答