1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
use tera::Context;
use crate::HumusFormat;
/// Provides context from the query for use in the [HumusEngine].
///
/// Its main job is the provide the requested [HumusFormat]
/// so that the Engine knows which output to produce.
///
/// Example implementation:
/// ```
/// use lib_humus::HumusQuerySettings;
/// use lib_humus::HtmlTextJsonFormat;
///
/// // Reuse the HtmlTextJsonFormat for now.
/// pub type ExampleResponseFormat = HtmlTextJsonFormat;
///
/// #[derive(Clone)]
/// pub struct ExampleQuerySettings {
/// pub response_format: ExampleResponseFormat,
/// }
///
/// impl HumusQuerySettings<ExampleResponseFormat> for ExampleQuerySettings {
/// fn get_format(&self) -> ExampleResponseFormat {
/// return self.response_format.clone();
/// }
/// }
/// ```
///
/// [HumusEngine]: ./struct.HumusEngine.html
/// [HumusFormat]: ./trait.HumusFormat.html
pub trait HumusQuerySettings<F>: Clone
where F: HumusFormat {
/// Called before rendering a template to initalize it with
/// values that come from the query itself.
///
/// 📖 Remember to document which values you set here in a place someone who
/// wants to do something with templating is able to find it.
///
/// The default implementation does nothing.
fn initalize_template_context(&self, _context: &mut Context) {}
/// Returns the requested output format.
fn get_format(&self) -> F;
}