]> git.proxmox.com Git - rustc.git/blob - vendor/handlebars/src/decorators/inline.rs
New upstream version 1.47.0~beta.2+dfsg1
[rustc.git] / vendor / handlebars / src / decorators / inline.rs
1 use crate::context::Context;
2 use crate::decorators::{DecoratorDef, DecoratorResult};
3 use crate::error::RenderError;
4 use crate::registry::Registry;
5 use crate::render::{Decorator, RenderContext};
6
7 #[derive(Clone, Copy)]
8 pub struct InlineDecorator;
9
10 fn get_name<'reg: 'rc, 'rc>(d: &'rc Decorator<'reg, 'rc>) -> Result<&'rc str, RenderError> {
11 d.param(0)
12 .ok_or_else(|| RenderError::new("Param required for decorator \"inline\""))
13 .and_then(|v| {
14 v.value()
15 .as_str()
16 .ok_or_else(|| RenderError::new("inline name must be string"))
17 })
18 }
19
20 impl DecoratorDef for InlineDecorator {
21 fn call<'reg: 'rc, 'rc>(
22 &self,
23 d: &Decorator<'reg, 'rc>,
24 _: &'reg Registry<'reg>,
25 _: &'rc Context,
26 rc: &mut RenderContext<'reg, 'rc>,
27 ) -> DecoratorResult {
28 let name = get_name(d)?;
29
30 let template = d
31 .template()
32 .ok_or_else(|| RenderError::new("inline should have a block"))?;
33
34 rc.set_partial(name.to_owned(), template);
35 Ok(())
36 }
37 }
38
39 pub static INLINE_DECORATOR: InlineDecorator = InlineDecorator;
40
41 #[cfg(test)]
42 mod test {
43 use crate::context::Context;
44 use crate::registry::Registry;
45 use crate::render::{Evaluable, RenderContext};
46 use crate::template::Template;
47
48 #[test]
49 fn test_inline() {
50 let t0 = Template::compile(
51 "{{#*inline \"hello\"}}the hello world inline partial.{{/inline}}".to_string(),
52 )
53 .ok()
54 .unwrap();
55
56 let hbs = Registry::new();
57
58 let ctx = Context::null();
59 let mut rc = RenderContext::new(None);
60 t0.elements[0].eval(&hbs, &ctx, &mut rc).unwrap();
61
62 assert!(rc.get_partial(&"hello".to_owned()).is_some());
63 }
64 }