Factor render functions

This commit is contained in:
Louis Dureuil 2025-05-27 18:27:58 +02:00
parent f26ab53941
commit 4fc0fd615a
No known key found for this signature in database

View File

@ -74,7 +74,22 @@ impl JsonTemplate {
Ok(Self { value, templates, bump }) Ok(Self { value, templates, bump })
} }
/// Renders this value by replacing all its strings with the rendered version of the template their represent from the contents of the given document. /// Renders this value by replacing all its strings with the rendered version of the template they represent from the given context.
///
/// # Error
///
/// - If any of the strings contains a template that cannot be rendered with the given context.
pub fn render(&self, context: &dyn liquid::ObjectView) -> Result<Value, Error> {
let mut rendered = self.value.clone();
for TemplateAtPath { template, path } in &self.templates {
let injected_value =
template.render(context).map_err(|err| error_with_path(err, path.clone()))?;
inject_value(&mut rendered, path, Value::String(injected_value));
}
Ok(rendered)
}
/// Renders this value by replacing all its strings with the rendered version of the template they represent from the contents of the given document.
/// ///
/// # Error /// # Error
/// ///
@ -83,33 +98,21 @@ impl JsonTemplate {
&'a self, &'a self,
document: D, document: D,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let mut rendered = self.value.clone();
let document = ParseableDocument::new(document, &self.bump); let document = ParseableDocument::new(document, &self.bump);
for TemplateAtPath { template, path } in &self.templates { self.render(&document)
let injected_value =
template.render(&document).map_err(|err| error_with_path(err, path.clone()))?;
inject_value(&mut rendered, path, Value::String(injected_value));
}
Ok(rendered)
} }
/// Renders this value by replacing all its strings with the rendered version of the template their represent from the contents of the search query. /// Renders this value by replacing all its strings with the rendered version of the template they represent from the contents of the search query.
/// ///
/// # Error /// # Error
/// ///
/// - If any of the strings contains a template that cannot be rendered from the contents of the search query /// - If any of the strings contains a template that cannot be rendered from the contents of the search query
pub fn render_search(&self, q: Option<String>, media: Value) -> Result<Value, Error> { pub fn render_search(&self, q: Option<String>, media: Value) -> Result<Value, Error> {
let mut rendered = self.value.clone();
let search_data = liquid::object!({ let search_data = liquid::object!({
"q": q, "q": q,
"media": media "media": media
}); });
for TemplateAtPath { template, path } in &self.templates { self.render(&search_data)
let injected_value =
template.render(&search_data).map_err(|err| error_with_path(err, path.clone()))?;
inject_value(&mut rendered, path, Value::String(injected_value));
}
Ok(rendered)
} }
} }