Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load partial from source when accessing partial in dev mode #434

Merged
merged 3 commits into from May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/partial.rs
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::HashMap;

use serde_json::value::Value as Json;
Expand All @@ -9,9 +10,31 @@ use crate::json::path::Path;
use crate::output::Output;
use crate::registry::Registry;
use crate::render::{Decorator, Evaluable, RenderContext, Renderable};
use crate::template::Template;

pub(crate) const PARTIAL_BLOCK: &str = "@partial-block";

fn find_partial<'reg: 'rc, 'rc: 'a, 'a>(
rc: &'a RenderContext<'reg, 'rc>,
r: &'reg Registry<'reg>,
d: &Decorator<'reg, 'rc>,
name: &str,
) -> Result<Option<Cow<'a, Template>>, RenderError> {
if let Some(ref partial) = rc.get_partial(name) {
return Ok(Some(Cow::Borrowed(partial)));
}

if let Some(tpl) = r.get_or_load_template_optional(name) {
return tpl.map(Option::Some);
}

if let Some(tpl) = d.template() {
return Ok(Some(Cow::Borrowed(tpl)));
}

Ok(None)
}

pub fn expand_partial<'reg: 'rc, 'rc>(
d: &Decorator<'reg, 'rc>,
r: &'reg Registry<'reg>,
Expand All @@ -30,10 +53,7 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
}

// if tname == PARTIAL_BLOCK
let partial = rc
.get_partial(tname)
.or_else(|| r.get_template(tname))
.or_else(|| d.template());
let partial = find_partial(rc, r, d, tname)?;

if let Some(t) = partial {
// clone to avoid lifetime issue
Expand Down
27 changes: 20 additions & 7 deletions src/registry.rs
Expand Up @@ -403,19 +403,32 @@ impl<'reg> Registry<'reg> {
}

#[inline]
fn get_or_load_template(&'reg self, name: &str) -> Result<Cow<'reg, Template>, RenderError> {
pub(crate) fn get_or_load_template_optional(
&'reg self,
name: &str,
) -> Option<Result<Cow<'reg, Template>, RenderError>> {
if let (true, Some(source)) = (self.dev_mode, self.template_sources.get(name)) {
source
let r = source
.load()
.map_err(|e| TemplateError::from((e, name.to_owned())))
.and_then(|tpl_str| Template::compile_with_name(tpl_str, name.to_owned()))
.map(Cow::Owned)
.map_err(RenderError::from)
.map_err(RenderError::from);
Some(r)
} else {
self.templates.get(name).map(|t| Ok(Cow::Borrowed(t)))
}
}

#[inline]
pub(crate) fn get_or_load_template(
&'reg self,
name: &str,
) -> Result<Cow<'reg, Template>, RenderError> {
if let Some(result) = self.get_or_load_template_optional(name) {
result
} else {
self.templates
.get(name)
.map(Cow::Borrowed)
.ok_or_else(|| RenderError::new(format!("Template not found: {}", name)))
Err(RenderError::new(format!("Template not found: {}", name)))
}
}

Expand Down