From 1152de2e41ff731a3e705be07472886280f3c845 Mon Sep 17 00:00:00 2001 From: Felix Obenhuber Date: Fri, 9 Mar 2018 12:19:25 +0100 Subject: [PATCH] (doc) add a hint about deserializing helper params This addition adds a note about the easy possiblity to work with helper parameters in their original type representation by using `Deserialize`. --- src/lib.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 93d61637a..9ebbf764a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,6 +264,33 @@ //! Data available to helper can be found in [Helper](struct.Helper.html). And there are more //! examples in [HelperDef](trait.HelperDef.html) page. //! +//! For complex helper implementations, the type of a parameter is more +//! convenient than its equivalent JSON representation. This requires those +//! types to implement `serde::Deserialize` in addition to `serde::Serialize`. +//! Be aware that the deserialization takes place on every call of the helper +//! and may be expensive! +//! +//! ``` +//!# extern crate handlebars; +//!# #[macro_use] +//!# extern crate serde_derive; +//!# extern crate serde_json; +//!# use handlebars::{Handlebars, RenderContext, Helper, HelperResult, RenderError}; +//!# fn main() {} +//! +//! #[derive(Deserialize)] +//! struct Foo; +//! +//! fn deserializing_helper(h: &Helper, _: &Handlebars, _: &mut RenderContext) -> HelperResult { +//! let a: Foo = h.param(0) +//! .ok_or(RenderError::new(format!("Parameter 0 is missing in {:?}", h))) +//! .map(|p| p.value().clone()) +//! .and_then(|v| serde_json::from_value(v).map_err(|e| e.into()))?; +//! // do something with "a" of type Foo +//! Ok(()) +//! } +//! ``` +//! //! You can learn more about helpers by looking into source code of built-in helpers. //! //! #### Built-in Helpers