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